Question: C++ only! Topics Loops While statement cin.ignore statement Description This assignment has two parts. Part I is a coding assignment as usual. Part II has

C++ only!

Topics

Loops

While statement

cin.ignore statement

Description

This assignment has two parts. Part I is a coding assignment as usual. Part II has 4 true/false questions. Although, the answers to these questions are provided with explanations. You are still to do these questions and submit their answers for practice and learning the material.

If you use both getline and cin for inputting, you should put in a cin.ignore( ) after every cin (but do not do that after a getline).

Part I Coding Assignment

Write a program that will displays a user supplied message a number of times desired by the user. This is much like the last assignment. However, in this exercise, first ask the user to supply the number of times the message is to be displayed and then ask the user to provide the message to be displayed. In other words, reverse the input sequence compared with the last assignment. In other word, in this exercise, first input the count using cin and then input the message using getline.

You will notice that by reversing the sequence, the program does not work correctly. The program asks the user to enter a count and then waits for the user to enter a number and press enter. Then, the program asks the user to enter a message. However, it does not wait for the user to enter the message. It considers the message to be an empty line and continues running.

The solution is to put a cin.ignore( ) instruction right after the cin instruction and before issuing a getline.

Part I Discussion:

cin.ignore

The above problem arises from the way cin and getline input data from the input buffer and treat leading and trailing white space. A white space is considered any number of mixture of consecutive spaces, tabs or newlines. A leading white space is any number of mixture of spaces, tabs, and newline that a user may enter before entering the actual data and a trailing white space is any number of mixture of spaces, tabs, and newlines that a user may enter after entering the actual data. For example, in " 12 ", all the spaces before 12 is a leading white space and all the spaces including after 12 is the trailing white space. Note that translates into to a newline character.

During input, a cin inputs data from the input buffer one character at a time. First, it inputs any leading white space characters and discard (throws away) them. Then it inputs all characters making up the data till it encounters a white space character which serves as a terminating character for cin. Cin stops inputting. However, it puts back in the input buffer the terminating white space character it last inputted.

The solution to the above problem is to issue a cin.ignore( ) call right after a cin and before issuing a getline. Cin.ignore inputs a single charcter and discards it. In the above case, it will input the character put back by the cin and will discard it. So, when getline is issued it will find the input buffer empty and will wait for the user to enter a message. The getline will input the message entered by the user ending with newline. It will keep the message and will discard the newline character.

Part II Discussion

cin inputs the leading white-spaces (spaces or newlines or tabs or an enter key characters) and discards them. Then it inputs non-white-space characters and keeps them as input. Then it input the first trailing white-space character, ends the input and puts back the ending white-space character.

getline inputs all characters (does not skip leading white spaces characters), keeps all characters (including leading white spaces characters), ends the input when it encounters a newline (or an enter key) character and discards the ending newline (or enter key) character

Part II TrueFalse Questions

Part II has four sections A), B), C) and D). Study each section and for each section, simply answer True or False. Although, the answers of these sections are given with explanations. You are still to do these questions and submit their answers for practice and learning the material

Part II Four True/False Question

A)

Code Fragment 1

cout << Enter message count: << endl;

cin >> count;

cout << Enter message: << endl;

getline(cin, message);

User input/output dialog (user input in bold)

Enter message count:

95

Enter message:

I love programming

Question

After the above code fragment executes with the above user input, the contents of count & message variables will be the following: (true/false)

Contents of count:

95

Contents of message:

(an empty string)

Answer

True.

Explanation

cin will input and discards leading spaces. It will input 95. It will keep 95 and put back the enter key character.

Then, getline will start inputting. It will input the character (put back above), regard the enter key as the ending character, stop inputting, discard enter key character and consider an empty string as the content of the message.

B)

Code Fragment 2

cout << Enter message count: << endl;

cin >> count;

cin.ignore( );

cout << Enter message: << endl;

getline(cin, message);

User input/output dialog (user input in bold)

Enter message count:

95

Enter message:

I love programming

Question

After the above code fragment executes with the above user input, the contents of count & message variables will be the following: (true/false)

Contents of count:

95

Contents of message:

I love programming

Answer

True.

Explanation

cin will input and discards leading spaces. It will input 95. It will keep 95 and put back the enter key character.

Then cin.ignore() will input character and will discard it.

Then, getline will start inputting. It will input I love programming and the character, regard the enter key as the ending character, stop inputting, discard enter key character and consider I love programming as the content of the message.

C)

Code Fragment 3

cout << Enter message count: << endl;

cin >> count;

cin.ignore( );

cout << Enter message: << endl;

getline(cin, message);

User input/output dialog (user input in bold)

Enter message count:

95

Enter message:

I love programming

Question

After the above code fragment executes with the above user input, the contents of count & message variables will be the following: (true/false)

Contents of count:

95

Contents of message:

(four spaces)

Answer

True.

Explanation

cin will input and discards leading spaces. It will input 95 and the first of the five space characters. It will keep 95 and put back the space character.

Then cin.ignore() will input one (of the total five) space characters and will discard it.

Then, getline will start inputting. It will input the four space characters and character, regard the enter key as the ending character, stop inputting, discard the enter key character and consider the four space characters as the content of the message.

D)

Code Fragment 4

cout << Enter message count: << endl;

cin >> count;

cin.ignore(100, ' ' );

cout << Enter message: << endl;

getline(cin, message);

User input/output dialog (user input in bold)

Enter message count:

95

Enter message:

I love programming

Question

After the above code fragment executes with the above user input, the contents of count & message variables will be the following: (true/false)

Contents of count:

95

Contents of message:

I love programming

Answer

True

Explanation

cin will input and discards leading spaces. It will input 95 and the first of the five space characters. It will keep 95 and put back the space character.

Then cin.ignore(100,' ') will input five space characters and the enter key character and will discard them.

Then, getline will start inputting. It will input the I love programming and the character, regard the enter key as the ending character, stop inputting, discard the enter key character and consider I love programming as the content of the message.

Testing Part I

Input Test Run

Use the following data for the test run. The user input is shown in bold.

Enter total number of times the message is to be displayed

5

Enter the message to be displayed

I love programming.

Output Test Run

Below is the output for the above test run.

I love Programming.

I love Programming.

I love Programming,

I love Programming,

I love Programming,

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!