如何在 C++ 中使用用户输入在另一个内部创建多个 if 语句?

时间:2021-01-19 23:54:59

标签: c++ if-statement user-input

所以我是编程新手,尤其是 C++ 并且我正在尝试编写一个银行程序,我目前正在尝试让这个人成为一个“帐户”,询问他们是否想要制作 Basic、Premium 或 Elite帐户,然后用用户的“是”或“否”回答确认帐户。这是我的代码:

#include <iostream>
using namespace std;

int main()
{

    string mystr;
    cout << "Hello there, which account would you like to start today? Basic, Premium or Elite? ";
    getline(cin, mystr);

    string yn_answer;
    if (mystr == "Elite") {
        cout << "You have chosen the Elite account option. Is this correct? (y/n): ";
        getline(cin, yn_answer);
        if (yn_answer == "y")
            cout << "Great, setting up account... \n";
        cout << "25% complete. \n";
        cout << "50% complete. \n";
        cout << "75% complete. \n";
        cout << "100% complete. \n";
        cout << "Process complete. \n";
    }
    else {
        cout << "I'm sorry for the inconvenience, please restart the program. Thanks! ";
    }

    if (mystr == "Premium") {
        cout << "Hello, Premium";
    }

    if (mystr == "Basic") {
        cout << "Hello, Basic";
    }

    if (mystr != "Basic", "Elite", "Premium") {
        cout << "I'm sorry, you did not choose one of the following options above. Please try again.";
    }

    return 0;
}

目前我只有第一个,Elite,完成,当我知道如何让它工作时,将复制 Basic 和 Premium。但是,当我尝试通过在用户输入中输入 Elite 来运行它时,我得到了字符输出,“对不起,您没有选择上面的以下选项之一。请再试一次。”,从最后一行开始尝试检查用户是否输入了 Elite、Basic 或 Premium 以外的内容。

如果我输入的不是三个选项,我应该得到“对不起,您没有选择以下选项之一。请重试”。但是,我收到了第一条“错误”消息,该消息应该是检查用户确认帐户类型以及我应该收到的实际错误消息。

我对编程很陌生,我真的不知道如何解释这些事情。如果您不理解我正在尝试做的事情,请告诉我,我会尽力解释得更好。另外,有没有办法可以更好地做到这一点,或者有没有办法解决我在这里所做的事情?谢谢。

*另外,我听说为 std 使用命名空间不是一个好的做法,但我目前正在使用在线编译器,这就是它最初的样子,我不想改变周围的东西现在在解决这个问题之后把它弄得比现在更糟。我已经为此工作了一段时间,一直在修复它,直到现在我不知道如何修复它。

1 个答案:

答案 0 :(得分:0)

string mystr;
cout << "Hello there, which account would you like to start today? Basic, Premium or Elite? ";
cin >> mystr;

string yn_answer;
if (mystr == "Elite")
{
    cout << "You have chosen the Elite account option. Is this correct? (y/n): ";
    cin >> yn_answer;
    if (yn_answer == "y")
    {
        cout << "Great, setting up account... \n";
        cout << "25% complete. \n";
        cout << "50% complete. \n";
        cout << "75% complete. \n";
        cout << "100% complete. \n";
        cout << "Process complete. \n";
    }
    else
    {
        cout << "I'm sorry for the inconvenience, please restart the program. Thanks! ";
    }

    return 0;
}
else if (mystr == "Premium")
{
    cout << "You have chosen the Elite account";
}
else if (mystr == "Basic")
{
    cout << "You have chosen the Basic account";
}
else
{
    cout << "This isn't a choice, restart the program";
}

经过测试,它应该可以工作,我已经看到您在 if (yn_answer == "y") 处忘记了 { 并且您在最后添加了一个 },通常在您启动它时结束它,当您结束时,你启动它,但它一定是一个错误,别担心,我更喜欢使用简单的 cin 而不是 getline。 cin 一直在工作。另外,你的 3 if 最后没有用,因为 mystr 不能等于 Elite 的其他东西,因为在程序中的这一点 mystr 需要等于 Elite(第一个 if 语句)。

如果您需要建议,学习 C++ 的下一步可能是 while 循环,因此不再需要重新启动程序,例如

bool restart = true;
string mystr;

while (restart)
{
    cout << "Do you want to restart ? (y/n)";
    cin >> mystr;

    if (mystr == "y")
    {
        cout << "Nice !\n";
    }
    else
    {
        restart = false;
    }
}

如果你说n,它会重新开始循环

相关问题