C++ command line argument help
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.
to compare strings use strcmp() or strncmp()
take a look at the reference for further info
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
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.
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\"";
}
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…
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 < x )
{
help();
}
else
{
if( argv[1][0] == '-' )
{
if( argv[1][1] == 'h' )
{
help();
}
else if( argv[1][1] == 'e' )
{ {
if( argv[2][0] == '-' )
{
if( argv[2][1] == 'b' )
{
//do something
}
else if( argv[2][1] == 'o' )
{
//do something
}
else
{
help();
}
}
else
{
help();
}
}
else if( argv[1][1] == 'd' )
{
if( argv[2][0] == '-' )
{
if( argv[2][1] == 'b' )
{
//do something
}
else if( argv[2][1] == 'o' )
{
//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..