0

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;
	}
}
2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by programmersbook
0

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;
	    }
	}
}

Edited by programmersbook: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.