Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
720 views
in Technique[技术] by (71.8m points)

reading from stdin in c++

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 aint main()a:
capture.cpp:6: error: ainput_linea was not declared in this scope

Any ideas whats missing?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...