Comparing a string to a defined word.

I tried to let a person type in a word and based on that word it would do different things, sadly this didn't work I used:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;
int main()
{
	string Input;

	cout << "What is your favo fruit?" << endl;
	cin >> Input;

	switch( Input )
	{
		case 'appel':	cout << "That's a good choose!" << endl;
						break;
		case 'taco':	cout << "Thats not a fruit!" << endl;
						break;
		default:		cout << "I didnt recognize that" << endl;
	}
}


Is their a simple way to solve this?
change ' with "
' is for chars
" is for strings
Still doesn't work.
Last edited on
Actually, you can't use switch with strings, only with integral types.

You would have to use ifs/elses to do it.
switch statements require integral types.

The easy fix is to use an if/if-else/else to get that behavior. See: http://www.cplusplus.com/doc/tutorial/control/

If you were really adamant about using the switch you could some kind of integral-type-returning hash function on Input and on your target values. That's kind of silly, though.
Last edited on
Topic archived. No new replies allowed.