Textbook Example Displaying Bugs

Got this example piece of code from a textbook I'm learning from and it's displaying some wierd bugs. It plays a game where soldiers are sent to battle and the casualties are displayed and only certain soldier types can take out other enemy soldier types but if you play a game and get to a point where, for example, both teams only have archers left it will state that other soldiers died in the battle despite there being none left. I won't put the whole code in but just what I think is the offending section but I can put the rest in if requested:

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
cout << "\nEntering battle...\n";
			int archers_dead, catapults_dead, swordsmen_dead;
			int g_archers_dead, g_catapults_dead, g_swordsmen_dead;

			archers_dead = 2 * g_catapults;
			catapults_dead = g_swordsmen;
			swordsmen_dead = 3 * g_archers;

			g_archers_dead = 2 * catapults_sent;
			g_catapults_dead = swordsmen_sent;
			g_swordsmen_dead = 3 * archers_sent;

			archers = (archers_dead < archers) ? archers - archers_dead : 0;
			catapults = (catapults_dead < catapults) ? catapults - catapults_dead : 0;
			swordsmen = (swordsmen_dead < swordsmen) ? swordsmen - swordsmen_dead : 0;

			g_archers = (g_archers_dead < g_archers) ? g_archers - g_archers_dead : 0;
			g_catapults = (g_catapults_dead < g_catapults) ? g_catapults - g_catapults_dead : 0;
			g_swordsmen = (g_swordsmen_dead < g_swordsmen) ? g_swordsmen - g_swordsmen_dead : 0;

			cout << "It was a long battle.\n"
				<< archers_dead << " archers died.\n"
				<< catapults_dead << " catapults died.\n"
				<< swordsmen_dead << " swordsmen died.\n";
			
			if ((archers + catapults + swordsmen) == 0)
				end = lost = true;

			else if ((g_archers + g_catapults + g_swordsmen) == 0)
			{
				end = true;
				lost = false;
			}
	}

	if (lost)
	{
		cout << "\nYou lost, try again next time.\n\n";
		return 0;
	}
	cout << "\nCongratulations! You won!\n\n";
	return 0;
	}
Bump?
Yes, it sure is buggy. What's your question?
What is wrong with the code basically. I got it from a textbook so expected it to be correct and work but it doesn't, so I'm not learning much when the examples it gives are wrong. Just want to know how to fix it.
Walk through the code yourself or use a debugger and see. Make sure to write down when the variables change.

By the way, are you sure that is "wrong"? As in, did the book specifically say that wouldn't happen or what?
I think it's because they (the book people) did not put in all the error checking possible.
In this game swordsmen are killed by archers.

So for example, if you have no swordsmen left and the enemy has 8 archers left .
then swordsmen_dead = 3 * g_archers;
will give a value of 24 swordmen on your side killed - even though as we say you
have no swordsmen left to kill.


I think that jaeger can give it a go to see if he can add more error checking and other
logic to the game.
Search for errata of your book.
The code is most definitely missing some error checking and I am coming up with strange results when people are dead, as guestgulkan says. The other issue is that on some occasions the game leads to a stalemate when both teams have only the same units left. If both teams have only archers left you can't win or lose because archers can't kill eachother. All in all, I think it's a poorly thought through example and I'm really dissapointed in this book as I seem to be left with more questions about why the code I'm writing is wrong than correct code and knowledge of how it works.

I'll give it a shot to make it better but I've only just started learning, so thanks for the help guys.

Also, this is a second edition of the book and I think it's the most recent as well so I don't think there is an errata of it after a quick search. Any bugs and glitches I'm just going to have to deal with until I can find a better book.
I'm thinking that the problem lies within the way the deaths are calculated rather than the logic and error checking. If the calculation was fixed it may solve the problem. I haven't done maths in a while and need to take a refresher but can anyone think of a calculation that would take into account the amount of soldiers the player has and then calculates based on that amount and the amount of enemy soldiers?

Promise I won't ask for maths again, just this once. Really just want to get this program fixed so I can move on from it and maybe learn some maths along the way.
Topic archived. No new replies allowed.