Menu Creation with diffferent files

I'm basically doing a kinda "large" project on c++, this one here: http://www.gamedev.net/topic/409907-c-workshop---project-1/

For now I've been doing a class that creates and shows the menu, I've decided to do it like this because I really think it looks better than coding every menu for separate.

So, if I put it all on main.cpp and on the main() function I just call add_value and put a string with words, the menu is created and it work just fine.

What I want to do is to separate this class into a different file, basically I want to have different headers files and cpp files, but I've NEVER done it before so I don't know what do I have to put in each file... here is the main code

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
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
using namespace std;

class Menu
{
public:

private:
    void add_values(std::string line);
    void create_menu(std::vector<std::string> table);
};



void create_menu(std::vector<std::string> table)
{
    for (int i = 0; i < table.size(); i++)
    {
        if (i == 0)
        {
            cout<< "\n================ "<< table[0]<< " ================";
        }
        else

        cout<< "\n"<<i<<". "<<table[i];
    }
    cout<< "\n================ "<< table[0]<< " ================";
}
void add_values(std::string line)
{

	std::stringstream  lineStream(line);

	std::string cell("");

	// collect all the strings
	std::vector<std::string> result;
	while (std::getline(lineStream, cell, ','))
	{
		result.push_back(cell);
	}

	create_menu(result);
}


int main()
{
    std::string first_menu ("MAIN MENU,Create Character,Buy Equipment,Fight!,Quit");
    std::string Create_character ("CREATE CHARACTER,Name,EyeColor,Height,Strength");
    add_values(first_menu);
    add_values(Create_character);
}


Thanks!
it work just fine.

It's not working as you think it is.
By this i mean, if you delete your class definition it'll still build and run 'just fine'. You are not using your class at all. the function implementations are stand-alone functions.

you have:
1
2
3
4
void create_menu(std::vector<std::string> table)
{
...
}


but you need (if you are using a class)
1
2
3
4
void Menu::create_menu(std::vector<std::string> table)
{
...
}


Then in main() do something like this:

1
2
3
4
5
6
7
8
9
int main()
{
    std::string first_menu ("MAIN MENU,Create Character,Buy Equipment,Fight!,Quit");
    std::string Create_character ("CREATE CHARACTER,Name,EyeColor,Height,Strength");
    
    Menu menu;
    menu.add_values(first_menu);
    menu.add_values(Create_character);
}

Last edited on
What did I do wrong now? :'(

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
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
using namespace std;

class Menu
{
public:

private:
    void add_values(std::string line);
    void create_menu(std::vector<std::string> table);
};



void Menu::create_menu(std::vector<std::string> table)
{
    for (int i = 0; i < table.size(); i++)
    {
        if (i == 0)
        {
            cout<< "\n================ "<< table[0]<< " ================";
        }
        else

        cout<< "\n"<<i<<". "<<table[i];
    }
    cout<< "\n================ "<< table[0]<< " ================";
}
void Menu::add_values(std::string line)
{

	std::stringstream  lineStream(line);

	std::string cell("");

	// collect all the strings
	std::vector<std::string> result;
	while (std::getline(lineStream, cell, ','))
	{
		result.push_back(cell);
	}

	Menu::create_menu(result);
}


int main()
{
    std::string first_menu ("MAIN MENU,Create Character,Buy Equipment,Fight!,Quit");
    std::string Create_character ("CREATE CHARACTER,Name,EyeColor,Height,Strength");
    Menu main_menu;
    Menu create_character_menu;
    main_menu.add_values(first_menu);
    create_character_menu.add_values(Create_character);
}
What is your compiler telling you?
(and then look at where in your class you have declared your functions)

edit:
just noticed this:
Menu::create_menu(result); on line 46 should just be
create_menu(result);

Last edited on
Just make your add_values and create_menu to public instead of private.
lol. i guess my hint in the right direction wasn't needed :)
k, now it works! But my main question was how can I separate this into different .cpp and .h? I have never done it, so could someone do it for me so I can do it the next time?

EDIT: I'm using code::blocks
Last edited on
Topic archived. No new replies allowed.