#include <iostream>
#include <windows.h>
using namespace std;

void about(){
		cout<<"Expence-List 14/10/2008:"<<endl
		      <<"Brought to you by www.******.com"<<endl
    		      <<"Programmed by **********."<<endl;
		Sleep(4000);
		system("cls");
	}


int MenuOne(){
		int MenuSelection=0;
		cout<<"What would you like to do?"<<endl
			<<"--------------------------------"<<endl
			<<"1. Create a new expence list."<<endl
			<<"2. Edit a previous expence list."<<endl
			<<"3. View a saved expence list."<<endl
			<<"4. Settings"<<endl
			<<"5. About."<<endl
			<<"6. Close Program"<<endl
			<<"--------------------------------"<<endl
			<<endl;
		cin>>MenuSelection;
		system("cls");
		return MenuSelection;
	}
	

int MenuProcessing(int MenuSize,int MenuSelection){
		int b=1;
		if(MenuSelection>MenuSize){
			cerr<<"You've entered an invalid option!"<<endl
				<<"Try again!."<<endl;
			Sleep(2000);
			system("cls");
			return 0;
		}

		while(MenuSize>b){
			if(MenuSelection=b){
			}

			}

			b++;
		}


int main(){
	start:
	int MenuSelection=0,EventStatis;
	about();
	MenuSelection=MenuOne();
	EventStatis=MenuProcessing(6,MenuSelection);
	if(EventStatis=0){
		goto start;
	}
    return 0;
}

so thats my code right.
where it says the function menuprocessing. I need a menuprocessing thingo cause i using lots and lots of menus.
so what i want is for where the

while(MenuSize>b){
			if(MenuSelection=b){
			}

			}

is i want it to send it to the approbate selected menu thing.
so something like a function array....

void MenuOne[1](){
}

so when

MenuMelection=b

it would go to the

MenuOne[b]();

sucks that function arrays don't exist.

how would i go about doing something like that?

Dani AI

Generated

A compact, maintainable pattern is to make the menu data-driven: put all handlers into a table (callable objects) and dispatch by index after validating input. This scales far better than a huge switch and avoids goto-based loops. As noted, a switch is fine for a couple of options; for many menus a handler table keeps code tidy. Also heed 's point to replace goto with a loop, and 's warning about using == (not =) in conditions.

Example (modern C++): a vector of callables with safe input parsing and a simple loop.

#include <iostream>
#include <vector>
#include <functional>
#include <string>

int main() {
    bool running = true;

    auto createExpense = []{ std::cout << "Create expense\n"; };
    auto editExpense   = []{ std::cout << "Edit expense\n"; };
    auto viewExpense   = []{ std::cout << "View expense\n"; };
    auto settings      = []{ std::cout << "Settings\n"; };
    auto about         = []{ std::cout << "About\n"; };

    std::vector<std::function<void()>> handlers = {
        createExpense, editExpense, viewExpense, settings, about,
        [&running]{ running = false; } // Quit handler
    };

    while (running) {
        std::cout << "Enter choice (1-" << handlers.size() << "): ";
        std::string line;
        if (!std::getline(std::cin, line)) break;
        int choice = -1;
        try { choice = std::stoi(line); }
        catch (...) { std::cout << "Please enter a number\n"; continue; }

        if (choice < 1 || choice > (int)handlers.size()) {
            std::cout << "Invalid option\n";
            continue;
        }
        handlers[choice - 1]();
    }
}

If the toolchain predates C++11, use a C-style function-pointer array:

typedef void (*Handler)();
Handler handlers[] = { createExpenseFunc, editFunc, viewFunc, settingsFunc, aboutFunc, quitFunc };
size_t n = sizeof(handlers) / sizeof(handlers[0]);
if (choice >= 1 && choice <= (int)n) handlers[choice-1]();

Troubleshooting tips: always validate and parse input with getline+stoi (clears bad input), check bounds before indexing, avoid system("cls")/Sleep if portability matters (prefer std::this_thread::sleep_for and platform-aware screen handling), and prefer returning a boolean or setting a flag for quitting instead of goto. This yields clearer, safer menu dispatching and makes adding/removing options trivial.

Recommended Answers

All 4 Replies

use a switch statement, then call the function you are after.

switch(b){
case 1:
     menu1();
     break;
case 2:
     menu2();
     break;
case 3:
     menu3();
     break;
default:
     std::cout << "Invalid Choice!";
     break;
}

I think that is what you are after,
Chris

Use a while loop instead of goto start.

Not to keep harping on this, but you really need to be careful about the difference between an assignment (=) and a comparison (==).

This means that you are assigning the value of the variable b to that of the variable MenuSelection.

if(MenuSelection=b){

This means that you are comparing the variables MenuSelection and b

if(MenuSelection == b){
commented: Whoo! =) +4

Lol oops thats bout the ==
Rofl umm forgot that thats a true and false and the other is a assignment was =.
Ty

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.