Need help

I am stuck with the program which asks the user what to do in a fighting match. either punch(50% accuracy, eliminate about 5 to 20% power of opponent), kick (25% accuracy, but can eliminate about 25-50% opp's power. Do it with a loop until you can defeat the opponent (power = 0) and calculate how many kick and punch are made. My program only works for one time, then exit. And I'm confused about how to get the remain power after the first punch to calculate for the second punch. (suppose punch1 took 20 power of your opp, he is 80 left. punch2 took another 20, now he is 60power.
Thank everyone so much...

#include<iostream>
#include<cstdlib>
#include<ctime>

using namespace std;

int punch()
{
int total, accuracy, inj = 0, punch = 0;
accuracy = rand() % 100 + 1;
if (accuracy < 50)
{
punch++;
inj = rand() % 16 + 5;
total = 100 - inj;
cout << "Nice punch. The opponent has " << total << " health left." << endl;
}
else
{
inj = 0;
total = 100;
cout << "Try again." << endl;
}
return total;
}

int kick()
{
int total, accuracy, inj = 0, kick = 0;
accuracy = rand() % 100 + 1;
if (accuracy < 25)
{
kick++;
inj = rand() % 26 + 25;
total = 100 - inj;
cout << "Nice kick. The opponent has " << total << " health left." << endl;
}
else
{
inj = 0;
total = 100;
cout << "Try again." << endl;
}
return total;
}
int main()
{
int health = 100, injury = 0;
char att;
srand(time(0));

while (health > 0)
{
cout << "What will you do? Punch, Kick or Defend? ";
cin >> att;

if (att == 'P')
{
health = punch();
}
else if (health == 'K')
{
health = kick();
}
else
{
return 0;
}
system("pause");
return 0;
}
cout << "KO." << endl;
cout << number of kick and punch//I'm not sure where to get this data from the punch() and kick() functions
}
Your problem is that kick and punch are local variables within the respective functions. They go out of scope when the functions exit.

The easy (but not recommended) solution is to make those variables global.

The better solution is to declare those variables in main and pass them by reference to the respective functions.

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
#include<iostream>
#include<cstdlib>
#include<ctime>

using namespace std;

int punch(int & punches)
{   int total, accuracy, inj = 0;
    accuracy = rand() % 100 + 1;
    if (accuracy < 50)
    {   punches++;
        inj = rand() % 16 + 5;
        total = 100 - inj;
        cout << "Nice punch. The opponent has " << total << " health left." << endl;
    }
    else
    {   inj = 0;
        total = 100;
        cout << "Try again." << endl;
    }
    return total;
}

int kick(int & kicks)
{   int total, accuracy, inj = 0; 
    accuracy = rand() % 100 + 1;
    if (accuracy < 25)
    {   kicks++;
        inj = rand() % 26 + 25;
        total = 100 - inj;
        cout << "Nice kick. The opponent has " << total << " health left." << endl;
    }
    else
    {   inj = 0;
        total = 100;
        cout << "Try again." << endl;
    }
    return total;
}
int main()
{   int health = 100, injury = 0;
    int punches = 0, kicks = 0;
    char att;

    srand(time(0));
    while (health > 0)
    {   cout << "What will you do? Punch, Kick or Defend? ";
        cin >> att;
        if (att == 'P')
        {   health = punch(punches);
        }
        else if (health == 'K')
        {   health = kick(kicks);
        }
        else
        {   return 0;
        }
        system("pause");
        return 0;
    }
    cout << "KO." << endl;
    cout << "Punches = " << punches << endl;
    cout << "Kicks = " << kicks << endl;
    system ("pause");
    return 0;
}


PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.


Thank AbstractionAnon!
Topic archived. No new replies allowed.