detect end of user input cpp

You can use the following C++ code to detect the end of user input:

#include <iostream>

int main() {
    std::string input;
    while (std::cin >> input) {
        // Process the input here
    }
    if (std::cin.eof()) {
        // End of input reached
    } else {
        // Input ended due to some error
    }
    return 0;
}

Explanation:

  1. Include the iostream header file for input and output operations.

  2. Define the main function as the entry point of the program.

  3. Declare a string variable called input to store the user input.

  4. Start a while loop that reads input from the standard input (keyboard) using std::cin and checks if the input operation was successful.

  5. Inside the while loop, process the input as needed.

  6. If the input operation reaches the end of the input (EOF), the loop will exit.

  7. Use std::cin.eof() to check if the end of the input was reached due to reaching the end of the input stream.

  8. If std::cin.eof() returns true, it means the end of input was reached.

  9. If std::cin.eof() returns false, it means the input ended due to some error.

  10. Return 0 to indicate successful program execution.

This code continuously reads input from the user until the end of the input is reached, and then it checks for the end of input condition using std::cin.eof().