Question

In: Computer Science

In this assignment you are going to use the menu you created in Assignment 1 to...

In this assignment you are going to use the menu you created in Assignment 1 to test both your Double and Integer classes. You should add functionality to the menu to allow you test the add, sub, mul, div functions for instances of both classes

You are going to have make a modification to your menu class. Currently it uses an array to hold a fixed amount of menu items. While this may be OK for most applications we want our menu to be as flexible as possible. To make this happen I want you to remove the array of menu items and replace it with a vector.

We want all of our code to be as organized as possible. You should have separate header and .cpp files for each class with your main function being in a separate file.

Because the menu is setup to take pointers to void functions you are going to have to create a layer of functionality that sits between the two. This kind of structure can be thought of as a three tier approach.

Here is a simplistic example

void doubleAdd();

int main()
{
    m.addMenu("1. Add Doubles",doubleAdd);
    m.runMenu();
}

void doubleAdd()
{ // Code to add two Double classes here. Should get input from the user.
    waitKey();
}

Main.cpp

#include <iostream>
#include <cstdlib>
#include <conio.h>
#include <string>
#include "Menu.h"

using namespace std;

void func1();
void func2();
void func3();
void Exit();

int main()
{
        Menu m;
        m.addMenu("1. Function1", func1);
        m.addMenu("2. Function2", func2);
        m.addMenu("3. Function3", func3);
        m.addMenu("4. Exit", Exit);
        m.runMenu();
}
void func1()
{
        Menu m;
        cout << "Hello from function 1." << endl;
        m.waitKey();
}
void func2()
{
        Menu m;
        cout << "Hello from function 2." << endl;
        m.waitKey();
}
void func3()
{
        Menu m;
        cout << "Hello from function 3." << endl;
        m.waitKey();
}
void Exit()
{
        Menu m;
        cout << "Goodbye." << endl;
        m.waitKey();
        exit(0);
}

Menu.h

#include <string>
#ifndef MENU
#define MENU

const int MAXCOUNT = 20;
struct menuItem
{
        void (*func) ();
        std::string descript;
};
class Menu
{
private:
        menuItem mi[MAXCOUNT];
        int count;
        void runSelection();
public:
        Menu();
        void addMenu(std::string Description, void (*f)());
        void runMenu();
        void waitKey();
};

#endif

Menu.cpp

#include <iostream>
#include <cstdlib>
#include <conio.h>
#include <string>
#include "Menu.h"

using namespace std;

Menu::Menu()
:count(0)
{

}
void Menu::addMenu(string Description, void (*f)(void))
{
        if (count < MAXCOUNT)
        {
                this->mi[count].func = f;
                this->mi[count].descript = Description;
                count++;
        }
}
void Menu::runMenu()
{
        for (;;)
        {
                system("CLS");
                for (int i = 0; i < count; i++)
                {
                        cout << this->mi[i].descript << endl;
                }
                runSelection();
        }
}
void Menu::runSelection()
{
        int select;
        cin >> select;
        if (select <= count)
                this->mi[select - 1].func();
}
void Menu::waitKey()
{
        cout << "Press any key to continue." << endl;
        while (!_kbhit());
        fflush(stdin);
}

Solutions

Expert Solution

Here is the solution to above problem. In the Menu.h the Mi is changed to vector instead of array. Please find the code below

Menu.h

#include <string>
#include <vector> //import vector library
#ifndef MENU
#define MENU

const int MAXCOUNT = 20;
struct menuItem
{
      
void (*func) ();
std::string descript;
};

class Menu
{
private:
std::vector<menuItem> mi; //create vector<menuItem>
int count;
void runSelection();
public:
Menu();
void addMenu(std::string Description, void (*f)());
void runMenu();
void waitKey();
};

#endif

Menu.cpp

#include <iostream>
#include <cstdlib>
#include <conio.h>
#include <string>
#include "Menu.h"

using namespace std;

Menu::Menu()
:count(0)
{

}
void Menu::addMenu(string Description, void (*f)(void))
{
   //in the add selection the new item needs to create and pushed in the vector
if (count < MAXCOUNT)
{
   //create a temp object and add all details to it
       menuItem m;
       m.func=f;
       m.descript=Description;
       //push the temp object in the vector
       this->mi.push_back(m);
count++;
}
}
void Menu::runMenu()
{
for (;;)
{
system("CLS");
for (int i = 0; i < count; i++)
{
cout << this->mi[i].descript << endl;
}
runSelection();
}
}
void Menu::runSelection()
{
int select;
cin >> select;
if (select <= count)
this->mi[select - 1].func();
}
void Menu::waitKey()
{
cout << "Press any key to continue." << endl;
while (!_kbhit());
fflush(stdin);
}


