Funtion displays nothing

I made a function to display a menu of options for a player to choose from in a console RPG, but for some reason when it calls the function in the code, the screen clears and nothing happens:

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
int menu(string option_1, string option_2, string option_3) {
	bool been_chosen;
	action = 1;

	while (been_chosen == false) {
		system("CLS");
		if (cin.get() == 72){
			if (action > 0) {
				action++;
			}
		}
		else if (cin.get() == 80) {
			if (action < 3) {
				action--;
			}
		}
		switch (action) {
		case 1:
			cout << "---------------------------------";
			cout << "-> " << option_1;
			cout << "   " << option_2;
			cout << "   " << option_3;
			cout << "---------------------------------";
		case 2:
			cout << "---------------------------------";
			cout << "   " << option_1;
			cout << "-> " << option_2;
			cout << "   " << option_3;
			cout << "---------------------------------";
		case 3:
			cout << "---------------------------------";
			cout << "   " << option_1;
			cout << "   " << option_2;
			cout << "-> " << option_3;
			cout << "---------------------------------";
		}
		if (cin.get() == '\n') {
			been_chosen = true;
		}
	}

	return 0;
}
Hi,

been_chosen is un-initialised - it could go either way. Always initialise your variables to something.

Btw:

1
2
3
4
bool been_chosen = false;
	action = 1;

	while (!been_chosen) {
AH! Thank you that might help let me try it
That did not work. For extra information, I will also display the main loop.
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
int main() {
	Person randall;
	Person dante;
	Person taylor;
	Person ezio;
	Person lance;

	is_player = true;
	Person player;

	player.stealth = 0;
	player.strength = 0;
	player.charisma = 0;
	player.constitution = 0;
	player.magic = 0;
	player.health = 10;

	system("CLS");
	cout << "Now you will create your stats. You have 30 points to spend on different skills." << endl;
	Console::ReadKey();
	system("CLS");

	int total_points;
	int points_left;

	while (player.stealth + player.strength + player.charisma + player.constitution + player.magic > 30 || player.stealth + player.strength + player.charisma + player.constitution + player.magic < 10) {
		//while statement controls point count
		system("CLS");
		total_points = player.stealth + player.strength + player.charisma + player.constitution + player.magic;
		points_left = 30 - total_points;
		cout << "Choose your stealth: ";
		cin >> player.stealth;
		system("CLS");
		total_points = player.stealth + player.strength + player.charisma + player.constitution + player.magic;
		points_left = 30 - total_points;
		cout << points_left << " points left.";
		Console::ReadKey();
		system("CLS");
		cout << "Choose your magic: ";
		cin >> player.magic;
		total_points = player.stealth + player.strength + player.charisma + player.constitution + player.magic;
		points_left = 30 - total_points;
		cout << points_left << " points left.";
		Console::ReadKey();
		system("CLS");
		cout << "Choose your strength: ";
		cin >> player.strength;
		total_points = player.stealth + player.strength + player.charisma + player.constitution + player.magic;
		points_left = 30 - total_points;
		cout << points_left << " points left.";
		Console::ReadKey();
		system("CLS");
		cout << "Choose your constitution (constitution is your overall health, like how you react to potions and stuff.): ";
		cin >> player.constitution;
		total_points = player.stealth + player.strength + player.charisma + player.constitution + player.magic;
		points_left = 30 - total_points;
		cout << points_left << " points left.";
		Console::ReadKey();
		system("CLS");
		cout << "Choose your charisma: ";
		cin >> player.charisma;
		total_points = player.stealth + player.strength + player.charisma + player.constitution + player.magic;
		points_left = 30 - total_points;
		cout << points_left << " points left.";
		Console::ReadKey();

		if (total_points > 30) {
			system("CLS");
			cout << "Too many points used. Please retry.";
			Console::ReadKey();
		}
		if (total_points < 15) {
			system("CLS");
			cout << "Well you have to give yourself more points than that.";
			Console::ReadKey();
		}
	}

	//Now starts the story:
	system("CLS");
	cout << "The Six Stones";
	cout << "Press any key to continue";
	Console::ReadKey();
	system("CLS");
	cout << "A Medevil RPG";
	Console::ReadKey();
	system("CLS");
	cout << "You will take on the role of the protaginist and make decisions which determine the story";
	Console::ReadKey();
	system("ClS");
	cout << "Your adventure starts here...";
	Console::ReadKey();
	system("CLS");
	cout << "You wake up on a boat dazed, you rise from the box you have slept behind and here yelling. Walking out, you walk into the harbor that the boat docked in, and you recognize the city.";
	Console::ReadKey();
	system("CLS");
	cout << "You have just reached the great city of Ragni!";
	Console::ReadKey();
	system("CLS");
	cout << "[Josiah]: Dang! I gotta get out of here fast! *Runs out of boat*" << endl << endl;
	Console::ReadKey();
	cout << "[Guard]: HEY! I DIDN'T SEE YOU ON THE BOAT! GIVE ME PROOF THAT YOU ARE NOT A STOWAWAY" << endl << endl;
	Console::ReadKey();
	cout << "--------------------------------" << endl;
	cout << "What do you say?";
	Console::ReadKey();
	menu("Engage in a fight", "Convince him to overlook this", "Run");
	return 0;
}

int menu(string option_1, string option_2, string option_3) {
	bool been_chosen;
	action = 1;

	while (been_chosen == false) {
		system("CLS");
		if (cin.get() == 72){
			if (action > 0) {
				action++;
			}
		}
		else if (cin.get() == 80) {
			if (action < 3) {
				action--;
			}
		}
		switch (action) {
		case 1:
			cout << "---------------------------------";
			cout << "-> " << option_1;
			cout << "   " << option_2;
			cout << "   " << option_3;
			cout << "---------------------------------";
		case 2:
			cout << "---------------------------------";
			cout << "   " << option_1;
			cout << "-> " << option_2;
			cout << "   " << option_3;
			cout << "---------------------------------";
		case 3:
			cout << "---------------------------------";
			cout << "   " << option_1;
			cout << "   " << option_2;
			cout << "-> " << option_3;
			cout << "---------------------------------";
		}
		if (cin.get() == '\n') {
			been_chosen = true;
		}
	}

	return 0;
}
Most of it is just dialogue, but I hope this helps with fixing the problem. You should try running the program just to see how it works
Last edited on
Hi,

You haven't changed the code :+)

You should try running the program just to see how it works


It doesn't look like standard C++ Console::ReadKey(); Unless you have your own classes with those names. Is it .NET or C++/CLI ?

There are no includes.

There is a lot of repetition in the code - normally there is a better way. Use functions more.

It's late at my end, I might not reply for another 12 hours :+)
You should try running the program just to see how it works


How ? The code you showed is not enough to compile it.
Like I said, I am using Visual Studio. If you have Visual Studio, then you would understand, and it IS possible. In visual studio, you can run it without compiling it
closed account (48T7M4Gy)
It is NOT possible to compile the code you have posted here. It doesn't take a genius to realise it won't compile - what is Person? what is Console? just for a start. There is no magic with VS that will make your post run.

And you can try it yourself. Press the wheel in the top RH corner and all sorts of magical reasons why you are wrong appear on the screen.

The problem you have is you are testing too much code. We try to avoid playing "pick the bug". It sounds like you have an infinite loop, In practical terms what you need to do is track what's happening by inserting a line cout << "got this far" << endl; and progressively work your way through the maze. System CLS is something you don't need in the debug phase so comment it out.

Another way is to 'unit' test the while loop. Make a copy of you work and extract the while loop section and work on that until you get it right or have a workable even though not operational section to post here if you want help.
Topic archived. No new replies allowed.