Intro/ Program freezes during function call

Hello, i'm just getting started into C++ programming and during the course of learning have repeatedly looked to this community as a resource/suppliment for how to program. Ive come to enjoy my time on the tutorials, and browsing your forums and would now like to start getting involved.

Now for my current problem, i've created a small game in order to practice the concepts i've learned, however during my main function I attempt to call a second function called battle() and the program freezes during runtime when the function is supposed to be called (The input cursor blinks after the introduction to the program, and nothing i do allows it to progress.) Theres also an issue with altering the health variables within the battle function, but thats something for me to work on once it stops freezing.

OS = Windows 7
Compiler = Code::Blocks

And now, the source code. (note that i exchanged alot of the narration with single characters to make it shorter.)

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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include <cstdlib>
#include <iostream>
#include <time.h>

using namespace std;

int battle();

int main(int argc, char *argv[])
{
    cout << "Welcome to the first turn-based game created by Jonah \n \n";      //Intro. //
    cin.ignore();
    cout << endl
         << "You've been thrown into the gladiators pit \n"
         << "facing you is a manhungry lion, only one may survive. \n"
         << "You are equipped with a spear, and a shield. Shoddy clothes \n"
         << "are all that protects your otherwise, good luck \n \n";
    battle();               //battle() defined on line 44.//
    switch (battle())       
    {                       //Ending scenarios, (determined by return value of battle(). //
        case 1:
        {
            cout << "P";
            cin.ignore();
            cout << "GAME OVER";
        }
        case 2:
        {
            cout << "Q";
            cin.ignore();
            cout << "YOU WIN!!";
        }
        case 3:
        {
            cout << "R";
            cin.ignore();
            cout << "GAME OVER";
        }
    }
    cin.ignore();
    return 0;
}

int battle()
{
    int attOpt;
    int lionAttOpt;

    long HP = 100;                      //Combatants "health".//
    long lionHP = 100;

    while (HP > 0 && lionHP > 0); // Ends loop when either combatant's HP reaches 0.//
    {
          cout << "Select an attack by entering '1-3' \n \n"    // Allows user to input their choice of attack.//
               << "1.Charge \n"
               << "2.Thrust \n"
               << "3.Defend \n \n";
          cin >> attOpt;
          switch (attOpt)
          {
                case 1:
                {
                     cout << endl
                          <<"A";
                     cin.ignore();
                     break;
                }
                case 2:
                {
                     cout << endl
                          << "B";
                     cin.ignore();
                     break;
                }
                case 3:
                {
                     cout << endl
                          << "C";
                     cin.ignore();
                     break;
                }
          }

          srand (time(NULL));                // Seeds the time into the rand (random) function. //
          lionAttOpt = (rand() % 3);         // Produces a random number (dependent on seed) to produce lionAttOpt. //

          switch (lionAttOpt)           // the lions attack choice. //
          {
                case 0:
                {
                     cout << endl
                          << "D";
                     cin.ignore();
                     break;
                }
                case 1:
                {
                     cout << endl
                          <<"E";
                     cin.ignore();
                     break;
                }
                case 2:
                {
                     cout << endl
                          << "F";
                     cin.ignore();
                     break;
                }
          }

          if ( attOpt == 1 && lionAttOpt == 0)      // lists different outcomes of various attack choices. //
          {
               cout << endl
                    << ""
                    << "G";
               HP - 10;                     // damage done to combatants//
               lionHP - 10;
          }
          if ( attOpt == 1 && lionAttOpt == 1)
          {
               cout << endl
                    << ""
                    << "H";
               HP - 30;
          }
          if ( attOpt == 1 && lionAttOpt == 2)
          {
               cout << endl
                    << ""
                    << "I";
               lionHP - 60;
          }
          if ( attOpt == 2 && lionAttOpt == 0)
          {
               cout << endl
                    << ""
                    << "J";
               lionHP - 25;
          }
          if ( attOpt == 2 && lionAttOpt == 1)
          {
               cout << endl
                    << "K";
          }
          if ( attOpt == 2 && lionAttOpt == 2)
          {
               cout << endl
                    << "L";
               HP - 30;
          }
          if ( attOpt == 3 && lionAttOpt == 0)
          {
               cout << endl
                    << ""
                    << "M";
               HP - 55;
          }
          if ( attOpt == 3 && lionAttOpt == 1)
          {
               cout << endl
                      << "N";
               lionHP - 40;
          }
          if ( attOpt == 3 && lionAttOpt == 2)
          {
               cout << endl
                      << "O";
          }
          cin.ignore();
    }

    if (HP <= 0 && lionHP > 0)
    return 1;

    if (lionHP <=0 && HP > 0)       // Return values determine the result of the battle.//
    return 2;

    else
    return 3;
}
Last edited on
while (HP > 0 && lionHP > 0);
This while loop contains no code, so it will run forever.
I expect you meant this line of code to read:
while (HP > 0 && lionHP > 0)
Ha, thats embarassing.... that fixed it, thank you for your help.
Next time this happens, run your code using a debugger and interrupt it when you feel like it; you will be shown the line of code currently being executed and can use that to diagnose such infinite loop problems.
Topic archived. No new replies allowed.