Welcome to HBH! If you had an account on hellboundhacker.org you will need to reset your password using the Lost Password system before you will be able to login.

C++ command line argument help


ghost's Avatar
0 0

I'm trying to set up a C++ program to collect command line arguments, and run different code based on the argument, but every time I run the code, no matter which argument I use, I get the results from the final else statement: Invalid argument. I'm assuming the way I'm going about comparing the characters is wrong, or else it's the structure of my conditional statements. here's the code (haven't gotten to the real code yet, I wanted to get command line args working first…)

#include <iostream>
#include <fstream>
using namespace std;

void properUse()
{
    cout << "Usage:\n";
}

int main(int argc,char* argv[])
{
    if(argc==1)
    {
        properUse();
        return 1;
    }
    else if(argv[1]=="h")
    {
        properUse();
        return 0;
    }
    else if(argv[1]=="u")
    {
cout << "success, \"u\"";
    }
    else if(argv[1]=="d")
    {
return 0;
    }
	else if(argv[1]=="b")
	{
return 0;
    }
    else
    {
        cout << argv[0] << ": Invalid argument.\n";
        properUse();
        return 2;
        }
}

I tried to use single quotes, but I get an error about C++ not being able to compare pointers to integers. any advice would be appreciated. the only info I could find on google was on how to output the command arguments in a loop.


ghost's Avatar
0 0

to compare strings use strcmp() or strncmp()

take a look at the reference for further info


ynori7's Avatar
Future Emperor of Earth
0 0

You didn't say how you were entering your arguments, but here's what I'm guessing your problem is:

It gives you an error when you use single quotes because each element of your argv array is a string: argv[0] is your first argument, not the first character of your argument. argv is an array of character arrays.

You can then either make an array and set it equal to each argument, or you can use double array notation (e.g. argv[0][1] is the second character of your first argument).

EDIT: Here's an example to compare to (scroll to the very bottom). It's in C, but it should be about the same: http://www.hellboundhackers.org/code/number-base-converter-1223_c.html


ynori7's Avatar
Future Emperor of Earth
0 0

Longbow wrote: to compare strings use strcmp() or strncmp()

The == operator is overloaded in C++ to compare strings.


ghost's Avatar
0 0

I know they are strings, that much I gathered from googling, and the first element in the array is the name of the program, thus, I want the program to accept 2 command line arguments, one as the program name, and one, which was originally to be -h or -u etc, but the program always assumes that the command line args don't match anything, and default to the final option, which is to say you used an invalid argument.


ynori7's Avatar
Future Emperor of Earth
0 0

Because "-h" is not equal to "h". The dash is part of the string.


ghost's Avatar
0 0

no, now it crashes as soon as I run it. I changed the code a bit. it still doesn't work, but it's ouputting that I used the argument I should have even though it doesn't work. This is why I don't work with Windows. here's the new code (I added the dashes back. I had removed them to see if that was the problem originally, but it wasn't so I added them back)

#include <iostream>
#include <fstream>
using namespace std;

ofstream cipherFile;
void properUse()
{
    cout << "Usage:\n";
}

int main(int argc,char* argv[])
{
    if(argc==1)
    {
        properUse();
        return 1;
    }
    else if(argv[1]=="-h")
    {
        properUse();
        return 0;
    }
    else if(argv[1]=="-u")
    {
cout << "success, \"u\"";
    }
    else if(argv[1]=="-d")
    {
return 0;
    }
	else if(argv[1]=="-b")
	{
return 0;
    }
    else
    {
        cout << "Invalid argument: \"" << argv[1] << "\"\n";
        properUse();
        return 2;
        }
}

it output 'Invalid argument "-u"', despite this bit of code:

  else if(argv[1]=="-u")
    {
cout << "success, \"u\"";
    }

ynori7's Avatar
Future Emperor of Earth
0 0

Edit to what I said earlier: You need to include <string> for == to work with strings. Otherwise you can use strcmp as was suggested earlier. Try changing that and it should work.

EDIT:

This is why I don't work with Windows. This has nothing to do with Windows.


ghost's Avatar
0 0

I can't help that I don't know what I'm doing, I've never worked with command line arguments before, except to output them in a loop, which is completely useless. it works with strcmp, though. thank you.


ghost's Avatar
0 0

Possibly. I'd never heard of the strcmp command, though. If I had known that before, I probably wouldn't have even needed help (yet). Also, I have no idea how I screwed up my code before, but the first time i tried it with the strcmp command, the program immediately crashed with status -10756134 or something like that. I have the command line args all working as they should, now, so naturally, the program is quickly beginning to grow beyond my ability to code and debug. It's great for learning, though…


eXXon's Avatar
Member
0 0

dont know if this helps but this is what I usually use when taking arguments.


int main( int argc, char* argv[] )
{
  int x = 3; //number of arguments passed
	
  if( argc &lt; x )
  {
    help();
  }
  else
  {
    if( argv[1][0] == &#39;-&#39; )
    {
	if( argv[1][1] == &#39;h&#39; )
	{
	  help();
        }
	else if( argv[1][1] == &#39;e&#39; )
	{		{
          if( argv[2][0] == &#39;-&#39; )
          {
	    if( argv[2][1] == &#39;b&#39; )
            {
                //do something
            }
	    else if( argv[2][1] == &#39;o&#39; )
            {
               //do something
            }
            else
	    {
	      help();
	    }
         }
        else
        {
          help();
        }
      }
      else if( argv[1][1] == &#39;d&#39; )
      {
	if( argv[2][0] == &#39;-&#39; )
       {
          if( argv[2][1] == &#39;b&#39; )
          {
            //do something
          }
	  else if( argv[2][1] == &#39;o&#39; )
          {
             //do something
          }
	  else
	  {
		help();
	  }
       }
       else
       {
          help();
       }
     }
   }
   else 
   {
      help();
   }
  }
  return 0;
}

so it's just a bunch of conditional statements but it's just a way to get around the comparing strings part.. i know it might seem messy but it's actually easier for me to find what i'm looking for this way.

by the way sorry if the indentation is all messed up..