Thursday, September 11, 2008

User Input: Strings and Numbers [C++]

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:
Help with Code Tags
cplusplus Syntax (Toggle Plain Text)
int number;cin >> number;cout << "You typed the number " << number << ".\n";
int number;
cin >> number;
cout << "You typed the number " <<>> 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:
Help with Code Tags
cplusplus Syntax (Toggle Plain Text)
#include using namespace std; int main() { int number; string name; cout << "Please enter a number." <<>> 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;}
#include
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!
Help with Code Tags
(Toggle Plain Text)
Please enter a number.
5
Enter your entire name (first and last).
Your full name is , and the number you entered is 5
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):
Help with Code Tags
cplusplus Syntax (Toggle Plain Text)
#include 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;}
#include
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:
Help with Code Tags
(Toggle Plain Text)
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
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 5The 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 by cin.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:
Help with Code Tags
cplusplus Syntax (Toggle Plain Text)
#include #include // need this for stringstreams!using namespace std; int main() { int number; string line; stringstream stream; cout << "Please enter a number." <<>> number; cout << "The number entered was " << number << ".\n"; return 0;}
#include
#include // 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:
Help with Code Tags
(Toggle Plain Text)
Please enter a number.
4
The number entered was 4.
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(, '\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::max() If you were to implement this in your first example, it would work like this:
Help with Code Tags
cplusplus Syntax (Toggle Plain Text)
#include using namespace std; int main() { int number; string name; cout << "Please enter a number." <<>> number; cin.ignore(numeric_limits::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;}
#include
using namespace std;
int main() {

int number;
string name;

cout << "Please enter a number." << endl;
cin >> number;
cin.ignore(numeric_limits::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:
Help with Code Tags
(Toggle Plain Text)
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
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 3However, 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!

0 comments: