variable not declared in the scope

Ok guys I am extremely new to c++ and programming in General (I started trying to learn c++ a month ago and started a book called "Learning c++ by creating Games with UE4".
Now this is a hobby and so I don't get lots of time to study/learn but I decided to try the code I have learned so far and create something totally on a slight tangent from the book.

I know my code won't be prefect in its presentation and any helpful advice would be appreciated. The main problem I am having though is on lines 45 to 47 in the while loop in the main function.
It tells me that the variables PlayerOne.Health, PlayerOne.Dmg, MonsterA.Health and MonsterA.Dmg are not declared in this scope.

Now I presume this is because they are private variables within their own functions but I am struggling to find a way to make them public. Sure if I didn't have screaming kids shouting at me tonight I could probably find the answer out eventually but thought I would ask the good folks on this forum for some advice.
With Thanks

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
  #include <iostream>
#include <stdlib.h>
#include <time.h>

using namespace std;
int AxeDmg;
int AxeSpd;
int SwordDmg;
int SwordSpd;
int DaggerDmg;
int DaggerSpd;
void PlayerA();
void CreatingMonster();


struct Player
{
    string Name;
    int Health;
    int Speed;
    string Weapon;
    int Dmg;
};

struct Monster
{
    int Health;
    int Speed;
    int Damage;
};


int main()
{
    srand(time(NULL));
    AxeDmg = rand() %10+10;
    AxeSpd = 3;
    SwordDmg = rand() %5+7;
    SwordSpd = 2;
    DaggerDmg = rand() %2+5;
    DaggerSpd = 1;
    PlayerA();
    CreatingMonster();

    while(PlayerOne.Health >0 || MonsterA.Health >0)
        cout << PlayerOne.Name << " attacks the monster" <<endl;
        MonsterA.Health -= PlayerOne.Dmg;
        cout << PlayerOne.Name << " is attacked by the monster" << endl;
        PlayerOne.Health -= MonsterA.Dmg;
}

void PlayerA()
{
    Player PlayerOne;
    PlayerOne.Health = 500;
    cout << "Please type a name for your character " << endl;
    cin >> PlayerOne.Name;
    cout << "Please choose a weapon from the following list " << endl;
    cout << "1) Sword" << endl << "2) Dagger" << endl << "3) Axe"<< endl;
    cin >> PlayerOne.Weapon;
    if (PlayerOne.Weapon=="Axe")
    {
        PlayerOne.Dmg=10+AxeDmg;
        PlayerOne.Speed=1*AxeSpd;
    }
    else if (PlayerOne.Weapon=="Sword")
    {
        PlayerOne.Dmg=10+SwordDmg;
        PlayerOne.Speed=1*SwordSpd;
    }
    else if (PlayerOne.Weapon=="Dagger")
    {
        PlayerOne.Dmg=10+DaggerDmg;
        PlayerOne.Speed=1*DaggerSpd;
    }
    else
    {
        PlayerOne.Dmg=10;
        PlayerOne.Speed=1;
    }

    cout <<"Your name is: "<< PlayerOne.Name << endl;
    cout <<"Your weapon is: " << PlayerOne.Weapon << endl;
    cout <<"Your Damage is: " << PlayerOne.Dmg << " and Speed is: " << PlayerOne.Speed;
}

void CreatingMonster()
{
    Monster MonsterA;
    MonsterA.Health = 400;
    MonsterA.Speed = 1;
    MonsterA.Damage = rand() %20+7;
}
You can't use PlayerOne in the main function because you created PlayerOne in the function PlayerA, so main does not know about it. What you could do is create it in main, and send it as a function parameter to PlayerA.
Well, PlayerOne is declared inside the function PlayerA. It isn't avisible outside of that function. MonsterA is also not visible outside of CreatingMonster. Those variables are local to those two functions. I see you've learned about classes. Because those variables are local, that also means that any changes you make to them while inside those functions will be lost when the function returns.

Why not create aPlayer and Monster in main and then use a function to assign the correct values to the variables inside PlayerOne and MonsterA? You could also create a function inside each of those classes that you call, like 'PlayerOne.GetInfo()" and "MonsterA.Create()", or whatever you decided to call them.

Edit: He beat me to it.
Last edited on
Brilliant thanks guys. I was thinking that it would be something like that.

Thanks for the additional information as well
Topic archived. No new replies allowed.