【问题标题】:How can I cause a while statement that takes integer input to respond in certain ways to non-integer input?如何使采用整数输入的 while 语句以某些方式响应非整数输入?
【发布时间】:2023-03-17 23:59:01
【问题描述】:

我正在尝试在一个简单的测试程序中创建一个 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";

}

【问题讨论】:

  • 1.您的程序中存在不止一个问题。例如,如果您不输入整数,while 条件将失败。 2. 可以读一个字。如果字符为“a”,则中断循环,否则将其放回流中并尝试读取整数。请参阅putbackungetpeek
  • 感谢zdf的回复。几个后续问题: 1. 因此,如果我在 while 循环中输入一个非整数,循环是否总是会立即中断而没有检查有关“a”的代码? 2. 解决方案可能是创建一个无限 for 循环并将 while (cin >> number) 语句转换为 if (cin >> number) 语句?我编辑了我的原始问题以显示这个可能的答案。 3. 请注意,我不确定读取字符是否是最佳解决方案,因为我还希望程序能够处理错误的字符串条目来代替整数。
  • 1.是的,因为cin &gt;&gt; number(数字是 int 类型),while 正在检查 int 的真实性。 2. 无限循环从来都不是最好的情况,因为如果我们无法处理它必须中断的条件,那么它就会继续下去。
  • while 语句在循环中执行语句。他们不“接受整数输入”或“响应......对非整数输入”。这只是他们控制的语句的保留,您必须编写while 声明与它无关。
  • 感谢您的澄清。

标签: c++ while-loop int


【解决方案1】:

详见内联 cmets:

#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";
}

【讨论】:

  • 感谢您注意到 while (cin) 可以工作。通过遵循 while (cin) 和 if (cin >> number),我想我已经找到了适合我的解决方案。但是,您将字符转换为整数很有趣。
【解决方案2】:

这是一个将 Phillip Siedler 的 Bumpy Road to Code 博客 (https://bumpyroadtocode.com/2017/07/11/sum-of-integer-range/) 中的程序与 Lakshmi 对 while (cin). 的使用相结合的解决方案

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

我对为什么 cin.clear() 后跟 cin &gt;&gt; test 有效感到有些困惑。如果您清除了cin,那么cin 中的cin &gt;&gt; test 行有什么可以接听的吗?也许我不完全理解 cin.clear() 的作用。

 #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";


}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-19
    • 2016-02-07
    • 1970-01-01
    • 2014-07-19
    • 1970-01-01
    • 1970-01-01
    • 2020-06-21
    相关资源
    最近更新 更多