当前位置: 首页 > 知识库问答 >
问题:

如何使接受整数输入的while语句以特定方式响应非整数输入?

齐晟
2023-03-14

我试图在一个简单的测试程序中创建一个while循环。此程序用于对用户输入的所有整数求和,并在用户按“a”后显示此和并退出循环。如果用户输入的不是整数或“a”,则程序用于响应“无效输入。请重试”,并允许用户输入其他数字。

我的程序成功地对整数求和,但如果输入了“a”以外的非整数,循环将结束,而不允许用户输入其他值。以if(!cin)开头的代码块似乎没有任何作用。

我认为问题出在(! cin)而不是test=to_string(number)。是否有替代方法来检测允许此程序成功的非整数?

这是程序。一如既往地感谢您的帮助!

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

int main()
{
int sum = 0;
int number;
string test;
cout << "Enter a series of integers; when you are done entering these integers, press a to display the sum of all integers.\n";
while (cin >> number)
{if (!cin)
    {
    test = to_string(number);
    if (test == "a")
        {break;}
    else 
        {cout << "Invalid input. Please try again.\n";
        continue;}
}
else 
{sum += number;}
}

cout << "The total sum is " << sum << ".\n";

}

共有2个答案

堵毅然
2023-03-14

这里有一个解决方案,它结合了PhillipSiedler的坎坷之路代码博客中的一个程序(https://bumpyroadtocode.com/2017/07/11/sum-of-integer-range/)Lakshmi使用了while(cin)

这似乎允许程序在输入“a”时中断,并在输入整数或“a”以外的内容时输入无效的输入消息并允许用户重试。

我有点搞不懂为什么。clear()后接cin

 #include <iostream>
using namespace std;
#include <string>
//Solution borrows significantly from Philipp Siedler's solution for Chapter 5 Exercise 8 of Programming Practice and Principles: https://bumpyroadtocode.com/category/chapter-05-principles-and-practice-using-c/ 


int main()
{
int sum = 0;
int number;
string test;
cout << "Enter a series of integers; when you are done entering these integers, press a to display the sum of all integers.\n";

while (cin)
{
if (cin >> number)
{sum += number;}
else 
 {cin.clear();
    cin >> test;
    if (test == "a")
        {break;}
    else 
        {
        cout << "Invalid input. Please try again.\n";
        continue;}
  }

}

cout << "The total sum is " << sum << ".\n";


}
翟青青
2023-03-14

有关详细信息,请参阅内联注释:

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

int main()
{
int sum = 0;
char ch; // Since we can't know whether user inputs an int or char
cout << "Enter a series of integers; when you are done entering these integers, press a to display the sum of all integers.\n";
while (cin) // Loop until user inputs
{
    cin >> ch;

if (isalpha(ch)) // Check if input is type char and handle as needed
{   
    if (ch == 'a')
        {
            break;
        }
    else 
        {
            cout << "Invalid input. Please try again.\n";
            std::cin.clear(); // Delete the current stream
            std::cin.ignore(32767,'\n'); // Revert back to previous input
            continue;
        }
}
else 
{
    sum += (ch - '0') % 48; // Since we are converting the char to int, handle it properly

}
}
cout << "The total sum is " << sum << ".\n";
}
 类似资料:
  • 我有密码 但是当我在文本字段中输入字符时,它们会保持打印状态,直到我切换到另一个文本字段。我需要完全禁止字符输入,我的意思是当字符输入时,我需要删除字符,或者如果可能的话,以某种方式使字符甚至不出现以供听众处理,当然也不出现在文本字段中。我的意思是只有数字字符必须出现。

  • 问题:安东尼娅和大卫在玩游戏。每位选手以100分开始。游戏使用标准的六面骰子,分回合进行。在一个回合中,每个玩家掷一个骰子。下滚的玩家失去了在较高的骰子上显示的点数。如果两个玩家掷相同的号码,任何一个玩家都不会丢分。写一个程序来确定最后的分数。 以下是我目前掌握的信息: 我知道我只具体要求了一件事,但有人能完成这个挑战并解释最好的方法吗?

  • 这是我发现的一个编码挑战网站的问题。这是我的代码: 我需要做什么或改变什么来获得想要的输出。 }

  • 问题内容: 因此,几天前我开始学习Java并遇到了一个问题。对于下一个表达式: 不被允许。但是,在中,我们可以使用类似: 由于不允许将整数隐式地分配给字符串,因此上述表达式为何起作用?任何人都可以给出详细的解释吗?我也想知道我们什么时候可以使用这种隐式事物,什么时候不能使用。 问题答案: PrintStream 有很多重载方法:

  • 我试图获取代码,以防止用户输入中包含数字。 基本上,我希望代码如下所示: 请求输入 关键是(为什么这不是一个重复的问题):我不能使用循环或其他我们还没有学会的语句。到目前为止,我们所学的唯一真正的语句是if/else/else if语句。这意味着我不能像一些答案所建议的那样使用for循环。虽然它们是很好的答案,而且很有效,但我会因为使用它们而失分。 我已经有了这个,但我不知道如何测试输入是否只包含

  • 问题内容: 我有这张桌子: 我使用这样的查询,但出现错误: 我想显示这样的表: 如何实现呢? 问题答案: 我想在您的查询的问题是, 是的,你是想选择一个空的()。 您必须解决方法: 更改为(2010,2012 ..将被视为字符串,我不知道是否可以) 放: