Skip to main content
GameDev.net gamedev.net
🔒 Locked

making menus and sub menus....plz help!

Started by cocopuffs Jun 8, 2010 at 9:15 PM 4 replies 1.4k views
Original Post
cocopuffs
cocopuffs
how do i make sub menus for a main menu

iv bein trying to do this for 3 days now by my self with books and i was trying it with the while and switch statments but they only work so far how can i do this

i kno its a dumb question lol most of u r like damn he doesnt kno this what a fag lol

someone plz help!!!!!

(this is what i have( i erased some stuff cuz it didnt work so i have the basic thing)




#include "stdafx.h"
#include "iostream"
#include "ctime"
#include "stdio.h"

int main()
{
using namespace std;


int menu;

do
{
cout << "==============================" << endl;
cout << "|| MAIN MENU ||" << endl;
cout << "==============================" << endl;
cout << "||Please make a selection: ||" << endl;
cout << "||1. create a character ||" << endl;
cout << "||2. shop ||" << endl;
cout << "||3. view stats ||" << endl;
cout << "||4. enter death arena ||" << endl;
cout << "||5. quit game ||" << endl;
cout << "============================== \n" << endl;
cin >> menu;



} while (menu != 1 && menu != 2 &&
menu != 3 && menu != 4 && menu != 5);

switch selection (menu)
{
case 1:

// sub menu character
cout << "" << endl; // space to look better
cout << "==============================" << endl;
cout << "|| CHARACTER MENU ||" << endl;
cout << "==============================" << endl;
cout << "||1. name your character ||" << endl;
cout << "||2. roll for stats ||" << endl;
cout << "||3. back to main menu ||" << endl;
cout << "============================== \n" << endl;
break;



case 2:

//sub menu shop
cout << "" << endl; // space to look better
cout << "==============================" << endl;
cout << "|| SHOP MENU ||" << endl;
cout << "==============================" << endl;
cout << "||1. purchase armor ||" << endl;
cout << "||2. purchase weapon ||" << endl;
cout << "||3. back to main menu ||" << endl;
cout << "==============================" << endl;
break;

case 3:

//sub menu shop
cout << "" << endl; // space to look better
cout << "==============================" << endl;
cout << "|| VIEW STATS ||" << endl;
cout << "==============================" << endl;
cout << "||1. name: ||" << endl;
cout << "||2. level: ||" << endl;
cout << "||3. strength: ||" << endl;
cout << "||4. dexterity: ||" << endl;
cout << "||5. constitution: ||" << endl;
cout << "||6. HP: ||" << endl;
cout << "||7. exp: ||" << endl;
cout << "||8. gold: ||" << endl;
cout << "||9. exit menu ||" << endl;
cout << "==============================" << endl;
break;




}


return 0;
}

[Edited by - jpetrie on June 9, 2010 12:31:25 AM]
hasonish
hasonish
I'm no expert, as I'm a beginner just like you, but I did notice a minor error.

cout << "" << endl; // space to look better

If I'm not mistaken, the double quotes aren't needed, as you can just type:

cout << endl;

"endl;" pretty much translates as pressing "Enter", so it automatically moves the cursor to the next line.

And as for your menu system, have you thought about using function calls?
--Jason Maurer Budding C++ Programmer
consequence
consequence
As hasonish says, you should really consider using at least a function for drawing menus. You can pass the function a structure of your choosing with the title and all the options, and then have it return the user's choice.

I think the problem you're seeing is that you aren't waiting for input after the user chooses a sub-menu. It just prints out that sub-menu, and then main() is done and returns 0.

I just wrote this up to show you what you could do here. I'm fairly new to c++ so don't take anything I've written as infallible.

#include <vector>using namespace std;const char* DIVIDER = "==============================";int drawmenu(vector<char*> menu){  int choice = -1;  // initialize the user's input to a default value  cout << DIVIDER << endl;  // I think this is a bit easier than pasting lines of '='  cout << "|| " << menu[0] << " ||" << endl; // this would be the title of the menu; e.g. MAIN MENU  cout << DIVIDER << endl;  cout << "||Please make a selection: ||" << endl;  for (int i = 1; i < menu.size(); i++)     // iterate through the remainder of the vector to print options  {    cout << "||" << i << ". " << menu << " ||" << endl;  }  cout << DIVIDER << endl << endl;  while (choice < 1 || choice > menu.size() - 1)  // receive input as long as the last one was invalid  {    cin >> choice;        // you could also put an error message for invalid inputs  }  return choice;}int main(){vector<char*> mainmenu;     // this is the main menu vector, holding all the main menu's infomainmenu.push_back("MAIN MENU");mainmenu.push_back("create a character");mainmenu.push_back("shop");mainmenu.push_back("view stats");mainmenu.push_back("enter death arena");mainmenu.push_back("quit game");int menu = drawmenu(mainmenu);    // this is how you would capture the user's choice// if you don't store the return value of drawmenu(), it will be a wasted call


After this, you would do your
switch (menu)
and call drawmenu() for the sub-menus as needed.
hasonish
hasonish
Wow, I've gotta say, that's pretty awesome code right there. I've barely learned about vectors, so I don't know how to use it much, but this is a pretty good example of it.

This is much better than what I was trying to work out. However, I still new to programming, so it's to be expected.

Once again, I'd like to thank you for that code snippet consequence.
--Jason Maurer Budding C++ Programmer
Aerodactyl55
Aerodactyl55
first i am not an expert at game programming...
To draw menus and submenus you can use gamestates and function pointers. Like this:

1. Create an enumeration of all the menu states of you game( main menu, options, in game options,... );
2. Create a function for each menu you need to show;
3. Create a function that will change the state of your game and pass the address of the appropriate function for the menu you need to draw( based on the current state ) to a function pointer;
4. For each time you need to show the menu use the function pointer;

I dont know if my idea is clear, anyway this method expects that the functions that will draw each menu have the same number of arguments and return value.
ExcessNeo
ExcessNeo
const char* DIVIDER = "==============================";


Should be:
const std::string DIVIDER(30, '=');


All beginner C++ programmers should be learning to use parts of the standard library which means std::string over char* strings.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.