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
 
./one t t

it should say
 
Foo

then exit.

But it doesn't. It just kindly replies:
 
t does not equal t


If you put the same character in argv[1] and argv[2] they still don't match. You should also get a response of
 
Foo

if you call it like
 
./one foo t

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?
You are comparing pointers. To compare strings, use strcmp() function
if (strcmp(argv[1],argv[2])==0 || strcmp(argv[1],"foo")==0)

http://www.cplusplus.com/reference/clibrary/cstring/strcmp/
Okay, thanks! That solves a lot of problems.
Topic archived. No new replies allowed.