[英]Second cin is either skipped or does not work properly
I have a couple of problems, that I think are closely connected, but I couldn't get them fixed following what I previously found on the website. 我有几个问题,我认为这些问题是密切相关的,但我无法按照我之前在网站上找到的内容修复它们。
My problems are related to the double use of cin
in my main function. 我的问题与我的主要功能中的
cin
的双重使用有关。 I need to read numbers from keyboard in order to either build small vectors or store single coefficients. 我需要从键盘读取数字,以便构建小向量或存储单个系数。 I cannot know in advance the length of the vectors that I am going to build.
我事先无法知道我要构建的向量的长度。
Here are the lines involved: 以下是涉及的内容:
#include <vector>
#include <iostream>
#include <limits>
int main() {
...
double a=0;
std::vector<double> coefficients;
while (std::cin>>a) {
coefficients.push_back(a);
}
...
std::vector<double> interval;
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max());
while(std::cin>>a) {
interval.push_back(a);
}
std::cout<<interval.size()<<std::endl;
std::cout<<*interval.cbegin()<<" "<<*(interval.cend()-1)<<std::endl;
...
}
I am using both macOS with g++ 6.3.0 and Linux with g++ 5.3.0. 我正在使用macOS与g ++ 6.3.0和Linux与g ++ 5.3.0。 The flags I send to the compiler are
-Wall -std=c++14 -o
. 我发送给编译器的标志是
-Wall -std=c++14 -o
。
On the macOS machine the second cin is completely skipped, while on the Linux one the second reading process does not behave like it is expected to. 在macOS机器上完全跳过第二个cin,而在Linux上,第二个读取过程的行为与预期的不同。 I mean that if i give
-1 1
at the second cin
, the printed vector size is 0 and, obviously, the program stops because of a segmentation fault. 我的意思是如果我在第二个
cin
给出-1 1
,则打印的矢量大小为0,显然,程序因分段错误而停止。
At each cin
I enter the requested numbers in a single line, like 1 0 0 1
, then press enter and then ctrl+D. 在每个
cin
我输入一行中请求的数字,如1 0 0 1
,然后按Enter键然后按Ctrl + D.
Thanks in advance to all! 在此先感谢所有人! :)
:)
您需要将换行符'\\n'
作为第二个参数添加到cin.ignore()
以便它在输入按下时cin.ignore()
忽略
Your call of std::cin.ignore(...)
set the fail bit of the stream. 您对
std::cin.ignore(...)
调用设置了流的失败位。 This makes it impossible to enter the loop. 这使得无法进入循环。 You need to move the
std::cin.clear()
call right before the loop, in order to make it run. 您需要在循环之前移动
std::cin.clear()
调用,以使其运行。 Also you have an out-of-bound read when there are no data in the second container. 当第二个容器中没有数据时,您还有一个越界读取。
#include <vector>
#include <iostream>
#include <limits>
#include <string>
int main() {
double a=0;
std::vector<double> coefficients;
while (std::cin>>a) {
coefficients.push_back(a);
}
std::cout << coefficients.size() << '\n';
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), 'X');
std::cin.clear();
char c;
std::cin>>c;
if(c != 'X')
{
std::cerr << "Invalid separator\n";
return 1;
}
std::vector<double> interval;
while(std::cin >> a) {
interval.push_back(a);
}
std::cout<< interval.size()<<std::endl;
if(interval.size())
std::cout<<*interval.cbegin()<<" "<<*(interval.cend()-1)<<std::endl;
return 0;
}
With the following data file, 使用以下数据文件,
$ cat data.txt
12 23
42
X
1 2
3 4 5
this output is generated: 生成此输出:
$ ./a.out < data
3
5
1 5
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.