Program hangs

I'm having a hard time figuring out why my program just hangs after I enter my skeleton and human.

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

#include "stdafx.h"
#include <iostream>
#include <random>
#include <ctime>
using namespace std;

int main()
{
	mt19937 randomGenerator(time(NULL));
	uniform_real_distribution<float> monsterAttack(0.00F, 0.25F);

	int numSkeleton = 0, numHuman = 0;
	float skeletonHeath = 100.00f, humanHealth = 100.00f;
	bool gameOver = true;

	cout << "Type number of skeleton: ";
	cin >> numSkeleton;
	cout << "Type number of human: ";
	cin >> numHuman;

	while (gameOver)
	{ 
		skeletonHeath -= monsterAttack(randomGenerator);
		humanHealth -= monsterAttack(randomGenerator);

		if (skeletonHeath == 0.00f) {
			numSkeleton--;
			skeletonHeath = 100.00f;
		}

		if (humanHealth == 0.00f){
			numHuman--;
			humanHealth = 100.00f;
		}
		
		if (numSkeleton == 0) {
			cout << "Human wins: humans have " << numHuman << " remaining" << endl;
			cout << numHuman << " remaining";
			gameOver = false;
		}
		else if (numHuman == 0) {
			cout << "Human wins: humans have " << numHuman << " remaining";
			cout << numSkeleton << " remaining";
			gameOver = false;
		}
	}

    return 0;
}

Last edited on
I liked your previous version better, with the "So-and-so wins" part after the loop. In that version you needed to change the || to an &&. But there was/is another problem. In general you shouldn't test floating point values for equality. It's better to test them for <= 0 instead of == 0. Since you're subtracting random values from 0 to 0.25 it's unlikely to become exactly zero.

Also note that your output strings both say "humans win".

And you can set the initial random seed using random_device instead of time if you want:

 
	mt19937 randomGenerator(random_device{}());

Last edited on
Thank you I will remember not to use equality to compare floating point value to exactly 0. I followed suggestions and the program does indeed work.

The reason I was using || is because in my head I wanted it to break out of the loop as soon as either one reaches 0 health. it sounded good in my head but didn't work code wise.


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
#include "stdafx.h"
#include <iostream>
#include <random>
#include <ctime>
using namespace std;

int main()
{
	mt19937 randomGenerator(random_device{}());
	uniform_real_distribution<float> monsterAttack(0.00f, 0.25f);

	int numSkeleton = 0, numHuman = 0;
	float skeletonHeath = 100.00f, humanHealth = 100.00f;

	cout << "Type number of skeleton: ";
	cin >> numSkeleton;
	cout << "Type number of human: ";
	cin >> numHuman;

	while (numSkeleton > 0 && numHuman > 0)
	{ 
		skeletonHeath -= monsterAttack(randomGenerator);
		humanHealth -= monsterAttack(randomGenerator);

		if (skeletonHeath <= 0.00f) {
			numSkeleton--;
			skeletonHeath = 100.00f;
		}

		if (humanHealth <= 0.00f){
			numHuman--;
			humanHealth = 100.00f;
		}
		
	}

	if (numSkeleton == 0) {
		cout << "Human wins: humans have " << numHuman << " remaining" << endl;
		cout << "Skeletons have " << numSkeleton <<  " remaining";
	}
	else if (numHuman == 0) {
		cout << "Skeleton wins: skeletons have " << numSkeleton << " remaining";
		cout << "Humans have " << numHuman << " remaining";
	}

    return 0;
}


Last edited on
Nevermind I'm an idiot I thought about it again and see exactly why or operator wouldn't work. I was thinking about English phrase "If either one reaches 0 health, break out of loop" either one means to use "or" in my brain.

Thank you sir for your help!
Last edited on
That one feels backwards to pretty much everyone at first. But when you think about it in "programming" terms it makes sense. :-)
Topic archived. No new replies allowed.