1

我拥有的这个函数调用另一个不返回任何内容的函数,然后如果输入 q,while 语句应该允许退出函数。它不工作。当我输入 q 或 Q 时,它会打印谢谢,再见。然后就像未满足条件一样开始切换。有任何想法吗?

void Watch::userInteraction()
{
    daysInMonthSwitch();
    char answer;
    while (answer != 'q' || answer != 'Q')
    {
        cout << "What would you like to do?" << endl;
        cout << "Enter r to run." << endl;
        cout << "Enter c to change time or date." << endl;
        cout << "Enter q to quit." << endl;
        cin >> answer;

        switch(answer)
        {
        case 'r':
            tick();
        break; 
        case 'R':
            tick();
        break;
        case 'c':
            changeTimeOrDate();
        break;
        case 'C':
            changeTimeOrDate();
        break;
        case 'q':
            cout << "Thank you, goodbye";
            break;
        case 'Q':
            cout << "Thank you, goodbye";
            break;
        default :
            userInteraction();
        }
    }
}
4

1 回答 1

1
while (answer != 'q' || answer != 'Q')

应该

while (answer != 'q' && answer != 'Q')

您还应该初始化answer.

此外,这不会导致任何问题,但如果您有两个具有相同操作的开关标签,则可以通过省略将它们组合起来以节省一些行break

case 'R':
case 'r':
    tick();
    break;
于 2013-04-16T02:33:27.043 回答