The instructor want us to write a program that can re displays the menu only whe
ID: 3538626 • Letter: T
Question
The instructor want us to write a program that can re displays the menu only when the user wants to restart a selection and add an option to continue with another selection.
The problem I have is when the user select a number from 1 to 4 and complete the selection, the program will ask the user if the user want to continue with another selection and when the user says no, the program still ask to select a number without ending program.
This what of I want on my program: The user select a number from 1 to 5. When the user select that number, the user will be asked " Do you want to continue another selection? (Y/N)" If the user says Yes, then the program will ask to select a number from 1 to 5. However, if the user says No, then the program will exit. But I having trouble on making program to do that.
So far I have this old code: http://www.mediafire.com/view/cozrcxi7xi58df1/lab8.txt
I try many things to figure out but cant get NO part to exit the program
Explanation / Answer
please rate - thanks
This correction allows multiple wrong input after each other, has minimum changes to your code
#include<iostream>
using namespace std;
int sp;
int speed = 0;
int M, K,x, c;
const int MINspeed = 10;
const int MAXspeed = 40;
int GetSpeed();
int GetMinSpeed();
int GetMaxSpeed();
int CheckContinue();
int selection;
int GetSpeed()
{
char c;
while(true)
{
cout << " Do you want the speed in mph or km/h? "
<< " Enter M or K followed by Enter: " << endl;
cin >> c;
if( (c != 'M')&& (c != 'K'))
{
cout << "Incorrect Selection. Try Again! ";
break;
}
if ( c == 'M')
{
cout << " Speed in mph: " << speed << endl;
return speed;
}
else if(c == 'K')
{
double toKmPerHour = 1.61;
double speedInKmPerHour = speed * toKmPerHour;
cout << " Speed in km/h:" << speedInKmPerHour << endl;
break;
}
}
return 0;
}
int GetMinSpeed()
{
cout << "MIN speed = " << MINspeed << endl;
return 0;
}
int GetMaxSpeed()
{
cout << "MAX speed = " << MAXspeed << endl;
return 0;
}
/*int SetSpeed(int sp)
{
cout << "The Set Speed is " << sp << endl;
return 0;
}
*/
void SetSpeed()
{
cout << "Input your speed: ";
cin >> speed;
}
int CheckContinue(void)
{
char x;
cout << " Do you want to continue with another selection? "
<< " Enter Y or N followed by Enter: " << endl;
cin >> x;
if ( x == 'Y')
return 0;
else
return -1;
}
/*
In this menu function, it will ask the user to input the selection, ranging from 1 to 5.
If the user puts a number that is not between 1 to 5 or letters, then the program will
ask the user to input a valid selection.
*/
void menu()
{
int selection;
int bye = 0;
while(bye==0)
{
cout << "Selection Menu" << endl;
cout << "--------------" << endl;
cout << " 1. Set Speed" << endl;
cout << "2. Get Speed" << endl;
cout << "3. Get MAX Speed" << endl;
cout << "4. Get MIN Speed" << endl;
cout << "5. Exit" << endl;
cout << " Your selection :" <<endl;
cin >> selection;
bye = 0;
switch(selection)
{
case 1:
SetSpeed();
break;
case 2:
GetSpeed();
break;
case 3:
GetMaxSpeed();
break;
case 4:
GetMinSpeed();
break;
case 5:
cout << "Good Bye" << endl;
bye = -1;
break;
default:
cout << " Please input valid selection: " << endl;
break;
}
if(bye==0)
bye=CheckContinue();
}
}
int main()
{
menu();
return 0;
}//end of main function
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.