Reputation: 9
My objective to display a menu in which user chooses between 5 functions (1-5). After function runs and outputs its data. At the end the user chooses between functions (1-5) again.
My (incorrect) semi-code:
int main(){
int option;
int *pOption = &option;
bool choice = true;
cout << "Main menu:
cout << "To use function 1 enter 1. \n";
cout << "To use function 2 enter 2. \n";
cout << "To use function 3 enter 3. \n";
cout << "To exit please enter 0. \n";
cin >> option;
while (choice == true) {
if (option == 1) {
cout << "You picked Function one: ";
functionOne(Variable);
}
if (option == 2) {
cout << "You picked Function 2: ";
functionOne(Variable);
}
if (option == 3) {
cout << "You picked Function 3: ";
functionOne(Variable);
}
}//end of while loop
return 0;
}
For example lets say the user hits option 1 and goes to this function below. After we do our output how can I utilize the pointer 'pOption' I created to select a new function to use? Or am I completely missing the ball?
void functionOne(Variable){
cout<< "This is function 1";
cin >>pOption;
return;
}
Upvotes: 0
Views: 1443
Reputation: 33952
I'm going to stick with what seems to be the main question and get to the other bits of if I have time before the battery runs out.
Rather than ask for the next option insidea called function, do it in the main function like this:
cin >> option;
while (0 != option) <-exit loop if option is zero
{
if (option == 1) {
cout << "You picked Function one: ";
functionOne(Variable);
}
if (option == 2) {
cout << "You picked Function 2: ";
functionOne(Variable);
}
if (option == 3) {
cout << "You picked Function 3: ";
functionOne(Variable);
}
cin >> option; <- ask for next option here
}//end of while loop
Next, you can save a bit of hassle by reading up on else if
and switch
cin >> option;
while (0 != option)
{
switch (option) {
case 1:
cout << "You picked Function one: ";
functionOne(Variable);
break;
case 2:
cout << "You picked Function 2: ";
functionOne(Variable);
break;
case 3:
cout << "You picked Function 3: ";
functionOne(Variable);
break;
default:
cout << "You picked an unsupported option: " << option;
}
cin >> option;
}//end of while loop
Next, what if some fool types in "I'm not a number, sucker" for option? Read up on input validation. Google's your friend here, not me anymore. MY battery's at 3% and I'm shutting down. Bye.
Upvotes: 1