File includes

Hello everyone and thank you for reading.

I'm currently making a text based game, and I'm trying to make the different location menus. I have tried many different things to be able to call the different menus from different files. Previously you could not get out of one of the locations because you could not call the Travel(); function because it was below the program(I assume). I have tried organizing everything into different classes and I am still recieving the same errors with reading the different functions. The code that I will post below has been very modified since the start of trying to fix it.
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
class Travel
{
public:
	void TravelMenu()
	{
		LocationCalls LocationCalls;
		int PlayerResponse1;
		cout <<"What location are you travelling to?\n1.Castle\n2.Mountain" << endl;
		cin >> PlayerResponse1;
			switch(PlayerResponse1)
			{
			case 1:
				cout << "You have selected the castle!" << endl;
				LocationCalls.CastleMenuCall();
				break;
			case 2:
				cout << "You have chosen the Mountain!" << endl;
				LocationCalls.MountainMenuCall();
				break;
			}
	}
};


class Locations
{
public:
void CastleMenu()
{
	TravelMenuCalls TravelMenuCalls;
	int PlayerAction;
	int PlayerAction1;
	int PlayerAction2;
	do{
	cout << "You are standing in the city square. What do you want to do?" << endl;
	cout << "1.Craft your items\n2.Smelt items at the forge\n3.Leave" << endl;
	cin >> PlayerAction;
		switch(PlayerAction)
		{
			case 1:
				cout << "You open up your bag and grab your crafting tools. What would you like to craft?" << endl;
				cout << "1.Convert Lumber to Planks\n2.Copper Sword\n3.Bronze Sword" << endl;
				cout << "4. Iron Sword" << endl;
				cin >> PlayerAction1;
					switch(PlayerAction1)
					{
					case 1:
						if(Log.count>0)
						{
						cout << "You have converted one log into three planks of wood." << endl;
						Log.count-=1;
						Planks.count+=3;
						}
						else
						{
						cout << "You do not have enough logs." << endl;
						}
						break;
					case 2:
						if(Copper.count = 25 || Copper.count > 25)
							{
								cout << "You have created a Copper sword." << endl;
								Copper_Sword.count+=1;
								Copper.count-=25;
							}
							else
							{
							cout << "You do not have enough Copper." << endl;
							}
							break;
					case 3:
						if(Bronze.count = 25 || Bronze.count > 25)
						{
						cout << "You have created a Bronze sword." << endl;
						Bronze_Sword.count+=1;
						Bronze.count-=25;
						}
						else
						{
						cout << "You do not have enough Bronze." << endl;
						}
						break;
			}
				case 2:
					cout << "You enter the forge to smelt raw metals." << endl;
					cout << "What would you like to do?\n1.Smelt\n2.Leave" << endl;
					cin >>  PlayerAction1;
						switch(PlayerAction1)
							{
							case 1:
								cout << "What would you like to smelt?" << endl;
								cout << "1.Tin\n2.Copper\n3.Iron\n4.Steel" << endl;
									cin >> PlayerAction2;
										if(PlayerAction2 = 1 || Tin_Ore.count >=10)
											{
											cout << "You have smelted ten Tin nuggets into a Tin bar." << endl;
											Tin_Ore.count-=10;
											Tin.count+=1;
											}
										else if(PlayerAction2 = 2 || Copper_Ore.count >=10)
											{
											cout << "You have smelted ten Copper nuggets into a Copper bar." << endl;
											Copper_Ore.count-=10;
											Copper.count+=1;
											}
										else if(PlayerAction2 = 3 || Iron_Ore.count >=10)
											{
											cout << "You have smelted ten Iron nuggets into an Iron bar." << endl;
											Iron_Ore.count-=10;
											Iron.count+=1;
											}
										else if(PlayerAction2 = 4)
										{
											if(Iron_Ore.count = 10 || Iron_Ore.count > 10)
											{
												if(Fuel.count>=5)
												{
											cout << "You have smelted ten Iron nuggets and five fuel units into a Steel bar." << endl;
											Iron_Ore.count-=10;
											Fuel.count-=5;
											Steel.count+=1;
												}
												else
												{
												cout << "You do not have enough fuel." << endl;
												}
											}
											else
											{
											cout << "You do not have enough Iron." << endl;
											}
										}
										else
											{
											cout << "You did not submit a correct number to perform an action." << endl;
											}
									break;
							}
		}
	}while (PlayerAction!=3);
}

void MountainMenu()
{
	LocationCalls LocationCalls;
	int PlayerAction;
	int PlayerAction1;
 cout << "What would you like to do?" << endl;
 cout << "1.Mine\n2.Leave" << endl;
 cin >> PlayerAction;
	switch (PlayerAction)
	{
	case 1:
		cout << "What mineral would you like to mine." << endl;
		cout << "1.Iron\n2.Copper\n3.Tin\n4.Fuel" << endl;
			cin >> PlayerAction1;
				switch (PlayerAction1)
				{
				case 1:
					cout << "You begin to mine for Iron." << endl;
					cout << "You have found a chunk of Iron ore." << endl;
					Iron_Ore.count+=1;
					break;
				case 2:
					cout << "You begin to mine for Copper." << endl;
					cout << "You have found a chunk of Copper ore." << endl;
					Copper_Ore.count+=1;
					break;
				case 3:
					cout << "You begin to mine for Tin." << endl;
					cout << "You have found a chunk of Tin ore." << endl;
					Tin_Ore.count+=1;
					break;
				case 4:
					cout << "You have begin to mine for fuel." << endl;
					cout << "You have found a chunk of fuel." << endl;
					Fuel.count+=1;
					break;
				}
	break;
	case 2:
		cout << "You have decided to leave the mountain." << endl;
		TravelMenuCalls.TravelMenuCall();
	default:
		cout << "That is not a listed function, please type the number that corresponds to the action." <<endl;
	break;
	}
}
};


