Comparing a string to a defined word.
Jan 5, 2010 at 6:24pm UTC
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?
Jan 5, 2010 at 7:05pm UTC
change ' with "
' is for chars
" is for strings
Jan 5, 2010 at 7:07pm UTC
Still doesn't work.
Last edited on Jan 5, 2010 at 7:17pm UTC
Jan 5, 2010 at 7:18pm UTC
Actually, you can't use switch
with strings, only with integral types.
You would have to use ifs/elses to do it.
Jan 5, 2010 at 7:19pm UTC
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 Jan 5, 2010 at 7:25pm UTC
Topic archived. No new replies allowed.