Exercise 3 - Command line arguments

#include <iostream>
#include <iomanip>
#include <string>

void help(void);

main(int argc, char *argv[])
{ int i, sw;
  string *cmdline;

  cmdline = new string[argc];
  if (cmdline != NULL)
   { for (i = 0; i < argc; i++)
      cmdline[i] = argv[i];

     for (i = 0; i < argc; i++)
      { sw = cmdline[i].find("--",0);
        if (sw != string::npos)
         { sw = cmdline[i].find("help", sw + 1);
           if (sw != string::npos)
            { help();
              break;
            }
         }
      }

     delete[] cmdline;
     return 0;
   }
  else
   { cerr << "ERROR: no space."
          << endl;
     return -1;
   }
}

void help(void)
{ cout << "This is the help."
       << endl;
}

There is really nothing new here. It is the previous example and uses the string class find method to first locate '--' and if found then locate 'help'.


Return to lesson


Copyright © 1999 - 2001 David Beech