while loop dis-functioning or random generator

write your question here.
I'm not sure what is wrong with my while loop.
the battle simulator isn't working as i thought it should.
Sometimes it will simple run through the different c-out puts as if there is no random generator when there is indeed one.
sometimes it will go on forever.
As well as sometimes it will say a complete wrong answer at the end: it'll say that there were 1494123145 when there was only 2 to begin with.
It also looks like it's not running the whole loop.
#include <iostream>
#include <string>
#include <random>
#include <ctime>

using namespace std;

int main()
{
cout << "the battle of ninjas and skeletons" << endl;

mt19937 randomEngine(time(0));
uniform_real_distribution<float> attack(0.0f, 1.0f);

//human properties
float ninjaHealth = 100.0f;
float currentNinjaHealth = ninjaHealth;

float ninjaAttack = 40.0f;
float ninjaAttackCritical = 65.0f;


//skeleton properties
float skeletonHealth = 75.0f;
float currentSkeletonHealth = skeletonHealth;

float skeletonAttack = 55.0f;
float skeletonAttackCritical = 80.0f;


//battle properties
char turn = 'N';
int numNinja;
int numSkeleton;
int startNinja = numNinja;
int startSkeleton = numSkeleton;


//battle attendants
cout << "How many ninjas will battle?" << endl;
cin>> numNinja;
cout << "How many skeletons will fight?" << endl;
cin >> numSkeleton;

cout << "Start of Combat!" << endl;

float ninjaAttackChance = attack(randomEngine);
float skeletonAttackChance = attack(randomEngine);

while ((numNinja >0) && (numSkeleton > 0)) {
if (turn = 'N') {
ninjaAttackChance;

if (ninjaAttackChance < .3f)
cout << "a ninja miss" << endl;
if ((ninjaAttackChance >= .4f) && (ninjaAttackChance <= .7f)) {
skeletonHealth -= ninjaAttack;
cout << "skeleton toke damage" << endl;
if (ninjaAttack >=.8f) {
skeletonHealth -= ninjaAttackCritical;
cout << "a skeleton toke a strike!" << endl;
if (skeletonHealth <= 0.0f) {
numSkeleton--;
cout << "a skeleton has fallen!" << endl;
currentSkeletonHealth = skeletonHealth;
}
}
}
}
else
if (skeletonAttackChance < .4f) {
cout << "the skeleton misses!" << endl;
if ((skeletonAttackChance >= .5f) && (skeletonAttackChance <=.8f)) {
ninjaHealth -= skeletonAttack;
cout << "a ninja was hit" << endl;
if (skeletonAttackChance >= .9)
ninjaHealth -= skeletonAttackCritical;
cout << "the ninja toke a blow!" <<endl;
if (ninjaHealth <= 0) {
numNinja--;
cout << "a ninja has fell!" << endl;
currentNinjaHealth = ninjaHealth;
}
}
}
turn = 'N';
}

cout << "the battle has ended" << endl;

//battle results

if (numNinja >0) {
cout << "the ninjas have emerge victorious!" << endl;
cout << "there are" << startNinja - numNinja << "ninjas left alive " << endl;
} else {
cout << "the skeletons have won!" << endl;
cout << "there are " <<startSkeleton - numSkeleton << " skeletons left and" << endl;
}

cout << startNinja - numNinja << " Ninjas and " << startSkeleton - numSkeleton << " skeletons lost their lives" << endl;



return 0;
}
your code, indented and with some comments
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
#include <ctime>
#include <iostream>
#include <random>
#include <string>

using namespace std;

