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
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.
*/
#include#include
using namespace std; int main () { string inputString; int inputInt; cout << "Enter a number: "; cin >> inputInt; cout << "Input was: " << inputInt << endl; cout << "Enter a string: "; getline(cin, inputString); cout << "Input was: " << inputString << endl; } Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