This is the coding for the CastleMenu(); and MountainMenu();. These locations are the only that I have made progress on so far. The error here is that I cannot seem to call the TravelMenu(); function if the CastleMenu(); or MountainMenu(); functions are called from the TravelMenu(); To call all of the functions equally I have tried to make seperate classes that contain functions that only call for the CastleMenu(); and MountainMenu(); The coding for that is here:
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
class LocationCalls
{
public:
void CastleMenuCall()
{

	Locations Locations;
	Locations.CastleMenu();
}

void MountainMenuCall()
{

	Locations Locations;
	Locations.MountainMenu();
}
};

class TravelMenuCalls
{
public:
void TravelMenuCall()
{
	Travel Travel;
	Travel.TravelMenu();
}
};


I don't believe that I am doing this at all right and I'm calling out for help. If anyone has any ideas or comments please post I'd love to get some feedback.
Thanks for reading.
Your functions aren't static, so I'm assuming you've created an object for each class right? If not, this would be your problem.

Once you define a class, it becomes a data type (not exactly, but it is easier to grasp this) just like int's and char's. You need to declare an object of the class that you want to use:
1
2
3
4
classname objname; //creates object of this class

//access "members" like this
objname.membername; //this is the same for functions 


EDIT: ignore this post, I didn't look at your code thoroughly enough
Last edited on
I don't fully understand what you mean although I think I get the concept. I don't understand what you mean by creating an object and then calling the member. If I'm correct I think in your explanation the member is the function but what kind of object. What sort of object would I create to access the functions using this?
As for having multiple files... You would need to change some stuff.

First, you should declare your functions inside the class, then define them in a separate file.

class.h (header file, contains declarations only)
1
2
3
4
5
6
7
8
9
10
#ifndef CLASS_H
#define CLASS H          //include guard, prevents multiple definition errors

class myclass {
    public:
    int myint;
    void myfunc(); //declaration only
};

#endif 


class.cpp (implementation file, definitions)
1
2
3
void myclass::myfunc(){
    //define the function...
}


main.cpp (the "calling" file)
1
2
3
4
5
6
7
8
9
#include <iostream>
#include "class.h"

using namespace std;

int main(){
    myclass obj; //create object
    obj.myfunc(); //call function
    return 0;}


Just apply these concepts to your classes
So when I am trying to include multiple files how would I include multiple .cpp files? Would i include them through main or a header that is included into main?
It seems you're misusing classes as namespaces.
If you just want to group several related functions together, you should use namespaces.
You also confused = (assignment) with == (test for equality) throughout your program.
I'm pretty new to programming so I don't fully understand everything about more advanced things like namespaces yet. I had only used classes to try and call the functions throughout different files.
The functions can be global and still be used from other files. There is no need for classes to do this. You just need to forward declare them at the top of all .cpp files using them. This is done most easily with header files.

header.h (contains the declaration)
1
2
3
4
5
6
7
#ifndef HEADER_H  //include guard
#define HEADER_H //this is so if this file is included 2+ times, func() won't be
//                              declared every time it is included

void func(); //forward declaration

#endif 


func.cpp (defines our function)
1
2
3
void func(){
//code here...
}


main.cpp (contains our main function)
1
2
3
4
5
6
7
8
9
#include <iostream>
#include "header.h" //this essentially copies and pastes the contents
//                              of header.h at this line

using namespace std;

int main(){
    func(); //calls our function...duh
    return 0;}


Easy peasy. A header file contains the declarations and is included by the .cpp files using the functions it declares. The implementation .cpp file contains the definitions for these functions. The other .cpp file includes the header file to get the declarations, and therefore, access to the functions in the implementation file. This can be done with hundreds of files as well, not just 3.
Last edited on
Thank you so much for that explanation I understand including the files now. Looking at the code pasted above do you see any way that I should rewrite this for it to be correct? I can't access the opposite function in trying to travel. It's a case of the chicken and the egg.
It doesn't appear that you need classes, or even namespaces really. Just make all of your functions global, and put them in separate files based on what they do (ex. MountainMenu() would go in mountains.cpp, etc). Then, you can create one big header file to have all the function declarations, or separate the headers based on type as well (mountains.h, etc). Then have a main.cpp file that contains int main() and includes your headers. From there, use main to call the other functions as necessary.

For your variables however, you would be better off with classes. Try this:
1
2
3
4
5
6
7
8
9
10
11
class resources {
    public:
    int money;
    int iron, stone, gold, shiny_thing;
}player_resources; //creates an "object" called player_resources

//an object is pretty much a group of related variables and functions that exist "inside" of the
 //"object"

//access like this:
player_resources.money += sellamount;


The C++ Language Tutorial in the Documentation section of this site has 2 great sections on classes as well.
Ok. Thank you guys for all of your help I'm gonna set the thread to solved. Thanks, I'm going to look around for any problems other people are having, maybe I can help around a little too.
Topic archived. No new replies allowed.