我正在学校实验室工作,我似乎无法弄清楚为什么代码会继续进入无限循环。我知道while语句有问题,但我不知道它是什么。
#include <iostream>
using namespace std;
int main()
{
int c = 0, p = 0, prof;
cout << "Do you like Coke or Pepsi? " << endl;
cin >> prof;
//put in data validation
while (prof != 'q' && prof != 'Q')
{
if (prof == 'p')
p++;
else if (prof == 'c')
c++;
cout << "Do you like Coke or Pepsi? " << endl;
cin >> prof;
}
if (p > c)
cout << "Pepsi Wins";
else if (p < c)
cout << "Coke Wins";
else
cout << " It's a tie";
system("Pause");
}
答案 0 :(得分:0)
变量prof
只需char
而不是int
char prof;