strcmp not working (console)

Hey guys,

so, for my homework, I got to do this project which works like a database with people. I just started and I already got stuck. In the startup menu, I ask the user to enter what they want to do and put it in a string.
1
2
3
string option;
cout << "What do you want to do?: ";
cin >> option;


Then, I check if what the user typed was "displayall", and thats where the problem comes.

1
2
3
4
if(strcmp(option,"displayall"))
{
    // do stuff
}


I get an error on this line when I try to compile it:
 
53 C:\Dev-Cpp\Projects\For School\Database\database.cpp no matching function for call to `strcmp(std::string&, const char[5])'  


Do you guys know why this is happening? I included string.h at the top.
Thanks.
That's because strcmp is expecting const char * as the first parameter, but string has no implict conversion to const char *.

Two solutions.
1) If you really want to use strcmp, then do the following:
 
if(strcmp(option.c_str(),"displayall"))

The c_str() member function will return a const char *.

2) The better solution is just to do a simple string compare:
 
if (option == "displayall")

The string class supports comparison operators with a quoted literal.
Topic archived. No new replies allowed.