This is the first code that I have tried to write without directly following a tutorial. It is a simple "if" statement, where each of the three possible answers (red, blue, or yellow)should lead to a different output. The code runs, but every input leads to the "else" statement, "That was not one of the choices", so my three words are not being recognized by the other three statements. I have spent about an hour trying to fix it myself, and have looked in forums, but have not found much concerning this type of word recognition. I am using Microsoft Visual C++ 2010 Express. Thank you.
# include <iostream>
int main()
{
usingnamespace std;
char word[80];
cout << "Which color do you like best, red, blue, or yellow? ";
cin >> word;
if(word == "red")
cout << endl << "Your favorite color is red. ";
elseif(word == "blue")
cout << endl << "Your favorite color is blue. ";
elseif(word == "yellow")
cout << endl << "Your favorite color is yellow. ";
else
cout << endl << "That was not one of the choices. ";
system("pause");
return 0;
}
Hey and welcome. Please edit your post and put all the code between code tags <> they are under the format section.
Move the
usingnamespace std;
to above main.
Is there any reason you are using a char array and not a string?
You cant compare it like you do in the if statements, becuase thats a char array.
It looks like you are using cout and cin which is c++, which means you should also be using string. Otherwise, if you decide to stick to char array for some reason, you'd have to use strcmp.
int main()
{
string word;
cout << "Which color do you like best, red, blue, or yellow? ";
cin >> word;
if (word == "red")
cout << endl << "Your favorite color is red. ";
elseif (word == "blue")
cout << endl << "Your favorite color is blue. ";
elseif (word == "yellow")
cout << endl << "Your favorite color is yellow. ";
else
cout << endl << "That was not one of the choices. ";
system("pause");
}
Thank you for the replies, the code tags make it much better. As far as the code, there was no reason why I was using a char array and not a string, aside from the fact that I had never seen string before. I don't have enough real experience to be able to fully appreciate the differences yet, but I will take your word for it and assume that it is better in this case. That being said, a made the suggested changes and am now receiving a build error. The code now looks like this:
# include <iostream>
usingnamespace std;
int main()
{
string word;
cout << "Which color do you like best, red, blue, or yellow? ";
cin >> word;
if(word == "red")
cout << endl << "Your favorite color is red. ";
elseif(word == "blue")
cout << endl << "Your favorite color is blue. ";
elseif(word == "yellow")
cout << endl << "Your favorite color is yellow. ";
else
cout << endl << "That was not one of the choices. ";
system("pause");
return 0;
}
I apologize if it seems that I am not doing enough to solve this on my own, but my knowledge is still a bit limited.
Thank you @TarikNeaj, it was the #include <string> that was the problem, it works great and I understand string now. @programmer007, I would also like to get this to work with char. I have the following, but feel like the # include <char> may not be right.
# include <iostream>
# include <char>
usingnamespace std;
int main()
{
char word[80];
cout << "Which color do you like best, red, blue, or yellow? ";
cin >> word;
if(strcmp(word,"red")==0)
cout << endl << "Your favorite color is red. ";
elseif(strcmp(word,"blue")==0)
cout << endl << "Your favorite color is blue. ";
elseif(strcmp(word,"yellow")==0)
cout << endl << "Your favorite color is yellow. ";
else
cout << endl << "That was not one of the choices. ";
system("pause");
return 0;
}
Also I sent you a link to a post that was asking the same and people showing how to solve it with strcmp etc. So you should give me the credit for that :(
@ TarikNeaj indeed I should give you credit for that, I apologize. I have read the previous post again and understand it much better after your help. Thanks again!