•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C and C++ Tutorials section within the Software Development category of DaniWeb, a massive community of 374,033 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,890 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C and C++ Tutorials advertiser:
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:
Now let's say you want the user to enter a string that contains a space in it. Since using
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:
Now you run it and the program totally skips the getline() statement!
That's rather odd. Let's try using only getline for our input (which means the number will be put into a string instead):
The program's output being:
The problem went away.
You will probably suspect something is up with
To fix this the best approach, although not that easy for newbies, is to avoid the use of
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
This outputs:
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
If you were to implement this in your first example, it would work like this:
And the output would be:
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
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:
cplusplus Syntax (Toggle Plain Text)
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:
cplusplus Syntax (Toggle Plain Text)
#include <iostream> 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 5That's rather odd. Let's try using only getline for our input (which means the number will be put into a string instead):
cplusplus Syntax (Toggle Plain Text)
#include <iostream> 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
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).
cingrabs 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: cplusplus Syntax (Toggle Plain Text)
#include <iostream> #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 best method is to use cin.ignore(<some large number>, '\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 numeric_limits<streamsize>::max() If you were to implement this in your first example, it would work like this:
cplusplus Syntax (Toggle Plain Text)
#include <iostream> 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! •
•
•
•
•
•
•
•
DaniWeb Marketplace (Sponsored Links)
- User Input: Strings and Numbers [C] (C and C++ Tutorials)
- error checking of user input (C++)
- user input into a string (C++)
- Need Help With Error Checking User Input (C)
Other C and C++ Tutorials
- Next Tutorial: User Input: Strings and Numbers [C]