Input words not being recognized in simple "if" statement

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.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# include <iostream>

int main()
{
	using namespace 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.   ";
	else if(word == "blue")
		cout << endl << "Your favorite color is blue.   ";
	else if(word == "yellow")
		cout << endl << "Your favorite color is yellow.   ";
	else
		cout << endl << "That was not one of the choices.   ";

system("pause");
return 0;
}
Last edited on
Hey and welcome. Please edit your post and put all the code between code tags <> they are under the format section.

Move the

using namespace 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.

Read here - http://www.cplusplus.com/forum/beginner/8457/

If you can use string. Just do it like this -
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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. ";
	else if (word == "blue")
		cout << endl << "Your favorite color is blue. ";
	else if (word == "yellow")
		cout << endl << "Your favorite color is yellow. ";
	else
		cout << endl << "That was not one of the choices. ";

	system("pause");
}
when you make an array (char word[80] ) you have to use strings......
include string header file and replace

if(word=="red")

by

if(strcmp(word,"red")==0) and so on

ps: please use code tags :D
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# include <iostream>

using namespace 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.   ";
	else if(word == "blue")
		cout << endl << "Your favorite color is blue.   ";
	else if(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.
try strcmp method.
Hey @tjeromeo. Yes. Char array is what was used in C. in c++ we use string, Obviously char arrays still has their uses but not for these stuff.

String is not very complicated. There are tons of videos on youtube, and threads on the googleeeeeees that explains all of it and more! :)

I think the reason you're getting an error is because. In order to use string. You would have to include it.

so Add #include <string> right under #include <iostream>

Oh and for the future. If you got errors or anything along those lines, its very helpful to copy-paste those errors in the post aswell :)
Last edited on
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# include <iostream>
# include <char>

using namespace 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.   ";
	else if(strcmp(word,"blue")==0)
		cout << endl << "Your favorite color is blue.   ";
	else if(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;
}
In fact, now I see that # include <char> has an error, so I guess it does not exist.
Also, I just got it to work so I do not know what the problem was. Thanks again @programmer007!
Yeh #include <char> is not a thing :D

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 :(
Last edited on
@ 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!
happy to help :D
Topic archived. No new replies allowed.