I tried to write a menu program as a switch-case structure with a separate function for a switch-case structure itself. The outcome for it currently is an undesired one, hence why I'm asking this question.
#include <iostream>
usingnamespace std;
int menu(int answer);
int main()
{
int answer;
cout << "Pick a choice from the list: " << endl;
cout << "Pick from choices 1, 2, or 3, depending on what you want from the menu.\n";
answer = menu(answer);
cin >> answer;
}
int menu(int answer)
{
do
{
switch (answer)
{
case 1:
cout << "I want to run.\n";
break;
case 2:
cout << "I want to walk.\n";
break;
case 3:
cout << "I just want to talk to my friends.";
break;
default:
cout << "Bad choice! Please try again later.\n";
}
} while (answer <= 0 || answer > 3);
return answer;
}
int main()
{
int answer; // <- 'answer' is uninitialized, so it will contain garbage. Let's say it contains
// 8 for this example
// some stuff gets printed to the user
cout << "Pick a choice from the list: " << endl;
cout << "Pick from choices 1, 2, or 3, depending on what you want from the menu.\n";
// you call 'menu' with answer as the param (remember, at this point answer=8/garbage)
answer = menu(answer);
/* --- code jumps to 'menu' function --- */
int menu(int answer) // <- answer=8/garbage
{
do
{
switch (answer) // <- switching on answer, which is 8/garbage
{
case 1: // <- answer does not == 1, so this case is skipped
cout << "I want to run.\n";
break;
case 2: // <- also skipped
cout << "I want to walk.\n";
break;
case 3: // <- also skipped
cout << "I just want to talk to my friends.";
break;
default: // <- since 8/garbage does not match any case, we do the default
cout << "Bad choice! Please try again later.\n"; //<- print this message
}
} while (answer <= 0 || answer > 3); // <- since 8 falls outside 0/3, we loop
// endlessly repeating the switch statement, and endlessly printing that message over, and over
// and over and over. Since the contents of 'answer' never changes, you never break out of this loop
char choice;
cout << "Which do you want to do? (1)Run, (2)Walk,(3)Talk enter the number"<< endl;
cin >> choice;
switch(choice)
{
case'1': cout << "Whatever";
case'2': cout << "Whatever";
case'3': cout << "Whatever";
default: cout << "You've chosen neither";
}
You have to ask which they want and save that as a variable. Here I was saving their choice as a character, because I'm telling them to enter 1, 2, or 3.
Use '1', '2', or '3' after case because this is what you do for chars.
I have to get it to reprint the menu if the user inputs something that isn't amongst the actual choices in the menu. How do I reprint it in that situation?
// Osman Zakir
// 2/14/2015
// Jumping into C++ Practice Problem
// Menu Program using Switch-Case
#include <iostream>
usingnamespace std;
// function prototypes
int menu(int answer);
// These are to make sure there's a separate function call for each menu choice
int run(int answer);
int walk(int answer);
int talk(int answer);
int main()
{
int answer;
cout << "Pick a choice from the list; (1)Run, (2)Walk, (3)Talk: ";
cin >> answer;
menu(answer);
}
int menu(int answer)
{
int attempts = 0;
while (answer <= 0 || answer > 3)
{
switch (answer)
{
case 1:
run(answer);
break;
case 2:
walk(answer);
break;
case 3:
talk(answer);
break;
default:
cout << "Bad choice! Please try again: ";
cout << "Pick a choice from the list; (1)Run, (2)Walk, (3)Talk.\n";
attempts++;
}
}
return answer;
}
int run(int answer)
{
cout << "Good choice! This will help you burn some calories!\n";
return answer;
}
int walk(int answer)
{
cout << "Still a good choice, as you'll burn calories slowly, but surely.\n";
return answer;
}
int talk(int answer)
{
cout << "We can't have that! You won't burn any calories unless you either run or walk.\n";
return answer;
}
I did take a lot of stuff you had out. but It should be okay with just this. I don't know if you want to output the number count or not but this is what I have.
#include <iostream>
#include <conio.h>
usingnamespace std;
int answer;
void calculations()
{
switch (answer)
{
case 1:
cout << "Good choice! This will help you burn some calories!\n";
break;
case 2:
cout << "Still a good choice, as you'll burn calories slowly, but surely.\n";
break;
case 3:
cout << "We can't have that! You won't burn any calories unless you either run or walk.\n";
break;
default:
cout << "Bad choice! Please try again: \n";
break;
}
}
int main()
{
bool keepLooping = true;
while (keepLooping)
{
cout << "Pick a choice from the list; (1)Run, (2)Walk, (3)Talk: ";
cin >> answer;
calculations();
if (answer < 1 || answer > 3)
keepLooping = true;
else
keepLooping = false;
}
_getch();
return answer;
}
Either way, though, I'll have to turn all of the cases into their own separate functions anyway because a practice problem in a later chapter pertaining to this program tells us to do that.
Something like:
1 2 3 4 5 6 7 8 9 10 11
switch(answer)
{
case 1:
run(answer);
case 2:
walk(answer);
case 3:
talk(answer);
default:
cout << "Bad choice! Please try again later.\n";
}
// Osman Zakir
// 2/14/2015
// Jumping into C++ Practice Problem
// Menu Program using Switch-Case
#include <iostream>
usingnamespace std;
// function prototypes
int menu(int answer);
// These are to make sure there's a separate function call for each menu choice
int run(int answer);
int walk(int answer);
int talk(int answer);
int main()
{
int answer;
bool keepLooping = true;
while (keepLooping)
{
cout << "Pick a choice from the list; (1)Run, (2)Walk, (3)Talk: ";
cin >> answer;
menu(answer);
if (answer < 1 || answer > 3)
{
keepLooping = true;
}
else
{
keepLooping = false;
}
}
}
int menu(int answer)
{
switch (answer)
{
case 1:
run(answer);
break;
case 2:
walk(answer);
break;
case 3:
talk(answer);
break;
default:
cout << "Bad choice! Please try again:" << endl;
}
return answer;
}
int run(int answer)
{
cout << "Good choice! This will help you burn some calories!\n";
return answer;
}
int walk(int answer)
{
cout << "Still a good choice, as you'll burn calories slowly, but surely.\n";
return answer;
}
int talk(int answer)
{
cout << "We can't have that! You won't burn any calories unless you either run or walk.\n";
return answer;
}