C string comparison problem in c++
Question
I've been having trouble with comparison in my c++ program. This is the boiled down version.
#include "stdafx.h"
#include <iostream>
#include <windows.h>
using namespace std;
int main(int argc, char *argv[])
{
if(argc>2){cout<<"3+Args"<<endl;}else//???
if(argc==2){
cout<<"2args"<<endl;
if(argv[1]=="/hide-icons"){}
if(argv[1]=="/show-icons"){}
if(argv[1]=="/reinstall"){setAsDefault();}
if(argv[1]=="/?"){showPossibleCommands();}
if(argv[1]=="/1"){cout<<"go"<<endl;FirstRun();}
}else{showPossibleCommands();cout<<argv[0]<<endl;}
return 0;
}
When I run "programname.exe /1", my program writes "2args" but not "go". Am I missing something obvious?
Solution
argv[1]
is a char*
, so by testing with ==
you're checking if the pointer points to the same spot as the start of the various string constants you're using... which is not going to be the case. To compare contents instead, use strcmp.
OTHER TIPS
The problem is, that your code compares the pointers to the strings, not the stings itself.
You have to replace the compares with calls to the string-compare function.
E.g.
if(argv[1]=="/1"){cout<<"go"<<endl;FirstRun();}
becomes
if(strcmp(argv[1],"/1") == 0) {cout<<"go"<<endl;FirstRun();}
You may have to include string.h to get the strcmp prototype into your code.
Another option is to convert the C-style arguments into a much more friendly vector of strings and process them instead:
#include <string>
#include <vector>
typedef std::vector<std::string> parameter_list;
int
cpp_main(std::string const& program_name, parameter_list const& params) {
for (parameter_list::const_iterator arg=params.begin(); arg!=params.end(); ++arg) {
if (*arg == "/hide-icons") {
} else if (*arg == "/show-icons") {
} else if (*arg == "/reinstall") {
set_as_default();
} else if (*arg == "/?") {
show_help(program_name);
} else if (*arg == "/1") {
first_run();
} else {
show_help(program_name);
}
}
return 0;
}
int
main(int argc, char **argv) {
return cpp_main(argv[0], parameter_list(&argv[1], &argv[argc]));
}