Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello, I am in need of a bit of guidance on a C++ question. The given codes has elements of Cstring, which our professor wants

Hello, I am in need of a bit of guidance on a C++ question. The given codes has elements of Cstring, which our professor wants to exchange for the string class. I am rather perplexed on how to accomplish this.

Menu.h

#include

#ifndef MENU

#define MENU

const int MAXCOUNT = 20;

struct menuItem

{

void (*func) ();

char descript[50];

};

class Menu

{

private:

menuItem mi[MAXCOUNT];

int count;

void runSelection();

public:

Menu();

void addMenu(char *Description, void(*f) ());

void runMenu();

};

#endif

Menu.cpp

#include

#include

#include "Menu.h"

using namespace std;

Menu::Menu()

:count(0)

{

}

void Menu::addMenu(char *Description, void(*f) (void))

{

if (count < MAXCOUNT)

{

this->mi[count].func = f;

strcpy_s(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();

}

main.cpp

#include

#include

#include

#include "Menu.h"

using namespace std;

void func1();

void func2();

void func3();

void Exit();

void waitKey();

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()

{

char c;

cout << "Hello from function 1.";

cin >> c;

}

void func2()

{

char c;

cout << "Hello from function 2.";

cin >> c;

}

void func3()

{

char c;

cout << "Hello from function 3.";

cin >> c;

}

void Exit()

{

cout << "Goodbye." << endl;

exit(0);

}

void waitKey()

{

cout << "Press any key to continue." << endl;

while (!_kbhit());

fflush(stdin);

}

Here is what our professor is asking for specifically: 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.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design And SQL For DB2

Authors: James Cooper

1st Edition

1583473572, 978-1583473573

More Books

Students also viewed these Databases questions