从c ++中的stdin读取 [英] reading from stdin in c++

查看:200
本文介绍了从c ++中的stdin读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用c ++从stdin读取,使用此代码

  #include< iostream> 
using namespace std;

int main(){
while(cin){
getline(cin,input_line);
cout<< input_line<< endl;
};
return 0;
}

当我编译,我得到这个错误..

  [root @ proxy-001 krisdigitx]#g ++ -o capture -O3 capture.cpp 
capture.cpp: â:
capture.cpp:6:error:âinput_lineâ未在此范围内声明

您尚未定义变量 input_line 任何想法缺少什么?

解决方案< 。



添加:

  string input_line; 

并添加此include。

  #include< string> 

这是完整的示例。我也删除了while循环后的分号,你应该在while中有 getline 可以正确检测流的结束。

  #include  #include< string> 

int main(){
for(std :: string line; std :: getline(std :: cin,line);){
std :: cout< <线<< std :: endl;
}
return 0;
}


i am trying to read from stdin using c++, using this code

#include <iostream>
using namespace std;

int main() {
    while(cin) {
        getline(cin, input_line);
        cout << input_line << endl;
    };
    return 0;
}

when i compile, i get this error..

[root@proxy-001 krisdigitx]# g++ -o capture -O3 capture.cpp
capture.cpp: In function âint main()â:
capture.cpp:6: error: âinput_lineâ was not declared in this scope

any ideas whats missing?

解决方案

You have not defined the variable input_line.

Add this:

string input_line;

And add this include.

#include <string>

Here is the full example. I also removed the semi-colon after the while loop, and you should have getline inside the while to properly detect the end of the stream.

#include <iostream>
#include <string>

int main() {
    for (std::string line; std::getline(std::cin, line);) {
        std::cout << line << std::endl;
    }
    return 0;
}

这篇关于从c ++中的stdin读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