Scanning whole program

I'm trying to program death into my text-based RPG game, and it would be much easier to be able to use something like "death = 1" and just have it like that. I thought putting a while statement in there would do it, but it doesn't seem to be doing anything. I have it on the top, right at the start of the int main function. Is there something else I can use to continuously scan my program to do this?

1
2
3
4
5
    while(death==1){
        cout << "Sorry, you died. Please try again!" << endl;
        death += 1;
        return 0;
    }
It's hard to tell without seeing your whole program, but you might try putting the death check in a function that returns a boolean, and calling that function whenever death gets modified.
Yeh, Do what tscott suggested, boolean would be better. True or false, If dead, return false, if alive return true or something like that.

Also, your while loop is weird. Whats the point of adding +1 to death, if you're just gonna shut down the program the next second? that'll make you re-open the program, and death will be restarted to its original value.
TarikNeaj, I wasn't sure if it would keep repeating even with the return 0, so that was just to make sure the user didn't get 10 million death statements. How do I call a function? Do I just copy the statement in parts where I want the player to die? Because that's what I was trying to avoid.
Calling a function is simple, you just write down its name and if it takes down any parameters. Watch this video -

https://www.youtube.com/watch?v=bsWWHo4KDHE&list=PLAE85DE8440AA6B83&index=9&ab_channel=thenewboston

If your function got parameters and dont know exactly how that works, check out video nr 10 or 11.
So I made it void (since I made death bool like you suggested) and put the bool name in the parameters. But it says that the name is declared void, which is an error. How should I do this? (PS, this is just a stupid game, so don't make fun of it :P)

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
#include <iostream>

using namespace std;

bool death = false;
int careerchoice;
int intelligence;
int charisma;
int strength;
int rileydecision;
int groupsize = 2;
int majdec1;
string firstname;
string lastname;
string fullname = firstname + lastname;


int main()
{
    cout << "What is your first name?" << endl;
    cin >> firstname;
    cout << endl << endl;
    cout << "What is your last name?" << endl;
    cin >> lastname;
    cout << endl << endl;
    cout << "Your name is " << fullname << endl;
    cout << "What is your background?" << endl << endl;
    cout << "1: I was a police officer." << endl;
    cout << "2: I was a sports star." << endl;
    cout << "3: I was a computer programmer." << endl;
    cout << "4: I was in a small-time band." << endl;
    cout << "5: I was a teacher." << endl;
    cin >> careerchoice;

    if(careerchoice==1) {
        strength = 75;
        intelligence = 50;
        charisma = 25;
    }
    if(careerchoice==2) {
        strength = 75;
        charisma = 50;
        intelligence = 25;
    }
    if(careerchoice==3) {
        intelligence = 100;
        charisma = 25;
        strength = 25;
    }
    if(careerchoice==4) {
        charisma = 100;
        intelligence = 25;
        strength = 25;
    }
    if(careerchoice==5) {
        intelligence = 75;
        charisma = 50;
        strength = 25;
    }

    cout << "Your stats are:" << endl;
    cout << "Intelligence - " << intelligence << endl;
    cout << "Charisma - " << charisma << endl;
    cout << "Strength - " << strength << endl << endl;

    cout << "You are twenty-five years old, and living with your twenty-two year old brother, Chris, in an apartment." << endl;
    cout << "Nobody was sure what happened, but suddenly the dead were walking in the streets. You wake up to see chaos outside of your window." << endl;
    cout << "You wake Chris up and tell him to get dressed. Each of you have 9mm pistols that you keep close while keeping low at your apartment." << endl;
    cout << "Everybody secretly hoped for this since Zombieland. However, it was a lot less glamorous in reality." << endl;
    cout << "Suddenly there is a knock on the door. You go look through the door and see that it is your friend, Riley. 'What do you want?' you call." << endl;
    cout << "He replies, 'Come on, man, let me in! It's insane out here!'" << endl;
    groupsize = groupsize + 1;
    cout << "'Yeah, it definitely is. Come in,' you reply." << endl;
    cout << "You unlock the door for Riley and he enters, looking extremely frightened." << endl;
    cout << "Wasting no time, Chris asks, 'Alex, what are we going to do?'" << endl;
    cout << "What do you say?" << endl;
    cout << "1: 'We should go to Mom and Dad's in the country.'" << endl;
    cout << "2: 'We should stay in the apartment for a day or two and wait for it to cool down out there.'" << endl;
    cout << "3: 'We should go out into town and try to find our friends.'" << endl;
    cout << "4: 'We should find the nearest military base and try to get shelter there.'" << endl;
    cin >> majdec1;
    if(majdec1==1) {
    cout << "'I'll pack our stuff, then.' Chris seemed relieved at this decision." << endl;
    }
    if(majdec1==2) {
    cout << "'I don't know how long our food will last, but if you think it's best . . .' Chris said." << endl;
    }
    if(majdec1==3) {
    cout << "'I have five friends living in a house down the street, that would be a good place to start,' Chris said." << endl;
    }
    if(majdec1==4) {
    cout << "'In all the movies those places are useless, but it's probably the safest,' Chris said." << endl;
    }
    death = true;

    cout << "Hi" << endl;

    return 0;
}

void die(death) {
    cout << "You are dead!" << endl;
}
The death at the end is just to test it out, btw. And the final cout is just to make sure nothing happens after I make him die.
I figured out how to correctly call the function, so I'm just going to get rid of the bool and use that only. A little more tedious than what I planned, but it still works. So unless you guys have anything else for me, thank you for the help and the link!
void die(death) You cant do this. It doesnt do anything. Please re-watch the video about functions with parameters. Or google how functions work.

You probably want to do something like
1
2
3
4
if(death == true)
{
    cout << "You are dead!"
}


Wouldnt that solve it? You dont need the function my friend.

Yeah, I forgot the boolean, I just said that :P I just made a void statement like this:
1
2
3
4
5
void die() {
    cout << "You are dead!" << endl;

    exit(0);
}
Last edited on
that's working for me, 100%
Topic archived. No new replies allowed.