Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote