I am trying to do a command line switch with an if statement. The problem I am running into is when I am using a letter, it wont recognize it. When I use a number, no problem. I think there are a few different ways to solve this. Can anyone show me the most direct way?

Thanks.

#include <iostream>

using namespace std;

int main ( int argc, char *argv[] )
{
	if (argv[1] == "-d")
	{
		cout<<"yes, it works"<<endl;
	}
else
	{
		cout<<"no, it won't read the if statement"<<endl;
	}
}

this should help, is not the perfect ways, but it works:

#include <iostream>
#include <cstring>

using namespace std;

int main ( int argc, char *argv[] )
{
    if(argc > 1){
    
	    if (strcmp(argv[1], "-d") == 0){
		        cout<<"yes, it works"<<endl;
	    }
        else{
		        cout<<"no, it won't read the if statement"<<endl;
	    }
	}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.