0

我试图让一个程序工作,它会猜测用户正在考虑的数字,但是它目前每次只输出默认的开关动作。请告诉我我做错了什么?谢谢!

srand(time(0));
int lowernum = 1;
int highernum = 1000;
int number=rand()%highernum + lowernum;
string letter = "";
int letternum = 0;

cout<<"\nOkay, think of a number between one and 1000 
and I will try to guess it!\n";
cout<<"\nIs your number higher (h), lower (l) 
or exactly (e): " << number << "\n";
cin>>letter;

if (letter == "h")
{
    letternum = 1;
}
else if (letter == "l")
{
    letternum = 2;
}
else if (letter == "e")
{
    letternum = 3;
}

switch(letternum){
case'1': highernum = number; cout<<"\nIs your number higher (h), 
lower (l) or exactly (e): " << number << "\n"; cin>>letter;
    break;
case'2': lowernum = number; cout<<"\nIs your number higher (h), 
lower (l) or exactly (e): " << number << "\n"; cin>>letter;
    break;
case'3': cout<<"\nWahoooooo! I win! :D\n";
    break;
default:cout<<"\nI don't understand what you just typed in.\n";
    break;

}
4

2 回答 2

3

letternum是一个整数,你的 switch 语句使用字符(例如'1'),只需从你的 case 表达式中的字符中删除引号:

switch(letternum){
case 1: 

ETC...

于 2012-10-22T22:28:37.740 回答
1

因为您的开关标签是字符,但您的 letternum 是 int,所以它们不匹配。

于 2012-10-22T22:29:01.727 回答