Program Crashing

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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// RPG Style C++

#include <iostream>
#include <vector>
#include <string>
#include <ctime>
#include <cstdlib>
using namespace std;

int main()
{
    
    // Setting up Classes
    vector<string> class1;
    
    class1.push_back("Sword");
    class1.push_back("Breastplate");
    class1.push_back("Sheild");
    
    vector<string>::iterator myIterator;
    vector<string>:: const_iterator iter;
    
    vector<string> class2;
    
    class2.push_back("Bow & Arrow");
    class2.push_back("Leather Chaps");
    class2.push_back("Daggger");
    
    vector<string>::iterator myIterator2;
    vector<string>:: const_iterator iter2;
    
    vector<string> class3;
    
    class3.push_back("Staff");
    class3.push_back("Cloak");
    class3.push_back("Book of Spells");
    
    vector<string>::iterator myIterator3;
    vector<string>:: const_iterator iter3;
    
    //Output Classes
    cout << "Please choose a Class.\n";
    cout << "\n\nClass1:\n";
    for(iter = class1.begin(); iter != class1.end(); ++iter)
    cout << *iter << endl;
    
    cout << "\nClass2:\n";
    for(iter2 = class2.begin(); iter2 != class2.end(); ++iter2)
    cout << *iter2 << endl;
    
    cout << "\nClass3:\n";
    for(iter3 = class3.begin(); iter3 != class3.end(); ++iter3)
    cout << *iter3 << endl;
    
    // Selecting Class
    string choice;
    cout << "\n\nWhich class would you like to use?\n";
    cin >> choice;
    
    if(choice == "Class1")
    {
     cout << "You've selected Swordsmans.\n";
    }
    
    if(choice == "Class2")
    {
     cout << "You've selected Specialist.\n";
    }
    
    if(choice == "Class3")
    {
     cout << "You've selected Mage.\n";
    }
    
    // User's Name
    string name;
    cout << "Enter you Name:\n";
    cin >> name;
    
    // Storyboard
    // Brother
    cout << "\n\nIt is now time to begin your journey";
    cout << "\n\nHey, " << name << " come down quick!";
    cout << "Check out the televison.\n";
    cout << "The king is looking for a warrior. " << endl;
    cout << "He said he'll give a reward to the best." << endl;
    cout << "Your a great warrior\n";
    cout << "You should go.";
    
    //Narration
    // Inventory Given
    cout << "\nOn your way out, Your brother gave you\n";
    
    if(choice == "Class1")
    for(iter = class1.begin(); iter != class1.end(); ++iter)
    cout << *iter << endl;
    
    if(choice == "Class2")
    for(iter2 = class2.begin(); iter != class2.end(); ++iter2)
    cout << *iter2 << endl;
    
    if(choice == "Class3")
    for(iter3 = class3.begin(); iter != class3.end(); ++iter3)
    cout << *iter3 << endl;
    
    // Stadium Entry
    cout << "\n\nYou make it to the Arena, and there are over 100 warriors\n";
    cout << "*Bump*\n";
    cout << "Stranger: HEY WATCH!!";
    cout << "What is that? Are you in the competition.\n";
    cout << "You?!? You can't win! Some of the best are here.\n";
    cout << "Kid, let me save you the trouble, just go home.\n";
    cout << "You ignore him and put your name in the entry.\n";
    
    cout << "As the tournement continues,";
    cout << "You see that this isn't a place for boys.\n";
    cout << "Your name is Drawn and your fighting Lancelot.\n";
    
    cout << "The rules are simple, last one staning....WINS.\n";
    
    // Given the Directions to fight
    cout << "\n\nTo fight your are given 3 moves based on your class.\n";
    cout << "Both you are the enemy have 100 hitpoints.\n";
    cout << "First one to zero, loses.\n";
    
    srand(time(0));
    // Class1
    int slash = rand();
    int stab = rand();
    int lunge = rand();
    
    // Class2
    int shortrange = rand();
    int longrange = rand();
    int dart = rand();
    
    // Class3
    int fireball;
    int freeze;
    int heal;
    
    int hit = rand();
    int hitpoints = 100;
    int life = 100;
    
    
// Attack
if(choice == "Class1")
{
    while(hitpoints > 1)
    {
     
     char move;
     cout << "Would you like to Slash(s) Strike(t) or Lunge(l)\n";
     cin >> move;
     
     if(move == 's' || move == 'S')
     {
     int slash =rand() % 30 + 1;
     life = (life - slash);
     cout << "You hit a " << slash << endl;
     cout << "Lancelot has " << life << " hitpoints left." << endl;
     
     int hit = rand() % 40 + 1;
     hitpoints = (hitpoints - hit);
     cout << "Lancelot has hit " << hit << endl;
     cout << "You have " << hitpoints << "hitpoints left." << endl;
     }
    
    if(move == 't')
    {
    int stab = rand() % 15 + 1;
    life = (life - stab);
    cout << "You hit a " << stab << endl;
    cout << "Lancelot has " << life << " hitpoints left." << endl;
    
     int hit = rand() % 40 + 1;
     hitpoints = (hitpoints - hit);
     cout << "Lancelot has hit " << hit << endl;
     cout << "You have " << hitpoints << "hitpoints left." << endl;
    }
    
    if(move == 'l' || move == 'L')
    {
    int lunge =rand() % 25 + 1;
    life = (life - lunge);
    cout << "You hit a " << lunge << endl;
    cout << "Lancelot has " << life << " hitpoints left." << endl;
    
     int hit = rand() % 40 + 1;
     hitpoints = (hitpoints - hit);
     cout << "Lancelot has hit " << hit << endl;
     cout << "You have " << hitpoints << "hitpoints left." << endl;
    }
    
    }
    
}

if(choice == "Class2")
{
 while(hitpoints > 1)
 {
  char move1;
  cout << "What would you like to do Fireball(f) Heal (h) Freeze (r)";
  cin >> move1;
  
  if (move1 == 'f' || move1 == 'F')
  {
   int fireball = rand() % 20 + 1;
   life = (life - fireball);
   cout << "You hit a " << fireball << endl;
   cout << "Lancelot has " << life << "hitpoints left.\n";
   
   int hit = rand() % 40 + 1;
   hitpoints = (hitpoints - hit);
   cout << "Lancelot has hit " << hit << endl;
   cout << "You have " << hitpoints << "hitpoints left." << endl; 
  }
  
  if (move1 == 'h' || move1 == 'H')
  {
   int heal = (rand() % 10) + 5;
   hitpoints = (hitpoints + heal);
   cout << "You healed your self, you have " << hitpoints << "hitpoints\n";
   
   int hit = rand() % 40 + 1;
   hitpoints = (hitpoints - hit);
   cout << "Lancelot has hit " << hit << endl;
   cout << "You have " << hitpoints << "hitpoints left." << endl; 
  }
  
  if(move1 == 'r' || move1 == 'R')
  {
   int freeze = rand() % 35 + 1;
   life = (life - freeze);
   cout << "You hit " << freeze << endl;
   cout << "Lancelot has " << life << "hitpoints left.\n";
   
   int hit = rand() % 40 + 1;
   hitpoints = (hitpoints - hit);
   cout << "Lancelot has hit " << hit << endl;
   cout << "You have " << hitpoints << "hitpoints left." << endl;
  }     
 
  } 
 
}
     
     
    system("pause");
    return 0;
}


