Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a simple menu class that you will be able to use throughout this semester

ID: 3879382 • Letter: C

Question

Create a simple menu class that you will be able to use throughout this semester. Basically, we are going to create a class called menu. It will have methods addMenuItem and runMenu.

The addMenuItem method is the tricky one because it will take as arguments the name that you want to appear on the menu, and a pointer to a function that you want ran when the menu item is selected.

At this point you are probably a little worried about this but don't be because we are going to do this one together. Although I should also point out that there is a flaw in this menu that I would like for you to fix and something I would like for you to add.

The code in the video shows the use of a CString called descript inside the menu class to hold the description. The video also shows the deprecated function strcpy being used. You could fix this problem by using the new strcpy_s function but I would like for you to remove the CString called descript and replace it with the string class. Don't forget to include string.

In addition, I would like for you to add a function called waitKey. The code for the function is below.:

void waitKey()
{

cout << "Press any key to continue" << endl;
   while (!_kbhit()); // kbhit is depricated. use _kbhit

fflush(stdin);
}


This function should be part of the menu class. NOTE: You must include conio.h for this to work

#include <conio.h>

NOTE:

The above function will not work with any compiler other than Microsoft. If you are using XCode then you are going to have to change the functions slightly:

void waitKey()
{

getchar();

}

This is not ideal but it will work in a pinch.

At the bottom of the functions you create to test your menu class you can then use waitKey to get your code to pause:

void func1()
{
cout << "This is func 1" << endl;
m.waitKey();
}

One last note on this. You need to create the instance of the menu in global scope of main.cpp. I know this is not a good thing but rest assured we will fix this later on.

Explanation / Answer

here is your program : ------------------>>>>>>>>>>>>>>>>>

#include<iostream>
#include<string>

using namespace std;

class MenuItem{
string name;
void (*fun)(void);

public:
  MenuItem(){
   
  }
  
  void addMenuItem(string n,void (*f)(void)){
   this->fun = f;
   name = n;
  }
  
  void runMenu(){
   fun();
  }
};

void WaitKey(void){
cout<<"press any key to continue";
cin.get();
cin.ignore();
}

MenuItem item1;

int main(){
void (*f)(void);
f = &WaitKey;
item1.addMenuItem("Wait",f);
item1.runMenu();

return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote