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

Do you like ice cream? (y / n): x Incorrect value Do you like ice cream? (y / n)

ID: 3824619 • Letter: D

Question

Do you like ice cream? (y / n): x Incorrect value Do you like ice cream? (y / n):8 Do you like ice cream? (y / n): y y Enter your age a an integer abcdefg You must enter an integer Enter your age as an integer:34 Thanks for your input Press any key to continue...First ask the user a yes/no question of your choice and ask them to input y or n as their answer. Check to make sure the input is valid. Then ask the user to enter her age. Remember that if the user does not enter a number the input stream goes into the fail state. Your need to rest the input steam back into working order:

Explanation / Answer

Hi, Please find below the program:

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int main() {
   char inpChar = {0};
   int inpNum = 0;
   string inp = "";
MAIN_LOOP:
while (true) {
  
cout << "DO you like ICE Cream (y/n)";
getline(cin, inp);

if (inp.length() == 1) {
inpChar = inp[0];
if(inpChar!='y' ||inpChar!='Y' ||inpChar!='N'|| inpChar!='n'){
cout << "Incorrect Value" << endl;
continue;
}
}

while(true)
{
cout << "Enter your age as an integer:";
getline(cin, inp);
stringstream checkNum(inp);
if (checkNum >> inpNum)
break;
cout << "You must enter an Integer" << endl;
if(!cin){
    cin.clear();
    cin.ignore(2000,' ');
}
}

cout <<"Press any key to continue...";
while(true){
    if(getline(cin, inp))
    break;
}
} // End of MAIN LOOP


return 0;
}

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