how-to-read-until-eof-from-cin-in-c++

#include <iostream>
#include <string>

int main() {
    std::string line;
    while (std::getline(std::cin, line)) {
        // Process 'line' here
    }
    return 0;
}
  1. #include <iostream>: Includes the input/output stream library for handling input and output operations.
  2. #include <string>: Includes the string library to work with string data types.
  3. int main() { ... }: Entry point of the program.
  4. std::string line;: Declares a string variable named line to store input.
  5. while (std::getline(std::cin, line)) { ... }: Uses a while loop to read input from std::cin (standard input) using std::getline. It reads a line of text until the end-of-file (EOF) is encountered.
  6. // Process 'line' here: Placeholder comment indicating where you can process each line of input within the loop.
  7. return 0;: Indicates successful completion of the main function.