Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 339
  • Last Modified:

c++ Command line arguments

I have a c++ program and i need to add some command line arguments ... i only need 1 argument to be passed in. there is only a possiibilty of 2 arguments, -day or -month, i need to have it so only a maximum of 1 arguement can be used but i would also like to be able to have NO arguments used and have it default to a certain string for eg

string test = "month" //defualt test string

//main prog
string arg = argv(1);
if (arg == "day") {
string test = "day";
}

if an arguement is given but does not equal day or month than we quit with an error message, if no argument given than the program continues on like normal and since we allready declared that "test" = month that effectively is the default string.
Any help in getting this right would be great, i am not sure if string is the right thing to be using so i don't care what anyone uses, i just need somesort of variable or something that holds that value "day" or "month".

Thanks





0
bulldogs
Asked:
bulldogs
  • 5
  • 2
1 Solution
 
sunnycoderCommented:
if ( argc > 2 )
    // more arguments than you expected

else if (argc == 1 )
    //use default arguments

else
     if ( strcmp ( argv[1], "-day") == 0 || strcmp ( argv[1], "-month" ) == 0 )
                //valid arguments
     else
               //invalid argument
0
 
sunnycoderCommented:
you can ofcourse replace if else consructs with
switch (argc)
0
 
bulldogsAuthor Commented:
ok we are getting somewhere :-)

only prob now is if i start my program with NO arguments that works ok, if i specify -day or -month that works, if i specify more than 1 argument if gives me my error message :-) but if i specify the wrong command line argument the program just continues on as normal? here is the code i have now

string arg = argv[1];
if ( argc > 2 ) {
// more arguments than you expected
cout << "too many arguments" << endl;
exit(1);
}
else if (argc == 1 )
{
//use default arguments
//string test = "month";
cout << "No arguments given" << endl;
exit(1);
}
else
if ( strcmp ( argv[1], "-day") == 0 || strcmp ( argv[1], "-month" ) == 0 ) {
           if (arg == "-day") {
              cout << "day argument found" << endl;
              //string test = "day"
              exit(1);
             }
             else if (arg == "-month") {
                cout << "month argument found" << endl;
                //string test = "month"
                exit(1);
               }
         else {
           cout << "Don't know that argument" << endl;
           exit(1);
             }
          }
cout << "Oops we should never reach this point" << endl;
exit(1);
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
sunnycoderCommented:
>if ( strcmp ( argv[1], "-day") == 0 || strcmp ( argv[1], "-month" ) == 0 ) {
would be true if argv[1] is either -day or -month
try
if ( (strcmp ( argv[1], "-day") == 0) || (strcmp ( argv[1], "-month" ) == 0) ) {

> the program just continues on as normal?
err as normal means? executes code for day/month
0
 
sunnycoderCommented:
i just tried the code snippet and it seems to work fine ... are you sure you are not having some = in place of == or something similar
0
 
sunnycoderCommented:
found your problem

    else {
          cout << "Don't know that argument" << endl;
          exit(1);

is treated as else of
if (arg == "-month") {

you need to close the

if ( strcmp ( argv[1], "-day") == 0 || strcmp ( argv[1], "-month" ) == 0 ) {
for the said else being treated correctly

 else if (arg == "-month") {
               cout << "month argument found" << endl;
               //string test = "month"
               exit(1);
              }
         }        =========== added
        else {
          cout << "Don't know that argument" << endl;
          exit(1);
            }
         } ========== remove this
0
 
bulldogsAuthor Commented:
Thanks sunnycoder that works great :-)
0

Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now