checking the value of argv[] in main()

So I have a program that I need to check what argv[1] is, depending on that it does something; however, I think the program is not checking it correctly, because I am putting in START but it skips what is in the if statement
CODE:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main(int argc, char* argv[])
{
	if (argv[1] == "Close")
	{
		exit(0);
	}
	else if (argv[1] == "START")
	{
                //Stuff
        }
	else
	{
		cout<<argv[1]<<"\n"<<argv[2];
		system("Pause"); //for debuging
	}


it always goes to the else at the end...

anyone know where I might be going wrong?

You need to use a string compare function, as you are dealing with a char*:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <string>

using namespace std;


int main(int argc, char **argv)
{
	char* instr = argv[1];

	if (strcmp(instr,"Close") == 0)
	{
		exit(0);
	}
	else if (strcmp(instr, "START") == 0)
	{
                //Stuff
        }
	else
	{
		cout<<argv[1]<<"\n"<<argv[2];
		system("Pause"); //for debuging
	}

	return 0;
}


Or, preferably use a std::string

HTH
The value of argc should be checked first, to see whether any parameters are present.

To use strcmp(), put #include <cstring> . If the strings are equal, the function returns zero, it is sufficient to use the ! operator in order to test for this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <cstring>

using namespace std;

int main(int argc, char **argv)
{
    if (argc > 1)
    {
        if (!strcmp(argv[1], "Close"))
        {
            return 0;
        }
        else if (!strcmp(argv[1], "START"))
        {
            cout << "Starting" << endl;
        }
        else
        {
            cout << "unknown parameter: " << argv[1] << endl;
        }
    }
    else
    {
        cout << "Missing parameter" << endl;
    }

    return 0;
}
Thank you very much!
Topic archived. No new replies allowed.