如何在 C++ 中接受用户输入的多个 if 语句?
How do I make multiple if statements inside one another taking user input in C++?
所以我是编程新手,尤其是 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 使用命名空间不是一个好习惯,但我目前正在使用在线编译器,这就是它最初的样子,我不想改变在处理这个问题之后,现在的事情比现在更糟。我已经为此工作了一段时间,一直在修复它,直到现在我不知道如何修复它。
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";
}
经过测试,它应该可以工作,我看到你忘记了一个 { at 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,它会重新开始循环
所以我是编程新手,尤其是 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 使用命名空间不是一个好习惯,但我目前正在使用在线编译器,这就是它最初的样子,我不想改变在处理这个问题之后,现在的事情比现在更糟。我已经为此工作了一段时间,一直在修复它,直到现在我不知道如何修复它。
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";
}
经过测试,它应该可以工作,我看到你忘记了一个 { at 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,它会重新开始循环