As the title is referencing is I want to setup functions to call at certain point within the code. For example if I set up four functions fn1, fn2 fn3, fn4 can I call these either from main or within the function? I am doing this off my iPad since I am not near my computer
void fn1();
void fn2();
void fn3();
void fn4();
main(){
fn1;
fn1(choice) {
switchcase 1 : do some stuff
fn2();
break;
case 2 : do some more stuff
fn3();
break;
case 4 : do even more stuff
.
.
.
}
void fn1(){
//setup call to fn2
}
I know it isn't complete but I am just making sure I can do this or not
Cool. I am working on a SIMPLE exercise out of primer plus and going beyond what the author is asking for. Just wanted to make sure it would work. I will post my code once I get done with it. Most likely tomorrow or later
So far I have got this to work. Somewhat... What I want to to do is go to the second switch statement which I have set as a function. The fAnimal function works like it is supposed to but upon '0' exit back to the main menu it jumps to the quit statement and exits the program. Am I missing something here? Also another little problem is in the sub switch if I try and enter anything other than the correct response from the list the program freezes. I have another program which is a calc program and everything works fine in it. so I am beating myself up over this. ie... under the default : I have this. and if anything is pressed other than the 5 different responses it keeps looping until the correct response if given.
1 2 3
default :
cout << "You must enter a 1 thru 4 or 0 to EXIT";
cin >> choice;
/*3. Write a precursor to a menu-driven program. The program should display a menu
offering four choices, each labeled with a letter. If the user responds with a letter
other than one of the four valid choices, the program should prompt the user to
enter a valid response until the user complies.Then the program should use a
switch to select a simple action based on the user’s selection.A program run could
look something like this:
Please enter one of the following choices:
c) carnivore p) pianist
t) tree g) game
f
Please enter a c, p, t, or g: q
Please enter a c, p, t, or g: t
A maple is a tree.
*/
#include "version.h"
#include <iostream>
usingnamespace std;
void showMenu();
void carnivoe();
void pianist();
void tree();
void game();
int fAnimal();
int piano, gamer, trees;
char choice;
int main()
{
showMenu();
cin >> choice;
while (choice != 'q' && choice != 'Q')
{
switch (choice)
{
case'C' :
case'c' :
{
carnivoe();
fAnimal();
}
/* case 'T' :
case 't' :
cout << "You chose a Tree.\n"
"Please select from the list of what you want:\n"
"1) Dog 2) Cat \n"
"3) Wolf 4) Bear \n"
"5) Lion\n";
cout << "Select: ";
cin >> select;
*/
case'p' :
case'P' :
/* cout << "You chose a Carnivore.\n"
"Please select from the list of what you want:\n"
"1) Dog 2) Cat \n"
"3) Wolf 4) Bear \n"
"5) Lion\n";
cout << "Select: ";
cin >> piano;
*/
case'g' :
case'G' :
/*
cout << "You chose a Carnivore.\n"
"Please select from the list of what you want:\n"
"1) Dog 2) Cat \n"
"3) Wolf 4) Bear \n"
"5) Lion\n";
cout << "Select: ";
cin >> gamer;
*/
case'q':
case'Q':
cout << "Good bye";
return 0;
default :
"You did not make a correct choice. please try again.";
showMenu();
cin >> choice;
}
showMenu();
cin >> choice;
}
return 0;
}
void showMenu()
{
cout << "Please enter one of the following choices:\n""c) carnivore p) pianist\n""t) tree g) game \n""q) Quit";
cout << "\nChoice: ";
}
void carnivoe(){
cout << "You chose a Carnivore.\n""Please select from the list of what you want:\n""1) Dog 2) Cat \n""3) Wolf 4) Bear \n""5) Lion 0) Exit \n";
cout << "Select: ";
}
int fAnimal(){
int animal;
cin >> animal;
while (animal != 0)
{
switch (animal)
{
case 1 :
cout << "You chose a dog which is a type of carnivore.\n";
break;
case 2 :
cout << "Yes your 'Fluffy' is also a type of carnivore.\n";
break;
case 3 :
cout << "A wolf if just a big, mean and nasty dog, but also a type of carnivore.\n";
break;
case 4 :
cout << "A bear is just a huge critter with a very bad disposition.\n";
break;
case 5 :
cout << "A Lion is a big kitty and also called the 'King of the Jungle'\n";
break;
case 0 :
cout << "Returning to the main menu\n";
showMenu();
cin >> choice;
default :
"You did not make a correct choice. please try again.\n";
cin >> animal;
}
carnivoe();
cin >> animal;
}
showMenu();
cin >> choice;
}
A function should have a single purpose. Your fAnimal does both something animal-related and asks for a choice. That is too complicated. Limit it to the animal-stuff.
You are using global variables. Don't. They might seem to make things simpler, but in practice they do the opposite.
That sort of works. Why do you have line 45? The choice was not 0, so line 18 will give you a new value.
Why does the main say "Enter two numbers", but the functions actually read them?
Why does the main have i and j at all, because it does not use them?
Following that thought, the cin >> choice; could be in showMenu().
fAnimal() is no different from add(); they are both called and they both return. It is the differences in the main() that makes one "work" and other "not".
Well I can get the program to work and it will call the functions that I have built but once I try and go back to the main menu is when everything gets squirrelly... It will show the main menu but jump to the very first case statement again... So is this not iteration? About to give up and move on to another exercise.
Line 14: choice is uninitialized. First time through the loop, you going to be comparing garbage. That doesn't explain the behavior you're seeing, but it's still wrong.
I was talking about the original switch case with a nested switch called from a function. Once I tell the function to return to the main switch it gets really trashed.