第二个 cin 被跳过或不能正常工作
Second cin is either skipped or does not work properly
我有几个问题,我认为它们密切相关,但我无法按照我之前在网站上找到的内容修复它们。
我的问题与在我的主函数中重复使用 cin
有关。我需要从键盘读取数字以构建小向量或存储单个系数。我无法提前知道我要构建的向量的长度。
以下是涉及的行:
#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;
...
}
我同时使用 macOS 和 g++ 6.3.0 以及 Linux 和 g++ 5.3.0。我发送给编译器的标志是 -Wall -std=c++14 -o
.
在 macOS 机器上,第二个 cin 被完全跳过,而在 Linux 机器上,第二个读取过程的行为不像预期的那样。我的意思是,如果我在第二个 cin
处给出 -1 1
,则打印的矢量大小为 0,显然,程序会因为分段错误而停止。
在每个 cin
处,我在一行中输入请求的数字,例如 1 0 0 1
,然后按回车键,然后按 ctrl+D。
在此先感谢大家! :)
您需要添加一个换行符 '\n'
作为 cin.ignore()
的第二个参数,这样它就可以消除输入时的忽略
您对 std::cin.ignore(...)
的调用设置了流的失败位。这样就无法进入循环。您需要在循环之前移动 std::cin.clear()
调用,以便使其成为 运行。当第二个容器中没有数据时,您还可以读取 out-of-bound。
#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;
}
具有以下数据文件,
$ cat data.txt
12 23
42
X
1 2
3 4 5
生成此输出:
$ ./a.out < data
3
5
1 5
我有几个问题,我认为它们密切相关,但我无法按照我之前在网站上找到的内容修复它们。
我的问题与在我的主函数中重复使用 cin
有关。我需要从键盘读取数字以构建小向量或存储单个系数。我无法提前知道我要构建的向量的长度。
以下是涉及的行:
#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;
...
}
我同时使用 macOS 和 g++ 6.3.0 以及 Linux 和 g++ 5.3.0。我发送给编译器的标志是 -Wall -std=c++14 -o
.
在 macOS 机器上,第二个 cin 被完全跳过,而在 Linux 机器上,第二个读取过程的行为不像预期的那样。我的意思是,如果我在第二个 cin
处给出 -1 1
,则打印的矢量大小为 0,显然,程序会因为分段错误而停止。
在每个 cin
处,我在一行中输入请求的数字,例如 1 0 0 1
,然后按回车键,然后按 ctrl+D。
在此先感谢大家! :)
您需要添加一个换行符 '\n'
作为 cin.ignore()
的第二个参数,这样它就可以消除输入时的忽略
您对 std::cin.ignore(...)
的调用设置了流的失败位。这样就无法进入循环。您需要在循环之前移动 std::cin.clear()
调用,以便使其成为 运行。当第二个容器中没有数据时,您还可以读取 out-of-bound。
#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;
}
具有以下数据文件,
$ cat data.txt
12 23
42
X
1 2
3 4 5
生成此输出:
$ ./a.out < data
3
5
1 5