Hello,

i need to create a binary tree menu such as,

Sports Clothing Menu:

1 Soccer
2 Rugby
3 Swimming
4 Add a new category
5 Add a Selling item

6 Quit to main menu

Could somebody please steer me in the right direction of how i can implement this structure?

Much appricated if somebody can generally tell me how to do this

Thankyou very much!

Sure. First realize that Menu's can be recursive, that is you can have menues inside menus. For example, you can have a top level menu called, Clothes which contains sub-menus like sports clothes or winter clothes. Noticing this recursive structure, might hint that a tree implementation might be a natural choice. Noting the above, here is some idea to go by.

struct Menu{
 std::string name; //could be menu name or a item name
 std::vector<Menu> subMenus;
Menu(const std::string& name, const std::vector<Menu> subMenus): name(name), subMenus(subMenus){}
 void add(const Menu& m){ subMenus.push_back(m); }
};

class SportsClothingMenu{
private:
 Menu sportsClothingMenu;
public:
 SportsClothingMenu(){}
private:
 void _populate(){
        Menu soccerClothesMenu("soccer");
        //...add soccor clothes to soccerClothesMenu
        sportsClothingMenu.add( soccerClothesMenu );
  }
}

Note that we know a menu is a leaf if it has no children, so in the above case if subMenu.empty() is true. You should be able to take it from there. There is no real need to use pointers here. Using std::vector will suffice.

commented: Ur a Champion +0

Wow thankyou very much for your quick response, ill keep you's updated to see how i went . :)

Just an update, im just trying to adjust everything in each own file although its still not working just to test once i compile i still get error

In File included from Main.cpp

This is the code

Main.cpp

//main.cpp

#include <iostream>
#include "Menu.h"
  
using namespace std;

int main(){
{
    SportsClothingMenu();
}
 
system("pause");
return 0;
}

Menu.h

//menu.h

#ifndef menu_h
#define menu_h

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

using namespaced std;


class SportsClothingMenu
{
private:
          Menu sportsClothingMenu;
          
public:
          SportsClothingMenu(){}
     
private:
          void _populate(){
               
                Menu soccerClothesMenu("soccer");
                //...add soccor clothes to soccerClothesMenu
                sportsClothingMenu.add( soccerClothesMenu );
  }
}

#endif

Menulmp.cpp

//menulmp.cpp

#include "Menu.h"
using namespace std;

struct Menu
{
    std::string name; //could be menu name or a item name

    std::vector<Menu> subMenus;
    
    Menu(const std::string& name, const std::vector<Menu> subMenus): name(name), subMenus(subMenus){}
    
    void add(const Menu& m){ subMenus.push_back(m); 
    
   }
};

I cant work out what is so wrong about the linking between the headers and the cpp :(

Hi, firstly you are forgetting a ; at the end of your header file.

Hi, firstly you are forgetting a ; at the end of your header file.

can you be more specific on where abouts on that header file?, sorry

can you be more specific on where abouts on that header file?, sorry

Hi, the semi colon will need to be at the end of your class declaration. So, after the final }.

It is exactly like the struct you just declared. Both structs and classes are basically the same thing.

Hi, the semi colon will need to be at the end of your class declaration. So, after the final }.

It is exactly like the struct you just declared. Both structs and classes are basically the same thing.

ok thankyou

im still getting that link error " in file included from main.cpp :(

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.