Question: Hello, I am needing to exhange the array in this program with a vector. The array can be found declared in menu.h, and used in
Hello, I am needing to exhange the array in this program with a vector. The array can be found declared in menu.h, and used in menu.cpp.
menu.h
#ifndef MENU
#define MENU
#include
#include
#include
const int MAX_COUNT = 20;
//struct
struct menuItem
{
void(*func)();
std::string description;
};
class Menu
{
private: //data section
menuItem mi[MAX_COUNT];
int count;
void runSelection();
public:
//constructors
Menu() : count(0) {};
//mutators
void addMenu(std::string description, void(*f)(void));
//accessors
void runMenu();
void waitKey();
};
#endif // !MENU
menu.cpp
#include "Menu.h"
#include
#include
void Menu::runSelection()
{
int select;
std::cin >> select;
if (select <= this->count)
this->mi[select - 1].func();
}
void Menu::addMenu(std::string description, void(*f)(void))
{
if (this->count < MAX_COUNT)
{
this->mi[count].func = f;
this->mi[count].description = description;
count++;
}
}
void Menu::runMenu()
{
for (;;)
{
system("CLS"); //clear the screen
for (unsigned int pos = 0; pos < this->count; pos++)
std::cout << this->mi[pos].description << std::endl;
runSelection();
}
}
void Menu::waitKey()
{
std::cout << "Press any key to continue" << std::endl;
while (!_kbhit());
std::fflush(stdin);
}
main.cpp
#include
#include
#include "Menu.h"
//global variables
Menu m;
//function prototypes
void func1();
void func2();
void func3();
void Exit();
int main()
{
m.addMenu("1. Function 1", func1);
m.addMenu("1. Function 2", func2);
m.addMenu("1. Function 3", func3);
m.addMenu("1. Exit", Exit);
m.runMenu();
return 0;
}
void func1()
{
std::cout << "This is function 1" << std::endl;
m.waitKey();
}
void func2()
{
std::cout << "This is function 2" << std::endl;
m.waitKey();
}
void func3()
{
std::cout << "This is function 3" << std::endl;
m.waitKey();
}
void Exit()
{
std::cout << "Good-Bye" << std::endl;
exit(0);
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
