How come if I enter a letter input my program gets stuck in its while loop?(如果我输入一个字母,我的程序怎么会卡在它的 while 循环中?)
问题描述
对不起,如果我说得不够清楚或犯了任何错误,这是我第一次发帖.
Sorry if I fail to be clear enough or make any mistakes, this is my first time posting.
我的代码在编译时运行没有错误,但第一个 while 循环(在 int main 中)在用户输入字母(如 "a"
)时卡住循环cin>>select;
而不是必需的 1
、2
或 3
.
My code runs without errors when complied but the first while loop (in int main) gets stuck looping whenever a user types a letter (like "a"
) for cin >> select;
instead of the required 1
, 2
, or 3
.
但是,当我输入 "4"
或任何其他随机数字字符串时,它运行良好并像它应该的那样转到我的错误消息.
However, when I input "4"
or any other random string of numbers, it runs fine and goes to my error message like it should.
这是为什么,我该怎么做才能让它正常运行?(运行错误消息以响应输入的字母,就好像它们是数字一样).
Why is this and what can I do to make it run normally? (run the error message in response to letters entered as if they were numbers).
我的代码:
#include <iostream>
#include <string>
using namespace std;
void calculator();
void unavailableitem();
int main()
{
string select;
while (true)
{
cout << " [Main Menu]
";
cout << " 1. Calculator
";
cout << " 2. [unavailable]
";
cout << " 3. [unavailable]
";
cout << "
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 << "
Invalid response.
";
}
}
void unavailableitem()
{
string react;
cout << "
[ITEM UNAVAILABLE]
";
while (true)
{
cout << "
Enter 'menu' to return to main menu: ";
cin >> react;
if (react == "menu")
{
cout << endl;
main();
break;
}
else
cout << "
Invalid response.
";
}
}
void calculator()
{
int choice;
double num1;
double num2;
double answer;
string choicesymbol;
cout << "List of operations:
";
cout << " 1. Addition
";
cout << " 2. Subtraction
";
cout << " 3. Multiplication
";
cout << " 4. Division
";
cout << "Enter the number on the left to pick an operation: ";
cin >> choice;
cout << "
Enter number 1: ";
cin >> num1;
cout << "
Enter 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 << " [Main Menu]
";
cout << " 1. Calculator
";
cout << " 2. [unavailable]
";
cout << " 3. [unavailable]
";
cout << "
Enter the number of your selection: ";
cin >> select;
if(!(cin >> select))
{
cout << "Input must be an integer.
";
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 << "
[ITEM UNAVAILABLE]
";
while (true)
{
cout << "
Enter 'menu' to return to main menu: ";
cin >> react;
if (react == "menu")
{
cout << endl;
return;
break;
}
else
cout << "
Invalid response.
";
}
}
void calculator()
{
int choice;
double num1;
double num2;
double answer;
string choicesymbol;
cout << "List of operations:
";
cout << " 1. Addition
";
cout << " 2. Subtraction
";
cout << " 3. Multiplication
";
cout << " 4. Division
";
cout << "Enter the number on the left to pick an operation: ";
cin >> choice;
cout << "
Enter number 1: ";
cin >> num1;
cout << "
Enter 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;
}
推荐答案
总的来说,我是编程的新手,但是使用您的代码和查找内容让我找到了某种解决方案.cin.clear
只清除输入的错误日志,相信还是保留了字母的值.
I am a newbie to programming in general, but playing with your code and looking up stuff made me find some sort of solution.
The cin.clear
only clears the error log of the input, and I believe that it still retains the value of the letter.
你应该在后面添加一个 cin.ignore(#,'
')
(# 是一个非常非常大的数字)让它避开该行并直接跳过它.
What you should add right after is a cin.ignore(#,'
')
(# being a very, very large number) to have it avoid the line and skip right through it.
在另一个中找到了解决方案 问题解释了两个cin命令的使用.
Found the solution in another question that explains the use of both cin commands.
这篇关于如果我输入一个字母,我的程序怎么会卡在它的 while 循环中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如果我输入一个字母,我的程序怎么会卡在它的 while 循环中?
基础教程推荐
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 从 std::cin 读取密码 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 为什么语句不能出现在命名空间范围内? 2021-01-01