Calling function inside switch-case, from a different file! How?

Hey! I am new here, so keep in mind that I am also kinda new with the C++ language and therefore need an easy explanation for how to solve my problem!

I am trying to create a console program/game with a menu, were I can see the stats for my "spaceship", or add new ones. Also, if I got the time, I will try to add enemies that I can "fight against", and so forth! The reason why I am telling this, is because you might find that my solution will cause problems later on, if I will add more features like these!

SO, my problem is: How do I call a function, within a function, that are placed in two different source- and class files? This is how it looks like (BOLD text for file names and were the problem occur):

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

(PlayerShip.cpp)

#include "PlayerShip.h"
#include "Menu.h"


PlayerShip::PlayerShip(string inName, int inBullets, int inShield)
{
	name = inName;
	bullets = inBullets;
	shield = inShield;
}

void PlayerShip::setName()
{
	cout << "Enter name here: ";
	cin >> name;
}
string PlayerShip::getName()
{
	return name;
}

void PlayerShip::displayInfo()
{
	cout << "Here is your name: " << name << endl;
	cout << "Here is your ammo: " << bullets << endl;
	cout << "Here is your shield: " << shield << endl;
}

PlayerShip::~PlayerShip()
{
	cout << "Goodbye!" << endl;
}

(PlayerShip.h)

#include <iostream>
#include <string>

using namespace std;

#pragma once

class PlayerShip
{

private:
	string name;
	int bullets;
	int shield;

public:
	PlayerShip(string inName, int bullets, int shield);
	~PlayerShip();

	void setName();
	string getName();
	void displayInfo();

};

(Menu.cpp)

#include "Menu.h"
#include "PlayerShip.h"
#include <iostream>
#include <string>

Menu::Menu()
{
	do
	{
		cout << "|-----MENU-----|" << endl;
		cout << "1.Info about your ship" << endl;
		cout << "2. View all the fighters!" << endl;
		cout << "3. Exit the game!" << endl;
		cin >> choice;

		switch (choice)
		{

		case 1:
		{
				  WHAT DO I PUT IN HERE TO DISPLAY "displayInfo();"?
				  break;
		}
		case 2:
		{
				  cout << "You picked 2" << endl;
				  break;
		}
		case 3:
		{
				  cout << "You picked 3" << endl;
				  break;
		}

		}

	} while (choice < 4);
}

Menu::~Menu()
{
}


(Menu.h)

#include <iostream>
#include <string>
#include "PlayerShip.h"

using namespace std;

#pragma once

class Menu
{

private:
	int choice;

public:
	Menu();
	~Menu();

	void displayMenu();
	void displayInfo();

};

(Main.cpp)

#include <iostream>
#include <string>
#include "PlayerShip.h"
#include "Menu.h"

using namespace std;

int main()
{
	cout << "Welcome to Space Fighter XL!" << endl;

	string newName;
	cout << "Enter the name of your mothership here: ";
	cin >> newName;

	PlayerShip obj1(newName, 20, 100);

	Menu myMenu;
	myMenu;

	system("pause");
	return 0;
}




Hope you can help me and thanks in advance!
Last edited on
hiya,
you main issue (i think) is on line 152 you create a PlayerShip object and then do nothing with it.
If you wanted to access you ship from other parts of the program (which is what I think you're asking?), then you could consider perhaps making the Menu constructor take a PlayerShip object.

however i think you need to look at the design a bit. a menu object would not in theory need knowledge of the ship object.

edit: i'll knock something up for you if you want.



okay, like this:

you main might look like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main()
{
	cout << "Welcome to Space Fighter XL!" << endl;

	string newName;
	cout << "Enter the name of your mothership here: ";
	cin >> newName;

	PlayerShip myShip(newName, 20, 100);

	Menu myMenu(myShip);

	return 0;
}


menu.cpp
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
Menu::Menu(PlayerShip playerShip) 
	: m_playerShip(playerShip)
{
	do
	{
		cout << "|-----MENU-----|" << endl;
		cout << "1.Info about your ship" << endl;
		cout << "2. View all the fighters!" << endl;
		cout << "3. Exit the game!" << endl;
		cin >> choice;
		
		switch (choice)
		{

			case 1:
			{
					// now you are free to get at the ship's data inside the menu object,
				    // but this doesn't seem right for a menu object to 'contain' a PlayerShip object, for example:
					m_playerShip.displayInfo();
					m_playerShip.getName();
                       
					break;
			}
			case 2:
			{
					  cout << "You picked 2" << endl;
					  break;
			}
			case 3:
			{
					  cout << "You picked 3" << endl;
					  break;
			}

		}

	} while (choice < 4);
}

Menu::~Menu()
{
}


menu.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Menu
{

private:
	int choice;
	PlayerShip m_playerShip;

public:
	Menu(PlayerShip pShip);
	~Menu();

	void displayMenu();
	void displayInfo();

};


BUT, as i've said in the comments. this allows you to get hold of the ship's data in your menu class, but from a OO point of view it makes no sense whatsoever for a menu class to contain a PlayerShip class.
hope that helps a bit. sorry if i've misinterpreted you.
Last edited on
Thank you mutexe, I will try this!

Yeah, the design might be kinda bad, so you might have a better option? For easier tasks I would use a case-switch menu, at all times, because I think they are super easy and functional. BUT now when I am working with classes and more advanced programming, another option would suit better?

Do you have another/better option to recommend? How do others create a menu, when working with several files and classes?
Topic archived. No new replies allowed.