Core dumped after do-while loop

Hey all, First post here. I've only been at this a few days, probably about 5-6 hours or so in all. I've written a shoddy little "game" if you will but I'm having some questions. It runs fine but after compiling with g++ the game runs fine until you go to exit. I just get a line that says
Illegal instruction (core dumped)
I'm really curious as to why this is showing up.

Here's the code to the game, If someone else has a second, I would love to learn why this is doing what its doing.

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
#include <iostream>
#include <cstdlib>
#include <limits>
using namespace std;


struct fighters {
 string name;
 int health;
 int health_max;
 int mana;
 int mana_max;
 int level;
 int xp;
 int strength;
 int endurance;
 int agility;
 int intelligence;
 int dmg_max;
 int dmg_min;
} player, nonplayer;

int damageroll (int min, int max) {
 srand(time(NULL));
 int random_int = -1;
 while ( random_int < min ) {
  random_int = (rand()%max);
 }
 return (random_int);
}

fighters post_fight(fighters a) {
 if (a.xp > 10 ) {
  cout << "Congrats on leveling up! You are now level "<< a.level++ << "!\n";
  a.mana = a.mana + 5;
  a.mana_max = a.mana_max + 5;
  a.xp = a.xp - 10;
  a.strength = a.strength + 1;
  a.endurance = a.endurance + 1;
  a.agility = a.agility + 1;
  a.intelligence = a.intelligence + 1;
         a.dmg_min = a.strength;
         a.dmg_max = a.strength * 2;
  a.health = a.health + 10;
  a.health_max = a.endurance * 10;
 }
 int rest;
 rest = damageroll(1,10);
 cout << "You rest for a bit and gain " << rest << " HP and MP\n";
 a.mana = a.mana + rest;
 if (a.health > a.health_max ) { a.health = a.health_max; }
 if (a.mana > a.mana_max ) { a.mana = a.mana_max; }
 a.health = a.health + rest;
 cout << "\nCurrent stats: "
 << "\nHealth: " << a.health << "/" << a.health_max << "\tMana: "<< a.mana << "/" << a.mana_max
 << "\nStrength: " << a.strength << "\t\tMax damage: " << player.dmg_max
 << "\nAgility: " << a.agility << "\t\tMin damage: " << player.dmg_min
 << "\nIntelligence: " << a.intelligence ;

 return (a);

}

int combat_move() {
 cout << "\nWhat would you like to do?\n"
  << "1. Attack\t\t3. Rest\n"
  << "2. Magic\t\t4. Retreat\n";
 int move;
 cin >> move;
 return (move);
}

fighters fight(fighters a, fighters b) {
 int move;
 int dmg;
 cout << b.name  << " comes into view, it is level " << b.level;
 while (a.health > 0 && b.health >0 ) {
         cout << "\nYou have "<< a.health <<" health left, and " << a.mana <<" mana left.";
         cout << "\n" << b.name << " has " << b.health << " health remaning.";
  move = combat_move();
  switch (move) {
  case 1:
   dmg = damageroll(a.dmg_min, a.dmg_max);
   b.health = b.health - dmg;
   cout << "You hit the " << b.name << " for "<< dmg <<"!\n";
   break;
  case 2:
   dmg = damageroll((a.dmg_min - a.dmg_min) , a.dmg_max + 2);
   if (a.mana > 0) {
    b.health = b.health - dmg;
    a.mana--;
    cout << "You cast magic! for " << dmg << "!\n";
   }
   else { cout << "You're out of mana!\n";}
   break;
  case 3:
   post_fight(a);
   break;
  case 4:
   cout << "\n"<< b.name << " snorts and lets you leave.\n";
   return(a);
  }
  dmg = damageroll(b.dmg_min, b.dmg_max);
  a.health = a.health - dmg;
  cout << b.name << " hits you for " <<dmg <<"!\n";
 }
 if ( a.health > 0 ) {
  cout << "\n" << b.name << " has been defeated.\nHero " << a.name
  <<" You have " << a.health << " health left.\n" << endl;
  a.xp = a.xp + b.xp;
  cout << "You have gained " << b.xp <<" experience.\n";
 }
 else { cout << "\n You have died... \n"; }
 return(a);

}

fighters generateenemy (int difficulty) {
 string namepool[10] = { "Goat", "Wolf", "Lion", "Kobold", "Bear", "Angry Rob", "Big Goat", "Hard to see Mosquito", "Slime" ,"Ogre"};
        nonplayer.name = namepool[difficulty - 1];
        nonplayer.health = difficulty * 10;
        nonplayer.mana = difficulty * 10;
        nonplayer.level = difficulty;
        nonplayer.xp = difficulty * 2;
        nonplayer.dmg_min = difficulty + 1;
        nonplayer.dmg_max = (difficulty * 2) + 1;
 return nonplayer;
}



int main () {
 int difficulty;
 cout << "Welcome to the battlegame, Cause stuff\n"
 << "by John 4/29/12 - First attempt at a game\n\n";
 cout << "Please enter your name: ";
 getline (cin, player.name);
 player.mana = 10;
 player.mana_max = 10;
 player.level = 1;
 player.xp = 1;
 player.strength = 5;
 player.endurance = 5;
 player.agility = 5;
 player.intelligence = 5;
 player.dmg_min = player.strength;
 player.dmg_max = player.strength * 2;
 player.health = player.endurance * 10;
 player.health_max = player.endurance * 10;

 do {
  cout << "\n\nEnter difficulty (1-10) or 0 to exit: ";
  cin >> difficulty;
  player = fight(player, generateenemy(difficulty));
  player = post_fight(player);
 } while ( difficulty > 0 && player.health > 0 );
 cout << "\n\nThanks for playing \n";
 return 0;
}


If anyone else has any other techniques that could improve this code I would love to hear it. I'm just a wanna-be student who can't afford school.

Thanks for your time!
Your problem is on line 154/120 when the user enters 0 you have -1 for the index in namepool

The way to improve your code would be enter the world of C++ with classes and member functions and such.
The amount of time I've spend scouring this code and I didn't even hit that. Well thanks for explaining it in such a timely manner.

The downside of trying to be self taught is that without a visible milestone its hard to keep on task and learn things you don't know exist. I was excited to learn about structs, and got carried away applying them. I'll get this on the back burner and see about finishing the awesome tutorial!

Thanks again!
Topic archived. No new replies allowed.