reading from stdin in c++

前端 未结 1 936
陌清茗
陌清茗 2020-12-07 23:58

I am trying to read from stdin using C++, using this code

#include 
using namespace std;

int main() {
    while(cin) {
        getline(cin,          


        
相关标签:
1条回答
  • 2020-12-08 00:58

    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;
    }
    
    0 讨论(0)
提交回复
热议问题