Im trying to add code so if the user mistypes they can go back in the program and retype the input but im not sure if the code i found and used is correct. here it is in my function:
/********************************************/
// Name: inspools /
// Description: Ask for and get number of /
// spools /
// Parameters: N/A /
// Reture Value: spoolnum /
/********************************************/
int spoolnum()
{
int spoolnum;
char type;
cout << "Number of spools to be shipped: " << endl;
cin >> spoolnum;
cout << spoolnum << " spool(s) of wire will be shipped" << endl;
cout << "Is this correct? [y/n] ";
cin >> type;
if ('n') << spoolnum;
if ('y') break;
return spoolnum ;
}
You said you searched for loops, but I don’t buy it. I imagine you are pretty new at programming. I’m going to give you the answer but not without some explanation first.
How While Loops Work
From Wikipedia:
Your Problem
Your problem is that you want to keep making the user enter a choice until they enter
y. To do this, you need at least aWHILEloop, or as other commenters have said aDO/WHILEloop.I have never preferred
DO/WHILEloops but others do prefer it.The problems you may have with the below code is that you have more than just
yreturned incinsuch as a newline (\n) character. You will have to handle that condition.or the alternative
DO/WHILE:In the above code, I removed your
if ('n') << spoolnum;because frankly it does not make sense.I also removed
if ('y') break;because thewhile(...)loop will break once the condition is met, which istype equal to 'y'.