tags:

views:

83

answers:

4

I have the following code:

int main()
{
// Variables
char name;

// Take the users name as input
cout << "Please enter you name..." << endl;
cin >> name;


// Write "Hello, world!" and await user response
cout << "Hello, " << name << "!" << endl;
cout << "Please press [ENTER] to continue...";
cin.get();

return 0;

}

After the user hits return to enter their name, that carriage return is carried forward to the end of the code where it is immediately applied as input to cin.get(), thus ending the program prematurely. What can I place on the line immediately following

cin >> name;

to stop this from happening? I know that it's possible, as I've done it before, but can't remember what it is or where I can find it. Thanks a lot in advance.

+2  A: 

simplest answer:

int main()
{
// Variables
char name;

// Take the users name as input
cout << "Please enter you name..." << endl;
cin >> name;
cin.get(); // get return here


// Write "Hello, world!" and await user response
cout << "Hello, " << name << "!" << endl;
cout << "Please press [ENTER] to continue...";
cin.get();

return 0;
}
Tim Hoolihan
I agree, this is how they showed us in the first year of university. Its a simple solution to catch that extra character :) Im sure there is an alternative to this though.
JonWillis
That will not work if the user entered "Tim Hoolihan<enter>"
Martin York
A: 

First of all name is a string and you are reading it in a char.

char name;

should be

string name;

Also add #include<string>

Now to clear the newline from the buffer you can do:

std::cin.ignore(1);

after reading the name.

codaddict
std::cin.clear does *not* remove any pending input
Bart van Ingen Schenau
@Bart: Thanks for pointing. I meant to use `cin.ignore`
codaddict
We are ignoring the first char after reading the name. Which happens to be the newline.
codaddict
... or may-be not.
UncleBens
+3  A: 

You can tell your stream to ignore the next N characters with

cin.ignore(1000, '\n');

This will cause cin to skip up to 1000 characters or until it found (and removed) a newline ('\n') character. Other limits or terminating characters can be specified as you desire.

Bart van Ingen Schenau
+1  A: 

Really you want to use everything on the input upto the newline as the name.
Currently your code only reads the first word.

#include <iostream>
#include <string>

int main()
{
    // Variables
    std::string name;

    // Take the users name as input
    // Read everything upto the newline as the name.
    std::cout << "Please enter you name..." << std::endl;
    std::getline(std::cin, name);

    // Write "Hello, world!" and await user response
    // Ignroe all input until we see a newline.
    std::cout << "Hello, " << name << "!\n";
    std::cout << "Please press [ENTER] to continue..." << std::flush;
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n')
}
Martin York
+1 for best practices answer. mine is certainly the duct-tape answer
Tim Hoolihan