Feb 12, 2013 at 11:15pm UTC
Trying to make a menu with ability to return to main, and quit.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
int main()
{
// Variable declaration
int choice = 9;
// I CUT STUFF OUT TO MAKE ROOM
cout << string( 10, '\n' );
system("PAUSE" );
// Enter fancy menu
while ( choice != 9 );
{
cout << string( 23, '\n' );
cout << "*******************************************************************************" << endl;
cout << "* *" << endl;
cout << "* *" << endl;
cout << "* *" << endl;
cout << "* THIS IS INTRO *" << endl;
cout << "* 1 to start *" << endl;
cout << "* *" << endl;
cout << "* *" << endl;
cout << "* *" << endl;
cout << "* *" << endl;
cout << "* *" << endl;
cout << "* *" << endl;
cout << "* *" << endl;
cout << "* *" << endl;
cout << "* *" << endl;
cout << "* *" << endl;
cout << "* *" << endl;
cout << "* *" << endl;
cout << "* *" << endl;
cout << "* *" << endl;
cout << "* *" << endl;
cout << "* *" << endl;
cout << "* *" << endl;
cout << "*******************************************************************************" << endl;
cout << "Make selection: " ;
cin >> choice;
// MAIN MENU
switch (choice)
{
{
case 1:
cout << string( 23, '\n' );
cout << "*******************************************************************************" << endl;
cout << "*THIS IS MAIN MENU *" << endl;
cout << "* *" << endl;
cout << "* *" << endl;
cout << "* *" << endl;
cout << "* *" << endl;
cout << "* *" << endl;
cout << "* *" << endl;
cout << "* *" << endl;
cout << "* *" << endl;
cout << "* *" << endl;
cout << "* *" << endl;
cout << "* *" << endl;
cout << "* *" << endl;
cout << "* *" << endl;
cout << "* *" << endl;
cout << "* *" << endl;
cout << "* *" << endl;
cout << "* *" << endl;
cout << "* *" << endl;
cout << "* *" << endl;
cout << "* *" << endl;
cout << "*Return to main menu = 1 *" << endl;
cout << "*******************************************************************************" << endl;
cin >> choice;
break ;
}
if (choice == 1)
{
case 1;
}
// OPTION 1
case 2:
{
cout << string( 23, '\n' );
cout << "*******************************************************************************" << endl;
cout << "*THIS IS NUMBER ONE *" << endl;
cout << "* *" << endl;
cout << "* *" << endl;
cout << "*Go to main menu = 1 *" << endl;
cout << "*Exit = 0 *" << endl;
cout << "* *" << endl;
cout << "* *" << endl;
cout << "* *" << endl;
cout << "* *" << endl;
cout << "* *" << endl;
cout << "* *" << endl;
cout << "* *" << endl;
cout << "* *" << endl;
cout << "* *" << endl;
cout << "* *" << endl;
cout << "* *" << endl;
cout << "* *" << endl;
cout << "* *" << endl;
cout << "* *" << endl;
cout << "* *" << endl;
cout << "* *" << endl;
cout << "* *" << endl;
cout << "*******************************************************************************" << endl;
cout << "Make selection: " ;
cin >> choice;
break ;
}
// CUT STUFF HERE TOO.
}
}
system("PAUSE" );
return 0;
}
Last edited on Feb 13, 2013 at 12:18am UTC
Feb 12, 2013 at 11:16pm UTC
I ran out of room due to my cheesy border and such.
I apologize in advance if my method is awful, that's why I came here.
I cut a ton of stuff out which served as an introduction, as well as some more "pages" may have messed it up, so try not to nitpick { and such.
I'm trying to make a base menu that can go out to single pages, and possibly back to the main menu, and so on.
147 is a complete shot in the dark. I'm lost when it comes to starting over at main menu.
Last edited on Feb 12, 2013 at 11:39pm UTC
Feb 13, 2013 at 6:16am UTC
I am far from being even close to an expert, but I'm pretty sure that if you want the program to keep running, you don't
return 0;
you tell it to return to wherever you want it to go, in this case, I would guess
return choice;
or whatever your main page reference is. If your wanting to return to main, I'd guess that you would have to add something to your code to reference from return that would allow you to pop back in there, and then throw in an option to choose, say a number, that would then return zero, and then exit the program. Just a guess, good luck!
Last edited on Feb 13, 2013 at 6:20am UTC
Feb 13, 2013 at 7:00am UTC
void menu2()
{
//menu stuff here//
//....
//....
}
void menu1()
{
//menu stuff here//
//....
//....
if (choice == ....)
menu2();
}
int main()
{
menu1();
return 0;
}
Last edited on Feb 13, 2013 at 10:55am UTC
Feb 13, 2013 at 7:18am UTC
Smac: so menu 1 is the base? Since you're allowing it to return 0 and end the program? Why does the first bit not have an if?
Are you writing it essentially backwards to make it work? Any help or details appreciated
Thanks xanthian, I'll try that too
Last edited on Feb 13, 2013 at 9:22am UTC
Feb 13, 2013 at 9:18am UTC
I (think I) followed what you gave me, Smac89, but I get an error at the if statement in "menu 1". It says the identifier menu is not found.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
// Variable declaration
int choice = 0;
// Enter menu
void menu8()
{
cout << string( 23, '\n' );
cout << "This is menu 8." << endl;
}
void menu7()
{
cout << string( 23, '\n' );
cout << "This is menu 7." << endl;
}
void menu6()
{
cout << string( 23, '\n' );
cout << "This is menu 6." << endl;
}
void menu5()
{
cout << string( 23, '\n' );
cout << "This is menu 5." << endl;
}
void menu4()
{
cout << string( 23, '\n' );
cout << "This is menu 4." << endl;
}
void menu3()
{
cout << string( 23, '\n' );
cout << "This is menu 3." << endl;
}
void menu2()
{
cout << string( 23, '\n' );
cout << "This is menu 2." << endl;
}
void menu1()
{
cout << string( 23, '\n' );
cout << "This is menu 1.\n\n" << endl;
cout << "Make a selection:" << endl;
cin >> choice;
if (choice == 1)
menu(2);
}
int main()
{
menu1();
return 0;
}
Last edited on Feb 13, 2013 at 9:22am UTC
Feb 13, 2013 at 10:04am UTC
Try menu2(); instead of menu(2);
Feb 13, 2013 at 10:13am UTC
Thanks plexus! I figured that typo out shortly after. :P
The problem now is, once I leave menu 1, I can't go back to it from another..
Feb 13, 2013 at 10:44am UTC
You cant call menu1() from any of the other menu#() functions because it's defined later.
Either you put it at the top of the function definitions or create function prototypes (which is the cleaner way).
But beware: don't call menu1() from one of the other menu functions because you would create some kind of weird indirect recursion like:
menu1() -call-> menu2() -call-> menu1() -call-> menu3() -....
Better do something like:
menu1() -call-> menu2() -return to-> menu1() -call-> menu3() -...
Feb 13, 2013 at 10:51am UTC
That is where function headers come in handy. Using these, you can call a function without having it's entire declaration above the code calling it. This is a compile time operation.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
// Variable declaration
int choice = 0;
// Enter menu
void menu7();
void menu8();
void menu1();
void menu4();
void menu2();
void menu6();
void menu3();
void menu5();
int main()
{
menu1();
return 0;
}
void menu8()
{
cout << string( 23, '\n' );
cout << "This is menu 8." << endl;
}
void menu7()
{
cout << string( 23, '\n' );
cout << "This is menu 7." << endl;
}
void menu6()
{
cout << string( 23, '\n' );
cout << "This is menu 6." << endl;
}
void menu5()
{
cout << string( 23, '\n' );
cout << "This is menu 5." << endl;
}
void menu4()
{
cout << string( 23, '\n' );
cout << "This is menu 4." << endl;
}
void menu3()
{
cout << string( 23, '\n' );
cout << "This is menu 3." << endl;
}
void menu2()
{
cout << string( 23, '\n' );
cout << "This is menu 2." << endl;
cout <<"Back to menu 1\n" ;
menu1();
}
void menu1()
{
cout << string( 23, '\n' );
cout << "This is menu 1.\n\n" << endl;
cout << "Make a selection:" << endl;
cin >> choice;
if (choice == 1)
menu2();
}
I scattered the declarations so you know that is doesn't matter the order you do them in as long as you declare them
Last edited on Feb 13, 2013 at 10:54am UTC