无限循环,我不知道为什么

Infinite loop and i dont know why

本文关键字:为什么 我不知道 无限循环      更新时间:2023-10-16

我正在学校实验室工作,我似乎无法弄清楚为什么代码一直进入无限循环。我知道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");
}

只是变量prof应该是char而不是int

char prof;