我正在尝试在一个简单的测试程序中创建一个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";
}
请参阅嵌入式注释以获取详细信息:
#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";
}
[这是将Phillip Siedler的Bumpy Road到Code博客(https://bumpyroadtocode.com/2017/07/11/sum-of-integer-range/)的程序与Lakshmi对while (cin).
的使用结合起来的解决方案
这似乎允许程序在输入“ a”的情况下中断,并输入无效的输入消息,并且如果输入的不是整数或“ a”,则允许用户再次尝试。
[为什么在cin.clear()
后跟cin >> test
上起作用,我有些困惑。如果清除cin
,cin
中有什么东西可以接cin >> 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";
}
while (cin >> number)
表示“当您能够读取整数时循环”。输入其他内容后,循环将结束;例如'a'
。
if(!cin)
是没有意义的,因为除非读取了有效的整数,否则无法达到此点。
[test = to_string( number )
不会将整数转换为"a"
。
解决方案(test it):
#include <iostream>
#include <limits>
int main()
{
using namespace std;
char c;
int sum = 0;
while ( cin >> c ) // read first character in line; if successful, continue with loop's body
{
if ( c == 'a' ) // end of input; you can move this in while condition: while ( cin >> c && c != 'a' )
break;
int n;
if (cin.unget() >> n) // putback in the stream the last read character and try to read an integer
{
sum += n;
continue;
}
cout << "Invalid input. Please try again.\n";
cin.clear(); // failed to read an integer: reset the error flags
cin.ignore(numeric_limits<streamsize>::max(), '\n'); // eat to the end of new-line
}
cout << "The total sum is " << sum << ".\n";
}