How to exit program on press of return key?
<p>I am writing a program in C++ which is supposed to take an input string from user, print it and ask the user to type in another string again and print it again until the user presses the return key without typing any string. In that case the program should exit.</p>

<p>I came up with the following, but it doesn't work as desired. Any ideas?</p>

<pre><code>int main(){

    string surname;
    int c;
    while (true) {
        surname = "";
        cout &lt;&lt; "Enter surname (RETURN to quit): ";
        c = cin.get();
        if (c == '\n') {
            break;
        }
        cin &gt;&gt; surname;
        cout &lt;&lt; surname &lt;&lt; endl;
    }
    return 0;   
}
</code></pre>