int main() {
	cout << "the battle of ninjas and skeletons" << endl;

	mt19937 randomEngine(time(0));
	uniform_real_distribution<float> attack(0.0f, 1.0f);

	// human properties
	float ninjaHealth = 100.0f;
	float currentNinjaHealth = ninjaHealth;

	float ninjaAttack = 40.0f;
	float ninjaAttackCritical = 65.0f;

	// skeleton properties
	float skeletonHealth = 75.0f;
	float currentSkeletonHealth = skeletonHealth;

	float skeletonAttack = 55.0f;
	float skeletonAttackCritical = 80.0f;

	// battle properties
	char turn = 'N';
	int numNinja;
	int numSkeleton;
	int startNinja = numNinja; //numNinja is uninitialised, so startNinja would be uninitialised too
	int startSkeleton = numSkeleton; //numSkeleton is uninitialised, so startSkeleton would be uninitialised too

	// battle attendants
	cout << "How many ninjas will battle?" << endl;
	cin >> numNinja;
	cout << "How many skeletons will fight?" << endl;
	cin >> numSkeleton;

	cout << "Start of Combat!" << endl;

	float ninjaAttackChance = attack(randomEngine);
	float skeletonAttackChance = attack(randomEngine);

	while((numNinja > 0) && (numSkeleton > 0)) {
		if(turn = 'N') { //warning: suggest parentheses around assignment used as truth value [-Wparentheses]
			ninjaAttackChance; //warning: statement has no effect [-Wunused-value]

			if(ninjaAttackChance < .3f)
				cout << "a ninja miss" << endl;
			if((ninjaAttackChance >= .4f) && (ninjaAttackChance <= .7f)) {
				skeletonHealth -= ninjaAttack;
				cout << "skeleton toke damage" << endl;
				if(ninjaAttack >= .8f) {
					skeletonHealth -= ninjaAttackCritical;
					cout << "a skeleton toke a strike!" << endl;
					if(skeletonHealth <= 0.0f) {
						numSkeleton--;
						cout << "a skeleton has fallen!" << endl;
						currentSkeletonHealth = skeletonHealth;
					}
				}
			}
		} else if(skeletonAttackChance < .4f) {
			cout << "the skeleton misses!" << endl;
			if((skeletonAttackChance >= .5f) && (skeletonAttackChance <= .8f)) {
				ninjaHealth -= skeletonAttack;
				cout << "a ninja was hit" << endl;
				if(skeletonAttackChance >= .9)
					ninjaHealth -= skeletonAttackCritical;
				cout << "the ninja toke a blow!" << endl;
				if(ninjaHealth <= 0) {
					numNinja--;
					cout << "a ninja has fell!" << endl;
					currentNinjaHealth = ninjaHealth;
				}
			}
		}
		turn = 'N';
	}

	cout << "the battle has ended" << endl;

	// battle results

	if(numNinja > 0) {
		cout << "the ninjas have emerge victorious!" << endl;
		cout << "there are" << startNinja - numNinja << "ninjas left alive " //startNinja was never initialised
		     << endl;
	} else {
		cout << "the skeletons have won!" << endl;
		cout << "there are " << startSkeleton - numSkeleton //startSkeleton was never initialised
		     << " skeletons left and" << endl;
	}

	cout << startNinja - numNinja << " Ninjas and " //startNinja was never initialised
	     << startSkeleton - numSkeleton << " skeletons lost their lives" //startSkeleton was never initialised
	     << endl;

	return 0;
}

// battle properties
char turn = 'N';
int numNinja;
int numSkeleton;
int startNinja = numNinja; //numNinja is uninitialised, so startNinja would be uninitialised too
int startSkeleton = numSkeleton; //numSkeleton is uninitialised, so startSkeleton would be uninitialised too

//the user is inputting the number of skeletons so i thought it wasn't suppose to be initialized




while((numNinja > 0) && (numSkeleton > 0)) {
if(turn = 'N') { //warning: suggest parentheses around assignment used as truth value [-Wparentheses]
ninjaAttackChance; //warning: statement has no effect [-Wunused-value]

can you please explain what the warnings means?
i don't understand why 'ninjaAttackChance' doesn't have an affect when i've initialized it like this:
float ninjaAttackChance = attack(randomEngine);
> the user is inputting the number of skeletons so i thought it wasn't suppose
> to be initialized
code executes from top to bottom. You use `numSkeleton' before you read the value from the user.

> //warning: suggest parentheses around assignment used as truth value
= is assignment
== is comparison
There you are assigning 'N' to turn.

> i don't understand why 'ninjaAttackChance' doesn't have an affect when
> i've initialized it like this:
Go yell "42" in the middle of the street and tell me what effect did it have.
You are not doing any operation there.
Topic archived. No new replies allowed.