User input/output is a key component in the programs you write, yet most online tutorials just provide a quick-and-dirty method of grabbing data. In other words, it works right up until the point the user does something unexpected.
In this tutorial you will learn how to avoid the common mistakes by doing it properly.
Take this typical method that tutorials use to teach user input:
int number;
cin >> number;
cout << "You typed the number " << number << ".\n";
Now let's say you want the user to enter a string that contains a space in it. Since using cin >> myString
won't work (it will only grab the first word and then exit), you decide you need to find a function that will grab a whole line of input.
After some Googling, you decide that getline() is probably the best way to do it. You add it to your existing code, and this is how your entire program looks:
#include <iostream>
#include <string>
using namespace std;
int main() {
int number;
string name;
cout << "Please enter a number." << endl;
cin >> number;
cout << "Enter your entire name (first and last)." << endl;
getline(cin, name);
cout << "Your full name is " << name
<< ", and the number you entered is " << number << endl;
return 0;
}
Now you run it and the program totally skips the getline() statement!
Please enter a number.
5
Enter your entire name (first and last).
Your full name is , and the number you entered is 5
That's rather odd. Let's try using only getline for our input (which means the number will be put into a string instead):
#include <iostream>
#include <string>
using namespace std;
int main() {
string number;
string name;
cout << "Please enter a number." << endl;
getline(cin, number);
cout << "Enter your entire name (first and last)." << endl;
getline(cin, name);
cout << "Your full name is " << name
<< ", and the number you entered is " << number << endl;
return 0;
}
The program's output being:
Please enter a number.
5
Enter your entire name (first and last).
Joe Programmer
Your full name is Joe Programmer, and the number you entered is 5
The problem went away.
You will probably suspect something is up with cin
and are right. What's actually happening in the first example is:
- You enter something in.
- You hit return.
- All of this is put into the input buffer (including the newline that results from hitting return).
-
cin
grabs whatever it needs (in this case the number), but leaves the newline behind! - Since
getline()
only grabs 1 line, all it gets is the newline left behind bycin
.
To fix this the best approach, although not that easy for newbies, is to avoid the use of cin
until you know what you're doing, and let getline()
handle your user input. It not only solves the newline problem, but it also solves a number of other problems induced by using cin
.
This is relatively easy for people to do until they get to numbers (which usually can't be read into strings because they need to be manipulated). For this, you can use a stringstream
to convert the string back into a number:
#include <iostream>
#include <string>
#include <sstream> // need this for stringstreams!
using namespace std;
int main() {
int number;
string line;
stringstream stream;
cout << "Please enter a number." << endl;
getline(cin, line);
stream << line;
stream >> number;
cout << "The number entered was " << number << ".\n";
return 0;
}
This outputs:
Please enter a number.
4
The number entered was 4.
You can now see how the operation is nearly identical, except that you are taking an extra step by first putting the input a string, and then putting it into a separate stream before popping it back out in the number.
Finally, you actually can mix cin
with getline()
, if you know what you are doing. The easiest method is to use cin.ignore([I]<some large number>[/I], '\n')
to clear the input buffer. You could simply hard code a large number, although it's usually better to use a built-in constant. The Standard Template Library provides such a constant, which is found in the <limits> header: numeric_limits<streamsize>::max()
If you were to implement this in your first example, it would work like this:
#include <iostream>
#include <string>
#include <limits>
using namespace std;
int main() {
int number;
string name;
cout << "Please enter a number." << endl;
cin >> number;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Enter your entire name (first and last)." << endl;
getline(cin, name);
cout << "Your full name is " << name
<< ", and the number you entered is " << number << endl;
return 0;
}
And the output would be:
Please enter a number.
3
Enter your entire name (first and last).
Joe Programmer
Your full name is Joe Programmer, and the number you entered is 3
However, this should only be used when absolutely necessary, as this is just a band-aid for the function. It doesn't really fix some of the other problems that come with using it. So perhaps it is best to stick with getline()
. Don't do the quick-and-dirty method that online tutorials teach you, because it will eventually come back and bite you in the back of the neck. Flushing the input buffer like mentioned previously is only a last resort for using cin
; it's not a good alternative to using getline()
to handle your input. A little bit of trouble now using getline()
will save you a lot of pain later!
Update: Narue has written a nice tutorial showing how to clear the input buffer and all the problems associated with doing so: How do I flush the input stream?