cin.get() forcing user to press enter twice

When the user gives no input, they have to press enter twice before "Done." is printed.

cout << "What do you want the name of your page to be? ";
std::getline(cin, pageTitle);
if (cin.get() == '\n')
pageTitle = "Welcome to Website.";
cout << "Done.\n";

Is there a way to print "Done." after pressing enter once?
Last edited on
When the user gives no input, they have to press enter twice before "Done." is printed.


Whether the user gives input or not, he must press enter twice before "Done." is printed.

Perhaps you were thinking of something like the following?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>

int main()
{
    using std::cout;
    using std::cin;

    std::string pageTitle;
    cout << "What do you want the name of your page to be? ";
    getline(cin, pageTitle);

    if (pageTitle.length() == 0)
        pageTitle = "Welcome to website.";

    std::cout << "Done.\n";
    std::cout << "Title: \"" << pageTitle << "\"\n";
}


perfect, thanks
Topic archived. No new replies allowed.