程序在终止循环后不接收输入
program not taking an input after terminating a loop
我正在尝试制作一个计算n个整数之和的程序。但是当我尝试运行该程序时,它不需要输入val2
.
int main()
{
int val1 = 0;
cout << "Enter the integers (enter '|' to terminate input): ";
vector<int> elements = {};
while (cin >> val1) {
elements.push_back(val1);
}
cout << "Enter the number to find the sum of integers: ";
int val2;
cin >> val2;
int sum = 0;
for (int i = 0; i < val2; ++i) {
sum = sum + elements[i];
cout << "The sum of first " << val2 << " elements is " << sum << endl;
}
system("pause");
return 0;
}
包含标题
#include <limits>
在此代码片段之前
cout << "Enter the number to find the sum of integers: ";
写
std::cin.clear();
std::cin.ignore( std::numeric_limits<std::streamsize>::max(), 'n' );
问题是输入符号'|'
输入流处于错误状态,您需要跳过输入缓冲区中的非数字符号。
相关文章:
- 在输入句子时终止 std::out_of_range
- 在这个无限循环中,当输入 0 时终止,当输入 int 范围之外的任何内容时,程序行为不正常
- 代码在执行之前终止,并且没有从用户那里获取输入
- 使用终止程序的指针在数组中输入字符串
- 程序在终止循环后不接收输入
- 如何用字符终止整数输入循环?
- 当我输入非整数值时,我的二次方程代码终止,如何让它正确循环?
- 程序终止,状态为 -1073741510 在 for 循环的中间,将输入分配给二维数组
- 程序终止,而不是继续整数输入验证循环
- 如何在C++中终止具有键盘输入的侦听 TCP 服务器
- 如何在C++中使用 if 语句终止向量输入
- (C++) 学习向量 - 编码/输入流终止/比较向量的问题
- 当用户输入 -1 时,它应该终止
- 如何将键盘输入终止
- C++循环未终止时的输入验证
- C++输入超过 11 个整数时程序终止
- cin函数是否添加了在输入结束时终止的null
- 是否有一种方法可以在输入负数时平均不包括负数,这是您终止程序的方式
- 如何通过输入终止数据输入
- 终止将输入读取到int中的内容