Text RPG Variable

I'm having two issues with my code. One is that when I run my program it starts with my bool as opposed to my main function. The other is within that bool, it won't read an integer from my Player class. It reads another variable totally fine. I have simplified my code in a test environment to try to troubleshoot.

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

class Player
{
public:
    double hp = 200;
    double money = 0;
    int weapon = 0;

    void showHUD()
    {
        system("cls");
        cout << "HP: " << hp << endl;
        cout << "Money: " << money << endl;
        cout << "Weapon: " << weapon << endl << endl;
    }
};

class Weapon
{
public:
    double unarmed = 2;
    double bat = 5;
};

class Enemy
{
public:
    double boss = 25;
    double boss1 = 30;
};

void showMain()
{
    cout << "1. Fight" << endl;
    cout << "2. Get Weapon" << endl;
    cout << "3. Quit" << endl;
    cout << "Choice: ";
}

void showError()
{
    system("cls");
    cout << "ERROR ERROR YOU FUCKED UP ERRRO ERROR" << endl << endl;
    system("pause");
}

bool showFight()
{
    Player user;
    Weapon item;
    Enemy badguy;
    double arm;

    // get weapon
    if(user.weapon==0) {arm = item.unarmed;}
    if(user.weapon==1) {arm = item.bat;}

    while(user.hp>=1 && badguy.boss1>=1)
    {
        system("cls");
        cout << "HP: " << user.hp << endl;
        cout << "Boss HP: " << badguy.boss1 << endl;
        cout << "USER WEAPON: " << user.weapon << endl << endl;
        system("pause");

        user.hp = user.hp - 5;
        badguy.boss1 = badguy.boss1 - arm;

        if(user.hp<=0)
        {
            system("cls");
            cout << "You lose" << endl << endl;
            system("pause");

            return false;
        }
        if(badguy.boss1<=0)
        {
            system("cls");
            cout << "You won!" << endl << endl;
            system("pause");

            return true;
        }
    }
};

int main()
{
    bool quit = false;
    Player user;
    int menuInput;
    bool fightWin = showFight();

    while(quit==false)
    {
        user.showHUD();
        showMain();
        cin >> menuInput;

        switch(menuInput)
        {
        case 1:
            // fight
            showFight();
            if (fightWin==true) {user.money = user.money + 5;}
            break;
        case 2:
            // get weapon
            user.weapon = 1;
            break;
        case 3:
            quit = true;
            break;
        default:
            showError();
            break;
        }
    }

    return 0;
}
Last edited on
One is that when I run my program it starts with my bool as opposed to my main function.

You are incorrect.


The other is within that bool, it won't read an integer from my Player class.


Incorrect again

Take out all the system commands and add this and you'll get the ouput below.

1
2
3
4
5
6
class Player
{
public:
    double hp = 33;
    double money = 22;
    int weapon = 1;




1
2
3
int main()
{
	cout << "Test1" << endl;


C:\Test
Test1
HP: 33
Boss HP: 30
USER WEAPON: 1

HP: 28
Boss HP: 25
USER WEAPON: 1

HP: 23
Boss HP: 20
USER WEAPON: 1

HP: 18
Boss HP: 15
USER WEAPON: 1

HP: 13
Boss HP: 10
USER WEAPON: 1

HP: 8
Boss HP: 5
USER WEAPON: 1

You won!

HP: 33
Money: 22
Weapon: 1

1. Fight
2. Get Weapon
3. Quit
Choice: 3

C:\Test
Last edited on
If you look at your post, a little gear shouldve popped up on the top-right of your code (if you don't see it, as sometimes is the case after just posting, refresh the page). You can click the gear and it'll run it on one of the online compiler sites -- does that online output look correct? After a fight, option 2 does seem to change the weapon, which is your only integer, so, as SamuelAdams said, not seeing the issue.

In general, since you appear to want public access to everything by default, perhaps just use struct everywhere instead of class. Then you won't need the public: modifier.

To be more class-y, perhaps have everyone derive from a common base class. One way to set things up:
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>
#include <string>
#include <array>
#include <vector>

using namespace std;

typedef pair<string, double> Weapon;
const array<Weapon, 3> Weapons
{{
    { "Unarmed", 1.0 },
    { "Bat", 2.0 },
    { "Knife", 4.5 }
}};

struct Person
{
    Person() : hp(200), money(0.0), weapon(Weapons[0]), name("Mob")
    {
    }
    
    Person(double hp, double money, Weapon weapon, string name) :
        hp(hp),
        money(money),
        weapon(weapon),
        name(name)
    {
    }
    
    Person(string name) : 
        hp(200),
        money(0.0),
        weapon(Weapons[0]),
        name(name)
    {
    }
    
    void Show()
    {
        cout << name << endl;
        cout << "  " << hp << " hp" << endl;
        cout << "  $" << money << endl;
        cout << "  " << weapon.first << "(" << weapon.second << ")\n\n";
    }
    
    double hp;
    double money;
    Weapon weapon;
    string name;
};

struct Player : Person
{
    Player() : Person()
    {
    }
    Player(double hp, double money, Weapon weapon, string name) :
        Person(hp, money, weapon, name)
    {
    }
    Player(string name) : Person(name)
    {
    }
};

struct Enemy : Person
{
    // Default enemy values
    Enemy() : Person(50, 100, Weapons[0], "Enemy")
    {
    }
    
    Enemy(double hp, double money, Weapon weapon, string name) :
        Person(hp, money, weapon, name)
    {
    }
};

int main()
{
    vector<Person> people = 
    {
        { Player() },
        { Player("Player1") },
        { Enemy() },
        { Enemy(100, 2500.0, Weapons[1], "Boss1") }
    };
    
    for (auto& p : people)
        p.Show();
    
    return 0;
}


can test at https://repl.it/repls/TinyFinancialNasm

Mob
  200 hp
  $0
  Unarmed(1)

Player1
  200 hp
  $0
  Unarmed(1)

Enemy
  50 hp
  $100
  Unarmed(1)

Boss1
  100 hp
  $2500
  Bat(2)

So why is it when I change user.weapon to 1 using option 2 it still returns as 0 in my showFight() function?
oh, I see what you mean now. Pass in the player by reference so that it reuses the same player, or else you'd have a local copy in there.

line 50 of yours -- bool showFight(Player& user)

line 52 of yours -- remove the "Player user;" line

change all your other showFight() calls to showFight(user)
That worked! Thank you very much!
Just learned structs in a class, what does the semicolon do in the declaration of a struct?

Example:
1
2
3
4
5
6
7
8
9
10
11
12
struct Enemy : Person
{
    // Default enemy values
    Enemy() : Person(50, 100, Weapons[0], "Enemy")
    {
    }
    
    Enemy(double hp, double money, Weapon weapon, string name) :
        Person(hp, money, weapon, name)
    {
    }
};
@wsme: Please ask a question in your own thread next time.

A semicolon in the declaration of a struct does the same thing that a semicolon does in the declaration of a class (like the Weapon class in the OP). Classes and structs are basically the same thing in C++, they just have different default access modifiers. (Classically, however, structs tend to be basic POD structures, but this is not a rigid rule. Don't worry about it if you don't know what this means).

The semicolon is needed because C and C++ syntax lets you declare an object of the struct or class type immediately after the declaration of the class. This, however, is usually a global variable (unless you have a locally defined class), and is generally avoided in C++.

1
2
3
struct MoonPie {
 
} what_a_time_to_be_alive;

what_a_time_to_be_alive is an object of type MoonPie.
@Ganado: My apologies

Okay I see. Thanks!
Topic archived. No new replies allowed.