做Loop Loop Endless

时间:2016-11-27 00:11:23

标签: c++

当你选择y时,试图找出为什么这将是一个无限循环。当用户选择y或Y时,它不应该离开do while循环吗?

#include <iostream>
using namespace std;

int main()
{
    int choice;


    do {
        cout << "Enter a number. If you would like to quit, press y.";
        cin >> choice;

        if (choice % 2 == 1)
        {
            cout << "Odd \n";
        }
        else
        {
            cout << "Even \n";
        }

    } while (choice != 'y'|| choice != 'Y');


    return 0;
}

1 个答案:

答案 0 :(得分:5)

choiceint;你不能把这封信y读进去。当你尝试时,它会打破&#34; cin,导致无休止的循环。