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
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
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
Then, getline will start inputting. It will input the
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
Then cin.ignore() will input
Then, getline will start inputting. It will input I love programming and the
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
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
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
Get step-by-step solutions from verified subject matter experts
