Simple, quite long code.

Hello everybody,

After reading some more about the tutorial, i started sketching some code for a simple game...
I didnt want to go "TÓ" deep into this, but i just want your opinion on the code. Note that i didn't use any classes or anything and that its really basic.

The rules clearly state to not post long useless codes, if you don't want to waste your time reading what i was trying to create you can scroll down to read my more "to the point" example.

The code is only half done but posting more crap doesnt state my point more clearly then this one.

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
#include <iostream>
using namespace std;
     
void Store(){
     
     cout << "The Item store is not finished yet\n" << endl;
     
     int Mainmenu();
     Mainmenu();
     
     }
     
void Character(){
     
     int PlayerHp, PlayerStr, PlayerMana;
     
     cout << "Your character has:\n\n";
               
     cout << PlayerHp << " Health" << endl;
     cout << PlayerStr << " Strength" << endl;
     cout << PlayerMana << " Mana" << endl;
     
     cout << endl;
     
     int Mainmenu();
     Mainmenu();
     }
     
void Inventory(){
     
     cout << "Your inventory contains:\n" << endl;
     
     int Mainmenu();
     Mainmenu();

     }
     
void Wilderness(){
     
     cout << "You search the Jungle and find:\n" << endl;
     
     string Creatures[10];
     
     Creatures[0] = "1";
     Creatures[1] = "2";
     Creatures[2] = "3";
     Creatures[3] = "4";
     
     cout << Creatures << " Is really dangerous " << endl;
     
     int Mainmenu();
     Mainmenu();
     }

void Mainmenu(){
     
     string choice;
     
     cout << "1: Attack creature" << endl;
     cout << "2: Buy equipment" << endl;
     cout << "3: Inventory" << endl;
     cout << "4: Stats" << endl;
          
          // invalid option returns to this
          InvalidOption:
                      
     cin >> choice;
     if (choice == "1"){
                Wilderness ();
                }
     else if (choice == "2"){
                     Store();
                     }
     else if (choice == "3"){
                     Inventory();
                     }
     else if (choice == "4"){
                     Character();
                     }
     else{
          cout << "You have not chosen a valid option" << endl;
          goto InvalidOption;
     }
                     

     
     }

int main()
{
    int PlayerHp = 100, PlayerStr = 20, PlayerMana = 50;
    Mainmenu ();
}


As you can see i used "Void" functions, simply because i know how to use those, and it might not be the most effectively written code, now when the program starts it skips all the functions and directly go's to main.

I figured thats a good way to define some variable there.

Health.
Strength.
Mana.

From this point there aint really any confusion, but when i call on any of these variable's it is said that the variable's aren't defined.
I figured the compiler does this because it needs to be able to read a variable type on an early'r line then it is called for, for example in the character void.

Thats where i got confused, because it skips the functions if they are not called for.

for example:

1
2
3
4
5
6
7
8
9
10
11
12
13
void character(){

cout << "Your health is: " << Health;

}


int main(){

int health = 100, strength = 20, Mana = 50;
character();

}


This would normally execute "main", and "main" would execute "character" but the compiler says that on line X,
cout << "Your health is: " << Health;
the variable health would not be defined.

Suggestions anyone ?.
Last edited on
This is because of the scope of the variables.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void character()  
{ 
    // There is no local variable call Health
    // there is also no Global variable call Health
    // so you get an error about undefined variable.
    cout << "Your health is: " << health;
    
}


int main()
{
    //The following variables have Local scope
    // they are only accessable from within the main function.
    int health = 100, strength = 20, Mana = 50;  

    character();

}


For now the easiest way to 'solve' the problem would be to give the variables global scope:

1
2
3
4
5
6
7
8
9
10
11
12
void character()  
{ 
    cout << "Your health is: " << health; 
}

// Variables with global scope:
int health = 100, strength = 20, Mana = 50;

int main()
{
    character();
}


See: http://www.cplusplus.com/doc/tutorial/variables.html

HTH
Last edited on
No it isn't defined, this is down to the scope. Health exists in main, and main only. It does not exist in character. You can pass it into character as a parameter
1
2
3
4
5
6
7
8
9
10
11
12
13
void character(int h ){ //parameter can be any name including health

cout << "Your health is: " << h;

}


int main(){

int health = 100, strength = 20, Mana = 50;
character(health);

}


EDIT: beaten to it.
Last edited on
Thanks for your comments that helped, the reason i didn't want to create the variable's in character is obvious, this is only a way to show the user what his hp was, if you gained more hp on any other way i wouldn't want it to be reset when i pressed the "stats" key.
Last edited on
Topic archived. No new replies allowed.