When I ask the user to enter in a number of games,
Say I wish to do 5 fights.

It runs the fight, then stops after 1 player wins.
It doesn't loop back around, DO health levels need to be reset?
I also think I did my coin flip wrong. What do you think?

Also any advice or tips is helpful and appreciated.

#include <iostream>
#include <string>
#include <time.h>

using namespace std;


int main()
{

	srand((unsigned)time(NULL)); 

	//Variables
	int numberofFights =0;
	int fighter1Wins = 0;
	int fighter2Wins = 0;
	int coinToss = 0;

	//Fighter 1
	string fighter1Name = "Itchy";
	int fighter1Health = 100;
	int fighter1Strength = 10;
	bool fighter1Turn = true;
	int fighter1Accuracy = 25;

	//Fighter 2
	string fighter2Name = "Scratchy";
	int fighter2Health = 100;
	int fighter2Strength = 10;
	bool fighter2Turn = false;
	int fighter2Accuracy = 50;

	cout << "Please enter the name, health, strength, and accuracy for fighter 1" << endl;
	cin >> fighter1Name >> fighter1Health >> fighter1Strength >> fighter1Accuracy;

	cout << "Please enter the name, health, strength, and accuracy for fighter 2" << endl;
	cin >> fighter2Name >> fighter2Health >> fighter2Strength >> fighter2Accuracy;

	cout << "Please enter number of fights desired: " << endl;
	cin >> numberofFights;

	for (int i=0; i < numberofFights; i++)
	
		
		// coin toss
	int coinToss=rand()%100 + 1; 
	if (coinToss <= 50)
	{
	fighter1Turn = true;
	fighter2Turn = false;
	}
	else if (coinToss > 50)
	{
	fighter1Turn = false;
	fighter2Turn = true;
	}


	
	
	while (fighter1Health > 0 && fighter2Health > 0)
	{
		if (fighter2Turn == true)
		{
			int hitChance = rand() % 100 + 1;
			if (hitChance <= fighter2Accuracy)
			{
				fighter1Health = fighter1Health - fighter2Strength;
				cout << fighter2Name << " just hit " << fighter1Name << " causing " 
					<< fighter2Strength << " damage, reducing his health to " << fighter1Health 
					<< "." << endl;
			}
			else
			{
				cout << fighter2Name << " just missed!" << endl;
			}
			fighter2Turn = false;
			fighter1Turn = true;
		}
		else if (fighter1Turn == true)
		{
			int hitChance = rand() % 100 + 1;
			if (hitChance <= fighter1Accuracy)
			{
				fighter2Health = fighter2Health - fighter1Strength;
				cout << fighter1Name << " just hit " << fighter2Name << " causing " 
					<< fighter1Strength << " damage, reducing his health to " << fighter2Health 
					<< "." << endl;
			}
			else
			{
				cout << fighter1Name << " just missed!!!" << endl;
			}
			fighter2Turn = true;
			fighter1Turn = false;
		}
		}
	if (fighter1Health > 0)
	{
	fighter1Wins++;
	}
	else if (fighter2Health > 0)
	{
	fighter2Wins++;
	}

	
	
	cout << "Fighter 1 Wins: " << fighter1Wins << endl;
	cout << "Fighter 2 wins: " << fighter2Wins << endl;
	

	system("pause");
}

Thanks for taking a look

Recommended Answers

All 3 Replies

Because of this right here this code won't compile:

for (int i=0; i < numberofFights; i++)
	
		
		// coin toss
	int coinToss=rand()%100 + 1;

Why don't you try extracting the problem logic into a function and call that 5 times?

You don't have a loop that runs the fight 5 times. All you have is a loop that tosses the coin 5 time. Think carefully about your loop, braces, and indentation.

I think you need to use "{" and "}" after for (int i=0; i < numberofFights; i++), because as I know funtions liek these execute just one line after for( ; ; ) statment.
So as it's empty, aplicasion counts up i value to numberofFights value and then continues to line int coinToss=rand()%100 + 1;

so it has to bee like:

for (int i=0; i < numberofFights; i++)
{
		// coin toss
	int coinToss=rand()%100 + 1;
//...
}

Why do you use { and } for WHILE, but not for FOR? :)

I hope it will help you :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.