Program Closes when it isn't supposed to

I am making a simple text RPG in C++, and it closes as soon as it starts a battle with something. It works perfectly fine, but when it goes into the battle, it shows the very first line and I get a windows crash message saying "Text Adventure.exe has stopped working" ... any ideas on the problem? I'm not sure if the text battle works (I can't debug) because of the error. I'll post the code here:

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
#include "stdio.h"
#include "iostream"
#include "clearscreen.h"

void forest();
void desert();

using namespace std;

struct enemystruct { char name[15]; int hp; int ap; int wh; int attack; int gold; int run;};
enemystruct e [4] = {
            {"Bandit", 100, 10, 20, 20, 50, 1},
            {"Goblin", 100, 15, 20, 20, 50, 1}};
            
typedef void(*maptype) ();
struct placestruct { char name[50]; maptype map;};
placestruct p [2] = {
            {"Forest", &forest},
{"Desert", &desert}};
int hp = 100, mhp = 100, gold = 0, attack = 20, armor = 10, weapon = 25, run, damage, valid, next, ehp, n, i;
char wname[20] = "Fisticuffs!";

void pressenter()
{
     printf("\n\n[Press Enter to Continue]");
     cin.get();
     ClearScreen();
}

void choice (int *n, int max)
{
if (!max) printf ("\"Where are we goin' first, captain?\"\n\n");
for (i = 0, *n = 0; i < 4 && !max; i++)
printf ("[%i] %s\n", i + 1, p [i].name);
if (!max) max = 4;
scanf ("%i", n);
fflush (stdin);
if (*n > 0 && *n <= max) valid = 1;
}

void battle(int m)
{
     run = 1;
     ehp = e[m].hp;
     while(ehp > 0 && hp > 0 && (run < 13 || !e [m].run))
     {
               for (valid = 0; !valid; choice (&n, 3))
               {
                   ClearScreen();
                   printf("You are in combat with: %s\n", e [m].name);
                   printf("Enemy HP: %s%i%sYour HP: %s%i/%i\n\t", ehp, "\t\t", hp, mhp);
                   printf("Enemy Armor: %s%i%sYour Armor: %s%i\n\t", e [m].ap, "\t\t", armor);
                   printf("\n\n[1] Attack\n[2] Run\n\n");
               }
               if (n == 1 && hp > 0)
               {
                     damage = weapon - e [m].ap / 4;
                     printf("You hit for %i damage\n\n", damage);
                     if ((ehp -= damage) < 1)
                     {
                              printf("You have killed the enemy\nYou gain %i gold\n", e [m].gold);
                              gold += e [m].gold;
                              hp += 5;
                              mhp += 5;
                     }
               }
               if (n == 1 && hp < 1)
               {
                     damage = e [m].wh - armor / 4;
                     printf("Enemy hits for %i damage\n", damage);
                     if ((hp -= damage) < 1) printf("\nYou have been destroyed.\n");
               }
               if (n == 2 && (run = rand() % 26) > 12 && e [m].run)
               {
               printf("You successfully ran away!");
               }
               if (n == 2 && (run < 13 || e [m].run))
               {
               printf("You failed to run away!");
               }
               pressenter();
     }    
}

void forest()
{
     ClearScreen();
    printf("You head through the dark forest, but see some strange eyes from up ahead...");
     pressenter();
     battle(1);
}

void desert()
{
ClearScreen();
printf("You see a few men up ahead, running toward you.");
pressenter();
battle(1);
battle(1);
}

int main()
{
int Choice;
printf("Simple Text Adventure\n\n");
pressenter();
for (valid = 0; !valid; choice (&n, 0)) ClearScreen();
    next = n;
    while (hp > 0) if (hp > 0) (*(p [next - 1].map)) ();
}


Thanks for the help!
The program is doing too much work at once. Try doing it in different sections at a time so it doesnt have to work so hard. BTW awesome RPG!
printf("Enemy HP: %s%i%sYour HP: %s%i/%i\n\t", ehp, "\t\t", hp, mhp);

ehp is an int, but your format specifier is %s. Treat an int as the address of string and strange things happens.

Also, your format string specifies that you supply 6 arguments and you only supply 4.
Topic archived. No new replies allowed.