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...
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
}
#include<iostream>
#include<cstdlib>
#include<ctime>
usingnamespace 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);
}
elseif (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.