text based rpg crash

here is my void game() function it is supposed randomly pick an event it does that perfectly fine until it reaches the end of the code it crashes. i call this function inside the main(), its supposed to repeat how many times the player says so inside a for loop.

for ( length > 1; length--;)
{
Game();
}

void Game()
{
//display stats
cout << "Your Health " << Health << endl;
cout << "Your Strength " << Strength << endl;
cout << "Your Intelligence " << Intelligence << endl;
//increase difficulty
Difficulty++;
//display difficulty
cout << "Difficulty is now at " << Difficulty << endl;
cout << "Your in a " << Place << endl;
//generate random number between 1 and 2
int random;
random = rand() % 2 + 1;
srand ( time(NULL) );
//pick outcome depending on random number
if (random = 1)
{
Outcome1();
}
else if (random = 2)
{
Outcome2();
}
}
Last edited on
A couple of things I noticed:

------------------------------------------------------------------------------------------------------------------------------------------------
(1) srand should be called only once in your entire program (first thing inside main would be fine)
------------------------------------------------------------------------------------------------------------------------------------------------
(2) This -> if (random = 1){/*...*/} actually means -> random=1; if (random){/*...*/}

What you need here is this -> if (random==1){/*...*/}
------------------------------------------------------------------------------------------------------------------------------------------------
(3) Same here -> else if (random = 2){/*...*/}

You could also replace this with a plain else
------------------------------------------------------------------------------------------------------------------------------------------------

I'm afraid I can't find the reason your program crashes with only that much code...

How are functions Outcome1 and Outcome2 defined?

PS: Use code tags -> [code]your code here[/code]

EDIT: Also, this -> for ( length > 1; length--;) should be -> for ( ;length > 1; length--)
Last edited on
Thanks you helped me fix a few things but here is the outcomes

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
void Outcome1()
{
        //yn is simply just asking for yes or no
        char yn[1];
        //displaying what happens in this outcome
        cout << "You find a chest do you wish to open it" << endl;
        //getting input for yes or no
        cin >> yn;
        //if yes
        if (yn[1] = 'y')
        {
               //adds ten to health then displays health
               cout << "You find a tomb of health" << endl;
               cout << "+ 10 Health" << endl;
               Health = Health + 10;
               cout << "Health: " << Health << endl;
        }
}
// outcome2 is basicaly the same as outcome 1 but its just increasing the intelligence
void Outcome2()
{
        char yn[1];
        cout << "You find a chest do you wish to open it" << endl;
        cin >> yn;
        if (yn[1] = 'y')
        {
               cout << "You find a tomb of intellect" << endl;
               cout << "+ 10 intelligence" << endl;
               Intelligence = Intelligence + 10;
               cout << "Intelligence: " << Intelligence << endl;
        }
}
Last edited on
Ah, ok, this explains it.

You see, yn is a char array consisting of 1 element. To access that element you should write yn[0].
Here, you use yn[1], which is one element past the end of the array. Moreover, you overwrite its value
(since you assign 'y' to it - remember, = is assignment, == is equality check). That's what's causing the crash.

It would really be simpler if you just used a single char:

1
2
3
4
char yn;
//...
if (yn == 'y')
//... 
There we go its fixed :D thanks you helped alot
Topic archived. No new replies allowed.