Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7887343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:33:20+00:00 2026-06-03T05:33:20+00:00

I’m trying to implement a menu structure for a little program I’m going to

  • 0

I’m trying to implement a menu structure for a little program I’m going to code.

There’s MenuItem which contains a Label, an associated function, a link to its parent and a vector that contains references to its children. Then there is Menu which is mainly responsible to select a new MenuItem and keeps track of which Item is currently active. Unfortunately when I use MenuItem in Menu and change the current MenuItem to one of its children I cant go further cause the following list of children is empty. Seems like the vector doesn’t get copied right. I tried to implement a copy constructor but that didn’t help. Here is my source as follows.

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

typedef void (*fptr)(int);

using namespace std;

void test(int i)
{
    cout << "test function called with argument: " << i << endl;
}

class MenuItem
{
private:
    vector<MenuItem*> children;
    string label;
    fptr f;
    MenuItem* parent;
public:
    MenuItem(string newLabel, fptr newFunction = NULL):label(newLabel),f(newFunction)
    {
    }

    void addChild(MenuItem& mi)
    {
        mi.parent = this;
        children.push_back(&mi);
    }

    MenuItem* getChild(int i)
    {
        return children[i];
    }

    MenuItem* getParent()
    {
        return parent;
    }

    string getLabel()
    {
        return label;
    }

    int countChildren()
    {
        return children.size();
    }

    void list()
    {
        vector<MenuItem*>::iterator i;
        for(i = children.begin(); i < children.end(); ++i)
        {
            MenuItem* m = *i;
            label = m->label;
            stringstream s;
            s << (i - children.begin());
            cout << s.str() << ": " << label << endl;
        }
    }

    void invoke(int i)
    {
        f(i);
    }
};

class Menu
{
private:
    MenuItem* current;
public:
    Menu(MenuItem* m)
    {
        current = m;
        open(0);
    }

    void open(int i)
    {
        current = current->getChild(i);
        if(current->countChildren() > 0)
        {
            cout << "[" << current->getLabel() << "]" << endl;
            current->list();
        }
        else
            current->invoke(i);
    }

    void back(int i)
    {
        current = current->getParent();
    }
};

int main() {
    MenuItem m1("Root");
        MenuItem m2("List Media");
            MenuItem m6("List Movies",&test);
            MenuItem m7("List Music",&test);
            MenuItem m8("List Games",&test);
            MenuItem m9("List Books",&test);
        MenuItem m3("Find Media");
            MenuItem m10("Find Movies",&test);
                MenuItem m14("Find by title",&test);
                MenuItem m15("Find by genre",&test);
            MenuItem m11("Find Music",&test);
                MenuItem m16("Find by title",&test);
                MenuItem m17("Find by genre",&test);
            MenuItem m12("Find Games",&test);
                MenuItem m18("Find by title",&test);
                MenuItem m19("Find by genre",&test);
            MenuItem m13("Find Books",&test);
                MenuItem m20("Find by title",&test);
                MenuItem m21("Find by genre",&test);
        MenuItem m4("Add Media");
            MenuItem m22("Add Movie",&test);
            MenuItem m23("Add Music",&test);
            MenuItem m24("Add Game",&test);
            MenuItem m25("Add Book",&test);
        MenuItem m5("Delete Media");
            MenuItem m26("Add Movie",&test);
            MenuItem m27("Add Music",&test);
            MenuItem m28("Add Game",&test);
            MenuItem m29("Add Book",&test);

    m1.addChild(m2);
        m2.addChild(m6);
        m2.addChild(m7);
        m2.addChild(m8);
        m2.addChild(m9);
    m1.addChild(m3);
        m3.addChild(m10);
            m10.addChild(m14);
            m10.addChild(m15);
        m3.addChild(m11);
            m11.addChild(m16);
            m11.addChild(m17);
        m3.addChild(m12);
            m12.addChild(m18);
            m12.addChild(m19);
        m3.addChild(m13);
            m13.addChild(m20);
            m13.addChild(m21);
    m1.addChild(m4);
        m4.addChild(m22);
        m4.addChild(m23);
        m4.addChild(m24);
        m4.addChild(m25);
    m1.addChild(m5);
        m5.addChild(m26);
        m5.addChild(m27);
        m5.addChild(m28);
        m5.addChild(m29);

    Menu m(&m1);

    int option;
    while(1){
        cout << "Media Library> ";
        cin >> option;
        m.open(option);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T05:33:22+00:00Added an answer on June 3, 2026 at 5:33 am

    The issue with this seems to be the open(0); in the constructor of Menu. This of course causes you to descend automatically into the “List Media” section, which has no further subsections.

    This should probably be replaced with current->list();

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I have this code to decode numeric html entities to the UTF8 equivalent character.
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.