gpt4 book ai didi

c++ - 如果我输入一个字母输入,为什么我的程序会卡在它的 while 循环中?

转载 作者:搜寻专家 更新时间:2023-10-31 01:05:45 24 4
gpt4 key购买 nike

对不起,如果我不够清楚或有任何错误,这是我第一次发帖。

我的代码在编译时运行没有错误,但第一个 while 循环(在 int main 中)在用户键入一个字母(如 “a”)时卡在循环中cin >> select; 而不是所需的 123

但是,当我输入 "4" 或任何其他随机数字字符串时,它运行正常并按照应有的方式转到我的错误消息。

为什么会这样,我该怎么做才能让它正常运行? (运行错误消息以响应输入的字母,就好像它们是数字一样)。

我的代码:

#include <iostream>
#include <string>
using namespace std;

void calculator();
void unavailableitem();

int main()
{
string select;
while (true)
{
cout << "\t[Main Menu]\n";
cout << " 1. Calculator\n";
cout << " 2. [unavailable]\n";
cout << " 3. [unavailable]\n";
cout << "\n Enter the number of your selection: ";
cin >> select;

if (select == "1")
{
cout << endl;
calculator();
break;
}
else if (select == "2")
{
unavailableitem();
break;
}
else if (select == "3")
{
unavailableitem();
break;
}
else
cout << "\nInvalid response.\n";
}
}

void unavailableitem()
{
string react;
cout << "\n \t [ITEM UNAVAILABLE]\n";
while (true)
{
cout << "\nEnter 'menu' to return to main menu: ";
cin >> react;

if (react == "menu")
{
cout << endl;
main();
break;
}
else
cout << "\nInvalid response.\n";
}
}

void calculator()
{
int choice;
double num1;
double num2;
double answer;
string choicesymbol;

cout << "List of operations:\n";
cout << " 1. Addition\n";
cout << " 2. Subtraction\n";
cout << " 3. Multiplication\n";
cout << " 4. Division\n";
cout << "Enter the number on the left to pick an operation: ";
cin >> choice;
cout << "\nEnter number 1: ";
cin >> num1;
cout << "\nEnter number 2: ";
cin >> num2;

if (choice == 1)
{
answer = num1 + num2;
choicesymbol = " + ";
}

if (choice == 2)
{
answer = num1 - num2;
choicesymbol = " - ";
}

if (choice == 3)
{
answer = num1 * num2;
choicesymbol = " * ";
}

if (choice == 4)
{
answer = num1 / num2;
choicesymbol = " / ";
}

cout << endl;
cout << num1 << choicesymbol << num2 << " = " << answer;
}

新代码:

#include <iostream>
#include <string>
using namespace std;

void calculator();
void unavailableitem();


int main()
{
int select;
while (true)
{
cout << "\t[Main Menu]\n";
cout << " 1. Calculator\n";
cout << " 2. [unavailable]\n";
cout << " 3. [unavailable]\n";
cout << "\n Enter the number of your selection: ";
cin >> select;

if(!(cin >> select))
{
cout << "Input must be an integer.\n";
cin.clear();
continue;
}
else if (select == 1)
{
cout << endl;
calculator();
break;
}
else if (select == 2)
{
unavailableitem();
break;
}
else if (select == 3)
{
unavailableitem();
break;
}
}
}



void unavailableitem()
{
string react;
cout << "\n \t [ITEM UNAVAILABLE]\n";
while (true)
{
cout << "\nEnter 'menu' to return to main menu: ";
cin >> react;

if (react == "menu")
{
cout << endl;
return;
break;
}
else
cout << "\nInvalid response.\n";
}
}

void calculator()
{
int choice;
double num1;
double num2;
double answer;
string choicesymbol;

cout << "List of operations:\n";
cout << " 1. Addition\n";
cout << " 2. Subtraction\n";
cout << " 3. Multiplication\n";
cout << " 4. Division\n";
cout << "Enter the number on the left to pick an operation: ";
cin >> choice;
cout << "\nEnter number 1: ";
cin >> num1;
cout << "\nEnter number 2: ";
cin >> num2;

if (choice == 1)
{
answer = num1 + num2;
choicesymbol = " + ";
}

if (choice == 2)
{
answer = num1 - num2;
choicesymbol = " - ";
}

if (choice == 3)
{
answer = num1 * num2;
choicesymbol = " * ";
}

if (choice == 4)
{
answer = num1 / num2;
choicesymbol = " / ";
}

cout << endl;
cout << num1 << choicesymbol << num2 << " = " << answer;
}

最佳答案

Ad Ed Heal 提到,这里的问题是cin 的故障位。当您执行 cin >> choice 时,用户键入“a”,然后转换为 int 失败。这设置了 cin 的 failbit,使得所有 future 的读取都失败,直到 failbit 被清除。因此,下次您到达 cin >> choice 时,用户甚至不会输入任何内容。

您可以使用 cin.clear() 恢复工作状态。

为了更稳健地做到这一点,你可以做类似的事情

while(true)
{
cout >> "Enter choice [1-4]: ";
if(!(cin >> choice))
{
cout << "Input must be an integer.\n";
cin.clear();
continue;
}
do_stuff_with_choice();
}

关于c++ - 如果我输入一个字母输入,为什么我的程序会卡在它的 while 循环中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22122945/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com