View Full Version : C++ question
cdietschrun
10-09-2008, 07:44 PM
I am making a C++ program and I am trying to check if any number of command line inputs matches an exact string.
For example
./a.out --pages-to-use 3
I need to see if there is an argument anywhere there that matches exactly "--pages-to-use" and if so, save the # ( 3, here ) to use later.
I was thinking a switch statement nested in a for loop...here's what I have, but the compiler is complaining that the switch is not an integer value. I know that you can use a character as a switch, so here's what I have.
for ( int index = 1 ; index < argc ; index++ ){
switch ( argv[index ] ) {
case 'a': cout << "yay" << endl;
break;
default: cout << "damn" << endl;
}
cout << argv[index] << endl ;
}
This was just testing, if any of the command-line arguments was 'a', I was going to print yay, if not there, damn.
Anyone?
Root Beer
10-09-2008, 07:58 PM
Switches only let you use integer types. A char is pretty-much an integer type, so that would work.
In this case, however, you're passing it an entire c-string. Check this out:
switch(argv[index]) //Illegal
But why?!
argv[index] is actually of type char*, so you won't be able to do that!
What you want to do is something like this:
int argument = 0;
if(strcmp("--pages-to-use", argv[index]) == 0)
{
cout<<"The strings match."<<endl;
//in this case the argument '3' would be in the following index, so you'd want to store that:
argument = atoi(argv[index+1]);
//The function atoi(char*) will convert a string of type char* to an integer. (The string of "3" will be thrown into an int.) Learn more about it here: http://www.cplusplus.com/reference/clibrary/cstdlib/atoi.html
}
else
cout<<"The strings do not match."<<endl;
The function strcmp will compare two strings to see if they match. You can read more about it here: http://www.cplusplus.com/reference/clibrary/cstring/strcmp.html
Hopefully this helps. Have fun!
cdietschrun
10-09-2008, 07:59 PM
Nevermind. Had to set the switch variable to dereference argv[index] :
*argv[index]...
cdietschrun
10-09-2008, 08:03 PM
Thank you man.
I was thinking a switch because I need to do something with the number at the end. i.e., if the string is exactly in the command line, the next number that appears after it needs to be saved into a preexisting variable. I dunno, I'm just starting on this program and seeing what works.
Root Beer
10-09-2008, 08:23 PM
Nevermind. Had to set the switch variable to dereference argv[index] :
*argv[index]...
You could dereference that, but you'd only be retrieving the first character in the string that was passed as an arg.
Example: char* argv[] = {"--pages-to-use", "3"};
In this case, *argv[0] = '-' , which isn't of much use to us.
Try out the string functions, as they should help a little bit more in terms of parsing the entire argument list.
I hope you find C++, as a language, as awesome as I do.
cdietschrun
10-09-2008, 10:51 PM
OK I didn't even test whether or not it got the whole string.
The main problem I thought a switch would solve is that the commands can be in any order, so long as the string matches exactly, it is a valid command that I need to use.
Any other ideas?
The main problem I thought a switch would solve is that the commands can be in any order, so long as the string matches exactly, it is a valid command that I need to use.
The switch doesn't solve that problem, the loop does.
Just use if clauses with strcmp to compare the strings as was suggested.
If you think about it, a switch statement is nothing more than a more restrictive shorthand for a series of if statements. There's nothing you can do with a switch statement that you can't do with a series of if statements.