The program keeps crashing everytime, I use Class2
Last edited on
You'll have to give more info about the problem. For example the last output you see.
Also, it's time you started using functions..
1
2
3
4
5
6
7
8
9
10
11
    if(choice == "Class1")
    for(iter = class1.begin(); iter != class1.end(); ++iter)
    cout << *iter << endl;
    
    if(choice == "Class2")
    for(iter2 = class2.begin(); iter != class2.end(); ++iter2)
    cout << *iter2 << endl;
    
    if(choice == "Class3")
    for(iter3 = class3.begin(); iter != class3.end(); ++iter3)
    cout << *iter3 << endl;


Here's your problem. Tell me, if the choice was Class2 or Class3, when will the for loop ever end?

You may be interested to know that I used valgrind to tell me what the problem was, and gdb to help. Debugging tools are very useful.
Last edited on
@Moschops
Man, I wouldn't have noticed that ever. You must be wearing some special glasses...

@Assassin7257
I like the game story. Reminds me of the past. (I hope I don't sound overly melancholic.)
Full disclosure; even when valgrind pointed out the bad line to me and informed me that it was trying to read uninitialised memory, it still took me bloody ages :)
^That's also why you use local variables in your for loops like that...
it took me almost 3 minutes but i finally saw it.
Topic archived. No new replies allowed.