Structure Help

I'm new to C++ and I'm working my way though some beginners tutorials and making simple programmes from what I've learned.

I've gotten up to Structures so I decided to make an RPG scenario where you meet a group of people and choose from some options which either raises your health or gets you killed.

I've created a Structure containing the players mana, health and money and declared them in main(). I also set a value to each one. When it comes to choice, it's meant to take away some of your money and raise your health. Instead, they stay the same.

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<cstdlib>

using namespace std;

struct database
{
    int health;
    int mana;
    int money;
};


int main()
{
    int option;
    database player;

    player.health = 100;
    player.mana = 20;
    player.money = 7;

    cout << "HUMAN: Why are you here?" << endl;;
    cout << endl;
    cout << "1. I need a place to sleep" << endl;
    cout << "2. I need a place to eat" << endl;
    cout << "3. I am going to kill you all" << endl;
    cout << endl;
    cout << "Enter: ";
    cin >> option;
    cin.get();

    switch (option) {
        case 1:
            system("CLS");
            cout << "HUMAN: We have a spare bed... for a price." << endl;
            player.money - 2;
            player.health + 5;
            cin.get();
            cout << "Money: " << player.money << endl;
            cout << "Health: "<< player.health << endl;
            cout << "Mana: "<< player.mana << endl;
            cin.get();
            break;
        case 2:
            system("CLS");
            cout << "HUMAN: Food is scarce here. I'll give you some scraps for some gold." << endl;
            player.money - 5;
            player.health + 10;
            cin.get();
            cout << "Money: " << player.money << endl;
            cout << "Health: "<< player.health << endl;
            cout << "Mana: "<< player.mana << endl;
            cin.get();
            break;
        case 3:
            system("CLS");
            cout << "HUMAN: No chance! Taste my blade scum!" << endl;
            cin.get();
            break;
        default:
            system("CLS");
            cout << "HUMAN: Say that again?" << endl;
            cin.get();
            int main();
            break;

    }
}



So what have I done wrong?
You shouldn't call main() from inside main()
int main();

But since you have, then you are creating an entirely new player when you enter main() for the subsequent time.

Rather than keep calling main() and creating a new player, it would be better to put your switch() into a loop. Perhaps a while() loop?


EDIT:

Actually you are not calling main() from inside main() you are just declaring it again.

Your problem is that you don't assign new values to your player. Try this instead:
1
2
            player.money -= 2;
            player.health += 5;

By way of explanation:
1
2
3
4
5
            player.money -= 2;

            // this is the same as

            player.money = player.money - 2;
Last edited on

And your arithmatic is not quite right...

1
2
            player.money - 2;
            player.health + 5;


the first of these lines takes 2 from player.money but does not store the result anywhere

Use

player.money = player.money - 2;

or

player.money -= 2;

Similarly for +/+= (careful! player =- 2; is also valid syntactically, but is just setting the variable to -2. That is, its the same as = -2)

And as Galok said, you should not be calling main() from main(). This will cause the program to run out of stack space if the games continues for long enough (Each function call uses a bit of stack memory which is released when you return from the function. But calling main, main, main, ... means you are not returning, and hence not freeing the memory!)
Last edited on
Thanks, that seemed to work. One last question though, what could I use instead of system("CLS")? I know that it's bad practise to use but I can't find an alternative.
The only pure, cross-patform C++ way to clear the screen is to print lots of blank lines.

For assorted platform specific solutions, see Duoas article: "Clear the screen"
http://www.cplusplus.com/forum/articles/10515/
Topic archived. No new replies allowed.