RPG menu screen not working

Hello, I've been working on the tutorial for an RPG for a couple days but when I got to the bit about the menu screen (which i completed the entire section), it wasnt working correctly.
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
#include <iostream>
#include <string>
using namespace std;

bool running = 1;
void titleFunc();
int userInput = 0;
int playerInfo[2];
int playerLocation = 0;
const int MAX_ITEMS = 100;
int playerItems[MAX_ITEMS][10];
string itemNames[] = { "Sword", "Average Clothing", "Rusted Armor" };
bool itemPickedUp2 = 0;
bool itemPickedUp4 = 0;
short playerItemCount = 0;

int main() {
    cout << "\t\t\t\t---Hero of Golar: Part 1---\n\n\n";
    cout << "\t\t\t\t   1: Play\n";
    
    cin >> userInput;
    
    if(userInput == 1){
                  void newGameFunc(); {
                       void menuFunc();{
                           return 0;
                           }
                      void menuFuncNav(); {
                           cout << " 1: Stats\n 2: Items\n";
                           cin >> userInput;
                           
                           if(userInput == 1) void menuFuncInfo();
                           else if(userInput == 2) void menuFuncItems();
                           return 0;
                           }
                      void menuFuncInfo(); {
                           
                           cout << "Gender: \n";
                           if(playerInfo[0] == 1) cout << "Male\n";
                           else if(playerInfo[0] == 2) cout << "Female\n";
                           
                           cout << "Class: \n";
                           if(playerInfo[1] == 1) cout << "Warrior\n";
                           if(playerInfo[1] == 2) cout << "Rogue\n";
                           if(playerInfo[1] == 3) cout << "Mage\n";
                           return 0;
                           }
                      void menuFuncItems(); {
                           for(int i = 0; i < playerItemCount; i++){
                                   cout << i+1 << ": ";
                                   cout << itemNames[playerItems[i][0]];
                                   cout << "\n";
                                   cin >> userInput;
                                   
                                   cout << "\nName: " << itemNames[playerItems[userInput][0]] << "\n";
                                   
                                   if(playerItems[userInput][1] == 1)
                                   cout << "Weapon: " <<playerItems[userInput][2] << "\n";
                                   cout << "Armor: " <<playerItems[userInput][2] << "\n";
                                   cout << "Consumable: " <<playerItems[userInput][2] << "\n";
                                   cout << "Quest: " <<playerItems[userInput][2] << "\n";
                                   cout << "Misc: " <<playerItems[userInput][2] << "\n";
                                   cout << "Press ENTER to continue ";
                                   cin >> userInput;
                                   }
                           return 0;
                           }

I've placed the menu code in different places and whenever I reach the point of where it's at my cmd console closes instantly, and I'm clueless as to why.
I'm assuming something is wrong with my function but I don't know where and why.

Any help would be appreciated, thanks.
You seem to be implying that this code will compile; it doesn't even get that far for me due to mismatched braces (something the indentation should help you notice).

In any case, you are doing something very wrong here; here is your code with extraneous braces removed and a few notes.

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

bool running = 1;
void titleFunc();
int userInput = 0;
int playerInfo[2];
int playerLocation = 0;
const int MAX_ITEMS = 100;
int playerItems[MAX_ITEMS][10];
string itemNames[] = { "Sword", "Average Clothing", "Rusted Armor" };
bool itemPickedUp2 = 0;
bool itemPickedUp4 = 0;
short playerItemCount = 0;

int main() {
	cout << "\t\t\t\t---Hero of Golar: Part 1---\n\n\n";
	cout << "\t\t\t\t   1: Play\n";

	cin >> userInput;

	if(userInput == 1) {
		void newGameFunc(); // declare a prototype
		void menuFunc(); // declare a prototype
			
		return 0; // close the program
		// all the stuff after here is never run!
		void menuFuncNav(); // prototype
			
		cout << " 1: Stats\n 2: Items\n";
		cin >> userInput;

                // based on user input, declare some prototypes
		if(userInput == 1) void menuFuncInfo();
		else if(userInput == 2) void menuFuncItems();
		return 0; // end the program
		// stuff after here wouldn't be run if we got this far
		void menuFuncInfo(); // prototype

		cout << "Gender: \n";
		if(playerInfo[0] == 1) cout << "Male\n";
		else if(playerInfo[0] == 2) cout << "Female\n";

		cout << "Class: \n";
		if(playerInfo[1] == 1) cout << "Warrior\n";
		if(playerInfo[1] == 2) cout << "Rogue\n";
		if(playerInfo[1] == 3) cout << "Mage\n";
		return 0; // end the program (again)
			
		void menuFuncItems(); // prototype
			
		for(int i = 0; i < playerItemCount; i++) {
			cout << i+1 << ": ";
			cout << itemNames[playerItems[i][0]];
			cout << "\n";
			cin >> userInput;

			cout << "\nName: " << itemNames[playerItems[userInput][0]] << "\n";

			if(playerItems[userInput][1] == 1)
				cout << "Weapon: " <<playerItems[userInput][2] << "\n";
			cout << "Armor: " <<playerItems[userInput][2] << "\n";
			cout << "Consumable: " <<playerItems[userInput][2] << "\n";
			cout << "Quest: " <<playerItems[userInput][2] << "\n";
			cout << "Misc: " <<playerItems[userInput][2] << "\n";
			cout << "Press ENTER to continue ";
			cin >> userInput;
		}
		return 0;
// missing a couple ending brackets for the if statement and main() 
Thanks a lot, atleast now it will continue, but there's still 1 problem.

Instead of being able to press 0 at any time your at a new location to look at your menu, the program closes. Additionally, whenever i get to the point where the menu's code is, the menu opens, which i also dont want. Heres a bit of code where you have the option to look into your menu

1
2
3
4
5
6
7
8
9
10
if(playerLocation == 3) {
                       cout << "You arrive to Novgorod safely and quickly.\n";
                       cout << "Novgorod is a large fishing town with an inn, and dusk is falling.\n";
                       cout << " 1: Rest in the inn.\n 2: Go south into the snow forests.\n";
                       cin >> userInput;
                       
                       if(userInput == 1) playerLocation = 4;
                       else if(userInput == 2) playerLocation = 5;
                       else if(userInput == 0)  void menuFuncNav();
                       }

Thanks for the help :D
Just like issue in your previous code, on line 9 you are declaring a prototype, not calling a function.
1
2
void menuFuncNav(); // declares a function prototype
menuFuncNav(); // calls the function menuFuncNav with no parameters 
Last edited on
either my tutorial is goofed or i dont understand functions well at all.
https://sites.google.com/site/thepenguinprogrammer/c-rpg-tutorial/the-menu-system
The tutorial is fine. You aren't copying the syntax properly.

For example:
https://sites.google.com/site/thepenguinprogrammer/c-rpg-tutorial/the-menu-system3
See how they are calling the function when the input is 2? They aren't including the return type like you are.

The other, and probably more important note:
https://sites.google.com/site/thepenguinprogrammer/c-rpg-tutorial/the-menu-system
See how they define functions? They aren't putting semicolons after the parameter list. You are, which instead defines a prototype and then opens a extraneous scope that effectively does nothing.
For some reason Dev-C++ gets upset with me when i dont put a semicolon after one the calls.

27 C:\Dev-Cpp\main.cpp expected `;' before '{' token
30 C:\Dev-Cpp\main.cpp expected `;' before '{' token
37 C:\Dev-Cpp\main.cpp expected `;' before '{' token
47 C:\Dev-Cpp\main.cpp expected `;' before '{' token


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
menuFunc() { //line 27
                    return;           
                   }                           
               menuFuncNav() { //line 30
                   cout << " 1: Stats\n 2: Items\n";
                   cin >> userInput;                           
                   if(userInput == 1)  menuFuncInfo();
                   else if(userInput == 2)  menuFuncItems(); 
                   return;                          
                   }
               menuFuncInfo() {  //line 37                         
                   cout << "Gender: \n";
                   if(playerInfo[0] == 1) cout << "Male\n";
                   else if(playerInfo[0] == 2) cout << "Female\n";                          
                   cout << "Class: \n";
                   if(playerInfo[1] == 1) cout << "Warrior\n";
                   if(playerInfo[1] == 2) cout << "Rogue\n";
                   if(playerInfo[1] == 3) cout << "Mage\n";
                   return;                           
                   }
               menuFuncItems() { //line 47
                   for(int i = 0; i < playerItemCount; i++){
                        cout << i+1 << ": ";
                        cout << itemNames[playerItems[i][0]];
                        cout << "\n";
                        cin >> userInput;                                   
                        cout << "\nName: " << itemNames[playerItems[userInput][0]] << "\n";                                   
                        if(playerItems[userInput][1] == 1)
                        cout << "Weapon: " <<playerItems[userInput][2] << "\n";
                        cout << "Armor: " <<playerItems[userInput][2] << "\n";
                        cout << "Consumable: " <<playerItems[userInput][2] << "\n";
                        cout << "Quest: " <<playerItems[userInput][2] << "\n";
                        cout << "Misc: " <<playerItems[userInput][2] << "\n";
                        cout << "Press ENTER to continue ";
                        cin >> userInput;                       
                        }
                        return;                           
                   } //MFI 


Thanks for all the help by the way. Sorry for being a newb.



Are you trying to define functions there? It looks like yes, in which case you need the return type. Perhaps it would serve you to read another tutorial on functions, which will talk about how to define and call them:
http://www.cplusplus.com/doc/tutorial/functions/
I honestly don't see what im doing wrong. I've even copy-pasted it and it wouldnt work.

EDIT: Here's my code ATM
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
#include <iostream>
#include <string>
using namespace std;

bool running = 1;
void titleFunc();
int userInput = 0;
int playerInfo[2];
int playerLocation = 0;
const int MAX_ITEMS = 100;
int playerItems[MAX_ITEMS][10];
string itemNames[] = { "Sword", "Average Clothing", "Rusted Armor", "Dagger", "Staff" };
bool itemPickedUp2 = 0;
bool itemPickedUp4 = 0;
short playerItemCount = 0;
void menuFunc();
void menuFuncNav();
void menuFuncItems();
void menuFuncInfo();
int main() {
    cout << "\t\t\t\t---Hero of Golar: Part 1---\n\n\n";
    cout << "\t\t\t\t   1: Play\n";    
    cin >> userInput;    
    if(userInput == 1){
          void newGameFunc(); {
               menuFunc(); {
                    return;           
                   }                           
               menuFuncNav(); {
                   cout << " 1: Stats\n 2: Items\n";
                   cin >> userInput;                           
                   if(userInput == 1)  menuFuncInfo();
                   else if(userInput == 2)  menuFuncItems();
                   return;                          
                   }
               menuFuncInfo(); {                                              
                    cout << "Gender: ";
                    if(playerInfo[0] == 1) cout << "Male\n";
                    else if(playerInfo[0] == 2) cout << "Female\n";
                    cout << "Class: ";
                    if(playerInfo[1] == 1) cout << "Warrior\n";
                    else if(playerInfo[1] == 2) cout << "Rogue\n";
                    else if(playerInfo[1] == 3) cout << "Mage\n"; 
               return;                           
               }
               menuFuncItems(); {
                        while(userInput != 0) {                                         
                        for(int i = 0; i < playerItemCount; i++) {
                        cout << i+1 << ": "; // Output the position of the item (one based)
                        cout << itemNames[playerItems[i][0]]; // Output the name of the item at that position
                        cout << "\n";
                        cout << "\nName: " << itemNames[playerItems[userInput][0]] << "\n"; // Output the items name
                        if(playerItems[userInput][1] == 1) { // Is the item of type 1?
                        cout << "Variable 1: " << playerItems[userInput][2] << "\n";
}                       if(playerItems[userInput][2] == 2) { // Is the item of type 1?
                        cout << "Variable 1: " << playerItems[userInput][2] << "\n";
                        if(playerItems[userInput][3] == 3) { // Is the item of type 1?
                        cout << "Variable 1: " << playerItems[userInput][2] << "\n";
                        if(playerItems[userInput][4] == 4) { // Is the item of type 1?
                        cout << "Variable 1: " << playerItems[userInput][2] << "\n";
                        if(playerItems[userInput][5] == 5) { // Is the item of type 1?
                        cout << "Variable 1: " << playerItems[userInput][2] << "\n";
                        cout << "Press ENTER to continue ";
                        cin >> userInput;
               return;                           
               } //MFI 


EDIT 2: I fixed those if statements >.<
Last edited on
Perhaps the problem is you are trying to do too much at once. Try writing a simple program that simply calls a function and see if you can get that to work.
If the function is void, it shouldn't be returning anything. Also, it looks like you're declaring a function from within your main function. I believe that's called a nested function, which isn't allowed in C++.

you should have it be like
1
2
3
4
5
6
7
8
9
10
11
12
13
void Function(); // prototype, if needed depending on order of function declarations in your program
                 // if you have your function declared after it's called in your main function, you'll need to have a prototype.

void Function()  // Notice: no semi-colon
{
    //...Do stuff...
}

int main()
{
    Function();
    ...
}

Edit: Also, depending on your compiler, it won't compile if you don't return an integer value at the end of your main function (instead of doing return; do return 0; )
Last edited on
Yeah my compiler wants an interger, but 0 shuts down the program...

EDIT: I what you told me to Ganado and i fixed everything my errors told me too but the only error i got left is [Linker error] undefined reference to `WinMain@16'
I heard this can be because of my int main (Dev-C++ told me to put a semicolon after it.) So i tried doing int main(int argc, char *argv[]); and it still wont work.
Last edited on
You must've created the incorrect type of project. Try selecting something like "Blank Project" and then manually add your files in there.

That said, Dev-C++ is really old and outdated; you should consider finding something newer.
Lines 25-28 (of your most recently-posted code) looks weird. Line 25 looks like a declaration of a function newGameFunc, but I don't see the definition of the function anywhere, and nor do I see that function being called anywhere.

On Line 26, you call menuFunc, but I don't see a definition for it anywhere.

Then, you open a new code block at the end of line 26, for some reason, and immediately return from main - which means that none of the following code will be executed.

Also, you've (correctly) defined main to return an int, so all the return statements should return a value.
Last edited on
Topic archived. No new replies allowed.