Arrays to Vectors

Hey there,

In order to be as organized as possible, according to my prof., vector is required to become organized. I am having trouble and do not know what my next move should be in my current codes. Any tips on what I should do is greatly appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
======== Menu.h =========
#ifndef MENUjavascript:PostPreview()
#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();
	void waitKey();
};

#endif

========= Menu.cpp =========
#include <iostream>
#include <cstdlib>
#include <conio.h>

using namespace std;

#include "Menu.h"

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

}

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

}

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

void Menu::waitKey()
{
	cout << "Press any key to continue" << endl;
	while (!kbhit());

	fflush(stdin);
}

the things you need to do to start are:


#include<vector>
...

struct menuItem
{
void(*func)(); //what is this for, anyway? can this go away? is it just a string manipulation function that is part of std::string?
string descript;
};

class Menu
{
private:
vector<menuItem> mi;

and down in the constructor for the class you may want to pre-allocate the array to its best-guess max size OR you can let it fill in on the fly with the push-back method. One or the other is needed.

there will be some cleanup of your string class and arrays of course.
strcpy for example just becomes =

I would do it one at a time, first string, then array to vec.
Last edited on
Topic archived. No new replies allowed.