Upload
others
View
6
Download
0
Embed Size (px)
Citation preview
getline()
functionwith companion ignore()
INPUTTING AN ENTIRE LINE OF DATA, SPACES AND ALL
Example 1: Input a string with
Example 2: Input a string with
Example 3: Simple Fix
Would you like to know more?
How the operator works
cin variable;1. If the cin buffer is empty, execution suspends and waits for the user to enter data and press [ENTER]
2. Any leading whitespace is extracted and discarded.
3. Characters are extracted until another whitespace is found; the whitespace is not extracted.
4. The extracted characters are converted to the data type of the variable (may succeed or fail).
5. If the conversion is successful, the variable is set equal
to the converted value.
What is a Whitespace?
is a sequence of one or more
characters consisting of:
(additional characters depending on platform)
Name C++ Literal Name C++ Literal
Space ' ' Vertical Tab '\v'
Tab (Horizontal) '\t' Carriage Return '\r'
Newline '\n' Form Feed '\f'
What happened in Example 2?
- the buffer was empty, so the program suspended awaiting user input.
- the user typed Sam_Smith[←] (_ represents BLANK here)
- the program resumed.
- Sam was extracted from the buffer, leaving _Smith[←] in the buffer
- Sam was assigned to the variable (name = "Sam");
What happened in Example 2?
- Enter your age is printed; no '\n' means the cursor stays on this line.
- the buffer is NOT empty, so the program does NOT halt to allow more input.
- the blank is extracted from the buffer and discarded.
- Smith is extracted, leaving only the [←] in the buffer
- "Smith" can not be converted to an integer; so it is discarded and the value
of is not changed.
What happened in Example 2?
- So name="Sam" and
age=-81234234 because it was never set equal to anything.
(the cin >> age should have set it equal to the user input, but could not
because the user entered string instead of integer).
- Sam is -81234234 years old! is printed.
Potential Problem: spaces in input
string name;
int age;
cout << "Enter your name: ";
cout << "Enter your age: ";
cin >> age;
- The program halts and waits for the user to press ENTER.
- Assume the user enters
Potential Problem: spaces in input
string name;
int age;
cout << "Enter your name: ";
cout << "Enter your age: ";
cin >> age;
- The program halts and waits for the user to press ENTER.
- Assume the user enters Sam Smith[enter]
- The characters are placed in the keyboard buffer, and the cin resumes.
- The chars Sam are extracted, but NO MORE, since the next char is SPACE.
- Sam is placed in the variable ; [space]Smith[enter] remains in the
buffer.
Potential Problem: spaces in input
string name;
int age;
cout << "Enter your name: ";
cin >> name;
cout << "Enter your age: ";
- When the second executes, there is already data in the buffer, so:
- The program does not halt.
- The [SPACE] is extracted and discarded.
- Smith is extracted. Extraction halts because the next char is [ENTER]
- Since Smith is a string, it can not be stored in integer age, so the program
fails at this point.
String as a Sequence of Char
Remember that a string is actually a sequence (list) of chars.
Individual chars in a string can be accessed using the operator.
The first char is [0], the second [1], etc.
string name = "Hello";
cout << name[0] << endl;
cout << name[1] << endl;
cout << name[2] << endl;
cout << name[3] << endl;
cout << name[4] << endl;
Converting to Upper Case
Use the toupper function to convert a char to upper case
(does not work on string!)
string name = "Hello";
char x = name[0]; // 'H'
char y = name[1]; // 'e'
x = toupper(x); // 'H'
y = toupper(y); // 'E'
cout << x << endl;
cout << y << endl;