This in C++. Correct the bottom program. Thanks /* since cin >> var; leaves a ne
ID: 2246311 • Letter: T
Question
This in C++. Correct the bottom program. Thanks
/*
since cin >> var; leaves a newline, and getline(cin, s) consumes all characters up to and including a new line, using the first before the second will cause the leftover newline character to be interpretted as hitting enter without any text on the second.
To fix this, we want to consume the extra newline left behind by our numerical input technique.
cin.ignore(n, pattern) ignores n characters or until the first encountered instance of pattern from the input stream.
*/
Explanation / Answer
#include <iostream>
#include <string>
using namespace std;
int main () {
string inputString;
int inputInt;
cout << "Enter a number: ";
cin >> inputInt;
cout << "Input was: "
<< inputInt << endl;
cout << "Enter a string: ";
cin.ignore(1,' ');
getline(cin, inputString);
cout << "Input was: "
<< inputString << endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.