'How can I keep track, if the last user input was a new line?

        while(std::getline(std::cin, input)){
            buffer += input;
            buffer += '\n';
        }

I want to know if the last user input was a new line, and if not remove the last new line in the buffer string. How can I do that? I want to allow empty lines. Example:

aaa aaa

aaa aaa aaa Ctrl+D       <----- here should the last newline added to buffer removed
aaa aaa
aaa

aaa aaa

Ctrl+D         <------- here it shouldn't 


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source