而循环在第二个用户输入C++后停止工作

while loop stops working after second user input C++

本文关键字:C++ 停止工作 输入 用户 循环 第二个      更新时间:2023-10-16

我正在尝试在我的程序中实现一个功能,上面写着"你想退出吗?如果"Y"或"y"退出程序。如果"N"或"n"重新运行菜单,让用户做任何他们想做的事情。

我面临的问题是我的菜单第一次运行,用户可以说是或否。然后第二次他们可以说是或否。然而,当它第三次到达while循环时,它会无限输出菜单。

任何人都可以提供建议吗?

菜单的代码是

        char exitInput = NULL;
        char Y = 'Y', y = 'y', N = 'N', n = 'n';
        while (exitInput != Y || exitInput != y || exitInput == N || exitInput == n)
        {
            cout << "*t Please choose one of the following options. t *" << endl;
            cout << "1. t" << "Transfer an amount n"
                 << "2. t" << "List recent transactions n"
                 << "3. t" << "Display account details and current balance n"
                 << "4. t" << "Quit n";
            int menuSelection;
            cout << "Enter your option here: ";
            cin >> menuSelection;
            switch (menuSelection)
            {
            case 1:
            .......
            case 2:
                cout << "Do you want to exit the application? ";
                cin >> exitInput;
            }
        }

此条件

while (exitInput != Y || exitInput != y || exitInput == N || exitInput == n)

永远都是真的。您必须将其更改为以下内容:

while (exitInput != Y && exitInput != y)

另外,exitInput的初始值没有多大意义,只需char exitInput = 'n'

我不确定我是否很好地理解了您的问题,但如果它是"当我说'是'时,菜单又回来了",这是正常的。 exitInput 不能同时等于 Y 和 y,因此 while 条件始终为真。

你应该尝试:

while ((exitInput != Y && exitInput != y) || exitInput == N || exitInput == n)

编辑

我可以在一个简单的 win32 应用程序中看到您的错误。我已经通过将menuselection的类型从 int 更改为 char 来解决它,但我不知道为什么它有时适用于整数。这是我的测试程序:

#include "stdafx.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
    char exitInput = 'n';
    char menuSelection;
    while (exitInput !='Y' && exitInput !='y')
    {
        cout << "*t Please choose one of the following options. t *" << endl;
        cout << "1. t" << "Transfer an amount " << endl
                << "2. t" << "List recent transactions " << endl
                << "3. t" << "Display account details and current balance " << endl
                << "4. t" << "Quit " << endl;
        cout << "Enter your option here: ";
        cin >> menuSelection;
        switch (menuSelection)
        {
        case '2':
            cout << "Do you want to exit the application? ";
            cin >> exitInput;
            break;
        default:            
            cout << "Command is : " << menuSelection << endl;
            cout << "Command not found !" << endl;
            break;
        }
    }
    return 0;
}

编辑 2

我找到了这个线程,其中解释了问题。 测试了,问题消失了。为什么我们会打电话给 cin-clear-and-cin-ign-ign-ignore-after-read-input

只需将 while 循环中的条件更改为:

while ((exitInput != Y) && (exitInput != y))

它将按预期工作。你的原始条件总是正确的,无论exitInput有什么值 - 这就是你编程失败的原因。请注意,无需检查 exitInput 是否为 n/Ny/Y 表示退出,任何其他输入表示不退出,因此您实际上只需要检查 y/Y

在 while() 检查中的比较中有一个错误。尝试这样的事情:

while (exitInput != Y && exitInput != y )
{
    cout << "*t Please choose one of the following options. t *" << endl;
    ...

我还可以评论其他事情,但它们没有解决您的问题。呵呵