i wrote this code originally with "command1" as a string and it worked fine but when i changed it to a char the "if (command1 == "open")" and other checks stopped testing true...
any idea or hint would be appreciated...


program just checks input against a list of "if" statements and outputs correct "response".

-----------------------------------------------

1.  #include <cstdlib>
2.  #include <iostream>
3.
4.  using namespace std;
5.
6.  int main(int argc, char *argv[]){
7.      char command1[256];
8.
9.      do{
10.          cout << "Enter command... (enter, pass, return, what or done)\n";
11.          cin.getline (command1, sizeof(command1));
12.    
13.          if (command1 == "enter"){
14.              cout << "Entered.\n\n";}
15.          else if (command1 == "return"){
16.              cout << "Returned.\n\n";}
17.          else if (command1 == "pass"){
18.              cout << "Passed.\n\n";}
19.          else if (command1 == "what"){
20.              cout << "what yourself.\n\n";}
21.          else if (command1 != "done"){
22.              cout << "I dont understand.\n\n";}
23.    }while (command1 != "done");
24.    
25.   
26.    system("PAUSE");
27.    return EXIT_SUCCESS;
28.    }

------------------------------------------------

thank you

Recommended Answers

All 3 Replies

You cannot compare strings with the == operator. Look up the function strcmp()

You cannot compare strings with the == operator. Look up the function strcmp()

true so call conditional statements like this
if (strcmp(command1,"enter")>0)
if (strcmp(command1,"enter")!=0)
should work

Another suggestion is replace the character array with std::string and you can keep those == comparison operators because std::string permits them.

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.