Related Solutions

Assignment 1 – Writing a Linux Utility Program Instructions For this programming assignment you are going...
Assignment 1 – Writing a Linux Utility Program Instructions For this programming assignment you are going to implement a simple C version of the UNIX cat program called lolcat. The cat program allows you to display the contents of one or more text files. The lolcat program will only display one file. The correct usage of your program should be to execute it on the command line with a single command line argument consisting of the name you want to...
/******************************************************************************* * * Welcome to Assignment 1! In this assignment, you're going to be practicing lots...
/******************************************************************************* * * Welcome to Assignment 1! In this assignment, you're going to be practicing lots * of new JavaScript programming techniques. You'll see comments like this one: * * @param {String} name - the name to greet in the message * * These are specially formatted comments that define parameters to functions, * and tell use the type {String} or {Number}, and also the parameter's name. * Finally, we also explain a bit about how the parameter is used,...
For this assignment, you are going to put your imagination to use. STEP ONE: Please choose...
For this assignment, you are going to put your imagination to use. STEP ONE: Please choose an area to describe. It may be an area you have driven by but do not know what is in the area. It may be in a city, a town, or in the country. You get to choose the area for this assignment. Draw a map of the area. Create a visual image of the area. Include trees, buildings, equipment, roads, parks, bridges, etc....
For this 1-2 page assignment, you are going to watch the video of a speech and...
For this 1-2 page assignment, you are going to watch the video of a speech and write a peer review as if the speaker would get the feedback from you in written form. In the review please address the following aspects of the speech, giving clear examples from the speech and then critique the student’s performance. At least three aspects of delivery At least two aspects of content Was the speech effective? Student Speeches for Analysis
Assignment 3 - Enhanced Employee Hierarchy For this assignment, you are going to enhance the Employee...
Assignment 3 - Enhanced Employee Hierarchy For this assignment, you are going to enhance the Employee Hierarchy that you created in Java in Programming Assignment 2 by adding an interface called Compensation with the following two methods: earnings() - receives no parameters and returns a double. raise(double percent) - receives one parameter which is the percentage of the raise and returns a void. Create the abstract class CompensationModel which implements the Compensation interface. Create the following classes as subclasses of...
Assignment #8: Chi-Square Test of Independence Directions: Use the Crosstabs option in the Descriptives menu to...
Assignment #8: Chi-Square Test of Independence Directions: Use the Crosstabs option in the Descriptives menu to answer the questions based on the following scenario. (Be sure to select Chi-square from the Statistics submenu and Observed, Expected, Row, and Column in the Cells submenu. Assume a level of significance of .05) The school district recently adopted the use of e-textbooks, and the superintendent is interested in determining the level of satisfaction with e-textbooks among students and if there is a relationship...
Assignment #8: Chi-Square Test of Independence Directions: Use the Crosstabs option in the Descriptives menu to...
Assignment #8: Chi-Square Test of Independence Directions: Use the Crosstabs option in the Descriptives menu to answer the questions based on the following scenario. (Be sure to select Chi-square from the Statistics submenu and Observed, Expected, Row, and Column in the Cells submenu. Assume a level of significance of .05) The school district recently adopted the use of e-textbooks, and the superintendent is interested in determining the level of satisfaction with e-textbooks among students and if there is a relationship...
Assignment #8: Chi-Square Test of Independence Directions: Use the Crosstabs option in the Descriptives menu to...
Assignment #8: Chi-Square Test of Independence Directions: Use the Crosstabs option in the Descriptives menu to answer the questions based on the following scenario. (Be sure to select Chi-square from the Statistics submenu and Observed, Expected, Row, and Column in the Cells submenu. Assume a level of significance of .05) The school district recently adopted the use of e-textbooks, and the superintendent is interested in determining the level of satisfaction with e-textbooks among students and if there is a relationship...
Confused about going through this C program. Thank you Program Specifications: *********************************************** ** MAIN MENU **...
Confused about going through this C program. Thank you Program Specifications: *********************************************** ** MAIN MENU ** *********************************************** A) Enter game results B) Current Record (# of wins and # of losses and # of ties) C) Display ALL results from all games WON D) Display ALL results ordered by opponent score from low to high. E) Quit Your program will have a menu similar to the example above. The game results will simply be the score by your team and...
For Part 2 of this assignment, you will use the “Assignment 1 – Linear Kinematics Data”...
For Part 2 of this assignment, you will use the “Assignment 1 – Linear Kinematics Data” excel file. In the data set you are provided with vertical position and time data for a person’s vertical center of mass motion for an unspecified movement task. You will utilize excel in all (well, some…) of its glory to calculate the vertical velocity and vertical acceleration data from the position and time data provided in the excel file. Again you will use the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT