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

Hello, I am in need of a bit of guidance on a C++ question. The given codes has

ID: 3741391 • Letter: H

Question

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 <string>

#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 <iostream>

#include <cstdlib>

#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 <iostream>

#include <cstdlib>

#include <conio.h>

#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.

Explanation / Answer

//Menu.h

#include <string>
#ifndef MENU
#define MENU
const int MAXCOUNT = 20;
struct menuItem
{
void (*func) ();
string descript;
};
class Menu
{
private:
menuItem mi[MAXCOUNT];
int count;
void runSelection();
public:
Menu();
void addMenu(string Description, void(*f) ());
void runMenu();
};

#endif

//Menu.cpp
#include <iostream>
#include <cstdlib>
#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();
}

//main.cpp

#include <iostream>
#include <cstdlib>
#include <conio.h>
#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);
}

CHANGES:
Change char* to string
char[] to string
to copy string use operator =. We do not need any function like strcpy

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