problem with reading a string and comparing

Hi!
I'm trying to read a string and then compare it to a word.
My code:

char* cmd = "start";
std::cin >> cmd;
if (strcmp (cmd, "eval"))
eval ();

When the program gets to the cin part, it breaks.
Thank you!!!!
strcmp returns 0 when the strings are equal.

http://en.cppreference.com/w/cpp/string/byte/strcmp
That because your cmd variable points to read only memory. When you trying to write into it it will crash. Declare your cmd variable like char cmd[10] = "start"; or char cmd[] = "start" or dymamically alloocate memory.
Even better would be use of std::string — no danger of overflow, no memory handling problems.
Thank you.
Peter87 - I added the "==0", and it didn't help.
MiiNiPaa - I changed it to std::string cmd, but when I try to use strcmp, I get an error - cannot convert argument 1 from 'std::string' to 'const char *'
Your first tip did it. Thank you!!!!
Last edited on
Well, you do not need strcmp with std::string, you can compare directly: if(cmd == "eval")
Topic archived. No new replies allowed.