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

时间:2017-10-20 14:34:56

标签: c++

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

}

1 个答案:

答案 0 :(得分:0)

变量prof只需char而不是int

char prof;