Table of Contents
Overview:
- Terms to know: input stream operator,
- New code:
- Get input (ints, floats, strings, etc.):
cin >> VARNAME; - Get string line input:
getline( cin, STRINGNAME ); - Flush the input buffer:
cin.ignore();
- Get input (ints, floats, strings, etc.):
- Common errors:
- If you're using
cin >> STRINGNAME;to get input for a string, it will only get ONE WORD and stop at the first space. If you want to allow spaces, usegetlineinstead. getlinecan only be used for strings, not ints/floats/etc.- If you're switching between
cin >> ...andgetline( cin, ... ), you need to putcin.ignore()after thecin >>and before thegetline.- If your program is skipping an input, it means you're missing
a
cin.ignore(). - If your program is getting input but truncating the first
letter of what you typed, you have too many
cin.ignore()calls.
- If your program is skipping an input, it means you're missing
a
- If you're using
- The "
getline" function is used for… - When do you need to have
cin.ignore();in your code?
1. Using cin >> for variables
When we want the user to enter a value for a variable using the
keyboard, we use the cin command (pronounced as "c-in" or
"character-in").
Example: Reading input into one variable
cin >> VARIABLENAME;
You can also chain cin statements together to read multiple values for multiple variables:
Example: Inputting data into multiple variables at once (separated by spaces)
cin >> VARIABLENAME1 >> VARIABLENAME2 >> ETC;
1.1. Strings and cin >>
When using cin >> with a string variable, keep in mind that it will
only read until the first whitespace character, meaning it can't
capture spaces or tabs. For example:
Example: Getting one word (ends at spaces) with the input stream operator
string name; cin >> name;
If you enter "Rachel Singh", name will contain "Rachel". To capture spaces, you need to use a different function.
Review 1.
coutis pronouned ○ kowt / ○ see-out / ○ countcinis pronounced ○ sin / ○ see-in / ○ kin
Review 2. Given an integer variable declared named totalCookies, how would you get input from the keyboard and store it in this variable?
2. Using getline(cin, var); for Strings
You can use the getline function to capture an entire line of text as a string. This is useful when you want to capture spaces and multiple words. For example:
Example: Getting a full line of text (including spaces) with the getline function
string name; getline(cin, name);
Review 3. A string variable named currentCourse has been declared. How would we read keyboard input (including spaces) and store it in this variable?
cout << "Please enter course: "; // Then what?
3. Mixing cin >> var; and getline(cin, var);
If you mix cin >> var; and getline(cin, var);, you might encounter
issues with the input buffer. To avoid this, use cin.ignore(); before
getline(cin, var); if you used cin >> var; before it.
int number; string text; cin >> number; cin.ignore(); getline( cin, text );
Review 3. Which of the following scenarios requires the cin.ignore() statement?
A.
cin >> var1;
cin.ignore();
cin >> var2;
B.
cin >> var1; cin.ignore(); getline( cin, var2 );
C.
getline( cin, var2 ); cin.ignore(); cin >> var1;
D.
getline( cin, var1 ); cin.ignore(); getline( cin, var2 );