Why can't I compare command-line arguments in an if statement?
For some reason, I can't compare arguments passed through the command line. I don't know the reason, but hopefully some of you do.
For example, in this thingy I wrote:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
#include <iostream>
using namespace std;
// Corbin
int main(int argc, char* argv[]) {
if (argv[1] && argv[2]) {
if (argv[1] == argv[2] || argv[1] == "foo") {
cout << "Foo \n";
return 0;
}
else {
cout << argv[1] << " does not equal " << argv[2] << endl;
return 1;
}
}
}
|
It never executes properly.
If you call it like
it should say
then exit.
But it doesn't. It just kindly replies:
If you put the same character in argv[1] and argv[2] they still don't match. You should also get a response of
if you call it like
It should also return "foo", but still, no dice. I cannot compare a command line argument to anything.
Can somebody please explain why, and how to fix it?
Okay, thanks! That solves a lot of problems.
Topic archived. No new replies allowed.