Very Early Adventure game with lots of errors please help

So I am new to C++ and I took a class on it last semester in School. I am very interested in programming and would like to become a Game Developer I made this very early "Text Game" but I have a lot of errors I can't solve any help is appreciated
Here is my code
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
#include <iostream>
#include <vector>
#include <string>
#include <cstdlib>
using namespace std;
int main()
{
    srand(static_cast<unsigned int>(time(0)));
    int lives = 3;
    int random = rand();
    int random1 = rand();
    int random2 = rand();
       class Player
        {
              public:
              int PLayerHealth;
              int PlayerDamage;
             Player (int P_Health = 100);
             Player (int BattleDamage = (random1 % 10) + 5);
        }
         class Skeleton
         {
               public:
               int (SkeletonHealth = (random2 % 100) + 50);
               int (SkeletonD = 1);
         }
         //Classes and declares all random functions
    int WhatToDo;
    PlayerHealth = P_Health;
    PlayerDamage = BattleDamage;
    Player P1;
    Player p2;
    Skeleton S1;
    Skeleton S2;
    /* This secton declares all the intergers in the game accordingly
    and will also decide the randome functions of the game 
    */
    cout<<"WELCOME To Ryan's Text based RPG!        V 1.0 \n";
    
    do {
 int ExploreFunc = (random % 2) + 1; //Decdides what will happen while exploring
        int BattleToDo;
        cout<<"Choose your action \n 1: Explore \n 2: Shop \n 3: Exit\n";
        cin>>WhatToDo;
        switch(WhatToDo) // Player decides what to do
        {
                        case 1:
                             if (ExploreFunc = 1 || ExploreFunc = 2)
                             {
                                cout<<"A skeleton ATTACKS!\n";
                                cout<<"Your health is: \n"<<P1.P_Health;
                                cout<<"Enemy health is: \n"<<S1.SkeletonHealth;
                                     while (S1.SkeletonHealth > 0)
                                      {
                                       cout<<"What do you do!?\n";
                                       cin>>BattleToDo;
                                       switch (BattleToDo)
                                      {
                                      case 1:
                        cout<<"You did: "<<P2.BattleDamage<<" To the enemy\n";
                        S1.SkeletonHealth = S1.SkeletonHealth - P2.BattleDamage;
                        cout<<"Enemy Health is now: \n"<<S1.SkeletonHealth;
                        cout<<"Enemy is now attacking it did: \n"<<S1.SkeletonD;
                        P1.P_Health = P1.P_Health - S2.SkeletonD;
                        cout<<"Your Health is now: \n"<<P1.P_Health;
                        if (S1.SkeletonHealth <= 0)
                        {
                         cout<<"YOU WON!\n";
                          }
                                                  
             }
}                                             
                             
          break;
                             
         }
        }while(WhatToDo != 3);
     
 system("pause");
 return 0;
 }

Thanks in advance.
Last edited on
Class declarations are usually outside of functions. Where you have declared class Player, and class skeleton, you need to move these outside the main function, preferably before where you typed it out.

consider reading here:
http://www.cplusplus.com/doc/tutorial/classes/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Player
{
  //all your player stuff
};

class Skeleton
{
  //all your skeleton stuff.
};

int main()
{

//all your game stuff
return 0;
};


You will also need to define a body for the constructor for each class. You have declared a constructor, but have not created one:
1
2
3
4
5
6
7
class Player
{
public:
Player(Player (int Health):
P_Health(Health)
{};
};


The link I provided above will explain things a little easier.

Thank you very much for your help.
No need to static_cast srand. srand( (unsigned)time( NULL ) ); should suffice.

Also, your class design is so overly complicated for such a simple game. WHy not keep it as simple as possible while you are starting out.

Something more like:

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
   
class Player
{
public:
	Player (int lives = 3, int health = 100);
	int Attack() { return rand() % 10 + 1; }
        void UpdateHP(int val) { health -= val; }
        bool IsDead() { return health <= 0; }
	//getters	
	int GetHealth() { return health; }
        int GetLives() { return lives; }
private:
	int lives;
	int health;
};

class Skeleton
{
public:
	Skeleton(int health = rand() % 100 + 50);
	int Attack() { return rand() % 10 + 1; }
        void UpdateHP(int val) { health -= val; }
        bool IsDead() { return health <= 0; }
	//getters
	int GetHealth() { return health; }
	
private:
	int health;
};


Then your main could be cleaner:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Player p1;
Skeleton s1;
int playerAttack;
int enemyAttack;

while(!p1.IsDead() and !s1.IsDead())
{
    playerAttack =  p1.Attack() ;
    enemyAttack =  s1.Attack() ;
    p1.UpdateHP(enemyAttack);
    s1.UpdateHP(playerAttack;);

    cout << "Player landed a " << playerAttack << "hit point attack!" << endl;
    cout << "Enemy retaliated with a " << enemyAttack << "hit point attack!"         
         << endl; 
}
if (p1.IsDead())
    cout << "You died sucka! Try again! << endl;
 

Something like that at least.

You could definitely take advantage of polymorphism here too. You could create a base class with all the shared variables and methods.
Last edited on
Line 48 also seems a bit odd to me as well..
if (ExploreFunc = 1 || ExploreFunc = 2)

This isn't checking the values of ExploreFunc, but it is assigning two different values to ExploreFunc. I presume you wanted the == to check the value.

Other than that, why not separate your game into different functions? Maybe it's just me, but I go crazy if main() is filled with so much clutter. It makes debugging very difficult if things are smashed all into one function like that.
Topic archived. No new replies allowed.