尽管为假,循环也不会退出

时间:2020-04-28 16:36:11

标签: c++ debugging

我知道这是一个基本问题,但我找不到解决方案。 尽管我输入了使循环为假的内容,但循环仍在继续。

{

        cout << "1 - Load Movie File" << endl;
        cout << "2 - View Total Movie Database Runtime" << endl;
        cout << "3 - View Movies by Rating" << endl;
        cout << "4 - Export Movies by Genre" << endl;
        cout << "5 - Clear Currently Loaded Database" << endl;
        cout << "6 - Print Database Sorted by Year" << endl;
        cout << "Q - Quit" << endl;
        cout << "Selection >> ";

        do{
                cin >> selectionMenu;
                selectionMenu = tolower(selectionMenu);

                if(selectionMenu != '1' || selectionMenu != '2' || selectionMenu != '3' || selectionMenu != '4' || selectionMenu != '5' || selectionMenu != '6' || selectionMenu != 'q' )
                {

                        cout << "Please enter a valid selection: ";

                }

        }while(selectionMenu != 1 || selectionMenu != 2 || selectionMenu != 3 || selectionMenu != 4 || selectionMenu != 5 || selectionMenu != 6 || selectionMenu != 'q' );



}

selectionMenu变量是一个字符,以防有人询问

1 个答案:

答案 0 :(得分:1)

这种情况:

if(selectionMenu != '1' || selectionMenu != '2' || selectionMenu != '3' || selectionMenu != '4' || selectionMenu != '5' || selectionMenu != '6' || selectionMenu != 'q' )

保证始终为真。使用&&代替||