Sign in to follow this  
Jynxx

trouble with console menu

Recommended Posts

Jynxx    122
i want to make a menu for a game project a friend and i are doing. its a text based game reliant solely on a menu of choices. upon loading one menu it will do its functions and proceed....however ive hit a writers block. creating a menu using arrays is easy but how can i switch from one array to the next for new menu options and use a return feature to swap back to the previous? ive been giving it thought and i have somewhat of an idea but i cant seem to grasp it all. thank you for any advice =)
#include <iostream>
#include <fstream>
#include <strstream>
#include "item.h"
#include "weapon.h"

using namespace std;

#define TRUE 1
#define FALSE 0
#define CLEAR_SCREEN system("cls")
#define WIDTH 4
#define HEIGHT 4
#define MAX_MENU_SIZE 4

typedef char txtFile[];
typedef char aMenu;

txtFile materials = "mats.txt";
txtFile weapons = "weps.txt";
txtFile armor = "armor.txt";

int loadResources(txtFile mats, txtFile weaps, txtFile armors);

aMenu *chMenu[WIDTH][HEIGHT] = {"1] Weapons", "2] Armor", "3] Items", "4] Exit",
								"1] Sword", "2] Bow", "3] Mace", "4] Return",
								"1] Helmet", "2] Shield", "3] Breastplate", "4] Return",
								"1] Mold", "2] Potion", "3] Rune", "4] Return"};

aMenu *chWeapons[WIDTH][HEIGHT] = { "1] Short Sword", "2] Long Sword", "3] Claymore", "4] Return" };


int loadGame();
void loadMenu();
int displayNext(int choice, aMenu selection);
void displayMain();
	
int gameState = TRUE;

int main() {

	if (loadResources(materials, weapons, armor) == TRUE) {
		loadGame();
	}

	return 0;
}

int loadResources(txtFile mats, txtFile weaps, txtFile armors) {
	ifstream fMats;
	fMats.open(mats);
    if (!fMats.is_open())
    {
        cout << "Error 101: Couldn't initialize fMats();" << endl;
        return -1;
    }else{
		cout << "fMats Stream Opened" << endl;
		fMats.close();
		cout << "fMats Stream Closed" << endl;
    }
    ifstream fWeaps;
	fWeaps.open(weaps);
    if (!fWeaps.is_open())
    {
        cout << "Error 102: Couldn't initialize fWeaps();" << endl;
        return -1;
	}else{
		cout << "fWeaps Stream Opened" << endl; 
        fWeaps.close();
		cout << "fWeaps Stream Closed" << endl;
    }
    ifstream fArmors;
	fArmors.open(armors);
    if (!fArmors.is_open())
    {
        cout << "Error 103: Couldn't initialize fArmors();";
        return -1;
    }else{
		cout << "fArmors Stream Opened" << endl;
        fArmors.close();
		cout << "fArmors Stream Closed" << endl;
	}
	return 1;
}

int loadGame() {
	loadMenu();
		
	system("PAUSE");
	return 0;
}

/*
void loadMenu() {
		int choice = 4;

		while(gameState == TRUE)
		{
			if(choice == 0)
			{
				gameState = FALSE;
			}
			while(choice == 4)
			{
				CLEAR_SCREEN;
				for (int i = 0; i<=HEIGHT-1; ++i)
				{
					cout << chMenu[0][i] << endl;
				}
				cin >> choice;
			}
			while(choice < 4 && choice != 0 && gameState == TRUE)
			{
				CLEAR_SCREEN;
				for(int j = 0; j<=HEIGHT-1; ++j)
				{
					cout << chMenu[choice][j] << endl;
				}
				cin >> choice;
			}
			gameState = FALSE;
		}
}
*/

//new menu functions

void loadMenu()
{
	int uInput;
	while(gameState == TRUE)
	{
		displayMain();
		cin >> uInput;
		while(uInput <= MAX_MENU_SIZE && uInput != 0)
		{
			int menuVec = displayNext(uInput);
			cout << menuVec;
			if(menuVec == -1)
			{
				cout << "Thank you for playing!" << endl;
				gameState = FALSE;
			}
			cin >> uInput;
		}
	}
}

void displayMain()
{
	for(int i=0; i<MAX_MENU_SIZE; ++i)
	{
		cout << chMenu[0][i] << endl;
	}
}

int displayNext(int choice, aMenu selection)
{
	CLEAR_SCREEN;
	if(choice == 4)
	{
		CLEAR_SCREEN;
		return -1;
	}
	for(int j=0; j<MAX_MENU_SIZE; ++j)
	{
		cout << chMenu[choice][j] << endl;
	}
	return choice;
}

EDIT: i am aware what this program does and why...its screwy output ive been messing around with it as you may see the argument for displayNext aMenu Selection and the menuVec placeholder to see what the input of the user was =)

Share this post


Link to post
Share on other sites
nGamer    184
It appears that you are asking about how you can have a menu system that does not follow linearity. For example if you enter a menu from one screen, you would like to return to the same previous screen when you back out. Is this what you are asking?

Share this post


Link to post
Share on other sites
Jynxx    122
Quote:
Original post by nGamer
It appears that you are asking about how you can have a menu system that does not follow linearity. For example if you enter a menu from one screen, you would like to return to the same previous screen when you back out. Is this what you are asking?


exactly what im trying to do

Share this post


Link to post
Share on other sites
nGamer    184
From main (), there could be,


void showMainMenu () ;
void showMenu1 () ;
void showMenu2 () ;
void showMenu3 () ;

int main ()
{
showMainMenu () ;
}

void showMainMenu ()
{
// Insert body here
if (someCondition1) showMenu1 ;
else if (someCondition2) showMenu2 ;
else if (someCondition3) showMenu3 ;
}

void showMenu1 ()
{
// Insert body here
if (someCondition) showMainMenu () ;
}

void showMenu2 ()
{
// Insert body here
if (someCondition) showMainMenu () ;
}

void showMenu3 ()
{
// Insert body here
if (someCondition) showMainMenu () ;
}

Share this post


Link to post
Share on other sites
benryves    1999
Quote:
Original post by nGamer
From main (), there could be,

*** Source Snippet Removed ***
Wouldn't that gradually fill up the stack? I'd expect to return to the main menu rather than repeatedly call it.

void showMenu1 ()
{
// Insert body here
if (someCondition) return;
}

void showMenu2 ()
{
// Insert body here
if (someCondition) return;
}

void showMenu3 ()
{
// Insert body here
if (someCondition) return;
}


Share this post


Link to post
Share on other sites
Crowseye    308
This seems like an opportunity to use a pointer that points to your current menu and is made to point to a different menu when the user enters a menu option (or NULL when the player wishes to quit). Sadly, I am not intelligent enough to figure out how to make the code you have do this without rewriting a lot of the program, so hopefully somebody with more experience will come along. In the past I've done console work with screen and/or menu classes that makes using a currentScreen pointer (or smart pointer) an excellent way to do what you seem to want to do, however.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this