Presentation is loading. Please wait.

Presentation is loading. Please wait.

getline() function with companion ignore()

Similar presentations


Presentation on theme: "getline() function with companion ignore()"— Presentation transcript:

1 getline() function with companion ignore()
Inputting an entire line of data, spaces and all

2 Example 1: Input a string with cin >>

3 Example 2: Input a string with cin >>

4 Example 3: Simple Fix

5 Would you like to know more?
Keep Reading!

6 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.

7 Whitespace is a sequence of one or more characters consisting of:
What is a Whitespace? Whitespace is a sequence of one or more characters consisting of: (additional characters depending on platform) Name C++ Literal Space ' ' Vertical Tab '\v' Tab (Horizontal) '\t' Carriage Return '\r' Newline '\n' Form Feed '\f'

8 What happened in Example 2?
cin >> name; - 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 (name = "Sam");

9 What happened in Example 2?
cout << "Enter your age: "; - Enter your age is printed; no '\n' means the cursor stays on this line. cin >> age; - 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 age is not changed.

10 What happened in Example 2?
cout << name << " is " << age << "years old!\n"; - So name="Sam" and age= 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 years old! is printed.

11 Potential Problem: spaces in input
string name; int age; cout << "Enter your name: "; cin >> 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]

12 Potential Problem: spaces in input
string name; int age; cout << "Enter your name: "; cin >> 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 name; [space]Smith[enter] remains in the buffer.

13 Potential Problem: spaces in input
string name; int age; cout << "Enter your name: "; cin >> name; cout << "Enter your age: "; cin >> age; - When the second cin 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.

14 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"; Output: cout << name[0] << endl; cout << name[1] << endl; cout << name[2] << endl; cout << name[3] << endl; cout << name[4] << endl;

15 Converting to Upper Case
Use the toupper function to convert a char to upper case (does not work on string!) string name = "Hello"; Output: char x = name[0]; // 'H' char y = name[1]; // 'e' x = toupper(x); // 'H' y = toupper(y); // 'E' cout << x << endl; cout << y << endl;


Download ppt "getline() function with companion ignore()"

Similar presentations


Ads by Google