example4_handling_empty_lines.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>

int main() {
    std::string input;
    int lineCount = 0;
    
    std::cout << "Enter text (empty line to finish):\n";
    
    while (true) {
        std::getline(std::cin, input);
        if (input.empty()) {
            break;
        }
        ++lineCount;
        std::cout << "Line " << lineCount << ": " << input << std::endl;
    }
    
    std::cout << "Total non-empty lines: " << lineCount << std::endl;
    return 0;
}
Back to std_getline