15
getline() function with companion ignore() INPUTTING AN ENTIRE LINE OF DATA, SPACES AND ALL

getline() functionkwjoiner/cs215/notes/getline.pdf · How the operator works cin variable; 1. If the cin buffer is empty, execution suspends and waits for the user to enter data and

  • Upload
    others

  • View
    6

  • Download
    0

Embed Size (px)

Citation preview

Page 1: getline() functionkwjoiner/cs215/notes/getline.pdf · How the operator works cin variable; 1. If the cin buffer is empty, execution suspends and waits for the user to enter data and

getline()

functionwith companion ignore()

INPUTTING AN ENTIRE LINE OF DATA, SPACES AND ALL

Page 2: getline() functionkwjoiner/cs215/notes/getline.pdf · How the operator works cin variable; 1. If the cin buffer is empty, execution suspends and waits for the user to enter data and

Example 1: Input a string with

Page 3: getline() functionkwjoiner/cs215/notes/getline.pdf · How the operator works cin variable; 1. If the cin buffer is empty, execution suspends and waits for the user to enter data and

Example 2: Input a string with

Page 4: getline() functionkwjoiner/cs215/notes/getline.pdf · How the operator works cin variable; 1. If the cin buffer is empty, execution suspends and waits for the user to enter data and

Example 3: Simple Fix

Page 5: getline() functionkwjoiner/cs215/notes/getline.pdf · How the operator works cin variable; 1. If the cin buffer is empty, execution suspends and waits for the user to enter data and

Would you like to know more?

Page 6: getline() functionkwjoiner/cs215/notes/getline.pdf · How the operator works cin variable; 1. If the cin buffer is empty, execution suspends and waits for the user to enter data and

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.

Page 7: getline() functionkwjoiner/cs215/notes/getline.pdf · How the operator works cin variable; 1. If the cin buffer is empty, execution suspends and waits for the user to enter data and

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'

Page 8: getline() functionkwjoiner/cs215/notes/getline.pdf · How the operator works cin variable; 1. If the cin buffer is empty, execution suspends and waits for the user to enter data and

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");

Page 9: getline() functionkwjoiner/cs215/notes/getline.pdf · How the operator works cin variable; 1. If the cin buffer is empty, execution suspends and waits for the user to enter data and

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.

Page 10: getline() functionkwjoiner/cs215/notes/getline.pdf · How the operator works cin variable; 1. If the cin buffer is empty, execution suspends and waits for the user to enter data and

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.

Page 11: getline() functionkwjoiner/cs215/notes/getline.pdf · How the operator works cin variable; 1. If the cin buffer is empty, execution suspends and waits for the user to enter data and

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

Page 12: getline() functionkwjoiner/cs215/notes/getline.pdf · How the operator works cin variable; 1. If the cin buffer is empty, execution suspends and waits for the user to enter data and

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.

Page 13: getline() functionkwjoiner/cs215/notes/getline.pdf · How the operator works cin variable; 1. If the cin buffer is empty, execution suspends and waits for the user to enter data and

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.

Page 14: getline() functionkwjoiner/cs215/notes/getline.pdf · How the operator works cin variable; 1. If the cin buffer is empty, execution suspends and waits for the user to enter data and

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;

Page 15: getline() functionkwjoiner/cs215/notes/getline.pdf · How the operator works cin variable; 1. If the cin buffer is empty, execution suspends and waits for the user to enter data and

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;