5

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.

5 Answers 5

10

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') } 
Sign up to request clarification or add additional context in comments.

1 Comment

+1 for best practices answer. mine is certainly the duct-tape answer
5

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; } 

3 Comments

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.
@tunetosuraj. Because operator>> when applied to a string name will read up to the first white space character. In 'Tim Hoolihan' this is the space between names. When you then apply the cin.get() there is still a bunch of text on the input to read from and thus it does not wait. Interactive user input is line based (as the buffer is flushed when you hit <enter>) so when dealing with manual user input you should read a line at a time.
@LokiAstari Ah! sorry I missed char name. OP should fix it to string name and place #include<string> header file.
4

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.

Comments

2

cin ignores the carriage return, the value before '\n' is stored in the variable and '\n' remains in the input stream. When cin.get() is called, it takes the value already in the input stream (which is '\n') and and thus it gets skipped.

To avoid this situation Tim's answer is perfect!

cin >> name; cin.get(); // get return here 

Alternatively you can also do

(cin >> name).get(); // get return as soon as cin is completed. 

UPDATE : As pointed out by Loki Astari, When using cin>> operator to the array name, it will read up to the first white space character. In 'Tim Hoolihan' there is a space between the names. Thus name array will store {'T','i','m','\n'} as the value, '\n' marking an end to the string. One should avoid using char array for strings, instead use the class string from #include<string> header file. A string can contain white space character in between, whereas an array can't.

#include<iostream> #include<string> int main() { string name; // string is a class, name is the object cout << "Please enter you name..." << endl; (cin >> name).get(); cout << "Hello, " << name << "!" << endl; // String using the "power" of an array cout << "First two characters of your name are " << name[0] << name[1] << endl; cout << "Please press [ENTER] to continue..."; cin.get(); return 0; } 

Comments

1

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.

1 Comment

We are ignoring the first char after reading the name. Which happens to be the newline.