5 Replies - 464 Views - Last Post: 19 February 2011 - 10:05 AM Rate Topic: -----

#1 leafdj  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 43
  • Joined: 05-February 11

Problems with setting a variable to itself with if statements?

Posted 17 February 2011 - 05:02 PM

I'm using the if statements in the style of:

xVar = (x > y) ? x: y;


And my question is just if you can use this to assign a variable to itself. For example, I have a war game where you send soldiers out to fight, and I need a statement that prevents more soldiers from dying than you sent out, so I want to use something like this.
soldiersDead = (soldiersDead >soldiersSent) ? soldiersSent: soldiersDead;


If there are more dead soldiers than you sent, then it is set to the amount of soldiers that you sent, otherwise it is set to the amount of soldiers that died.

My program has an error where all the swordsmen you send die no matter what, and I'm wondering if it has something to do with this statement.

Is This A Good Question/Topic? 0
  • +

Replies To: Problems with setting a variable to itself with if statements?

#2 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1623
  • View blog
  • Posts: 5,710
  • Joined: 03-August 09

Re: Problems with setting a variable to itself with if statements?

Posted 17 February 2011 - 05:12 PM

1) this is called the ternary operator
2) what is the whole code, it's hard to tell from just 1 line
Was This Post Helpful? 0
  • +
  • -

#3 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3100
  • View blog
  • Posts: 10,889
  • Joined: 08-August 08

Re: Problems with setting a variable to itself with if statements?

Posted 17 February 2011 - 05:13 PM

Put a break point in. What are the values when you get to that point?
Was This Post Helpful? 0
  • +
  • -

#4 leafdj  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 43
  • Joined: 05-February 11

Re: Problems with setting a variable to itself with if statements?

Posted 17 February 2011 - 05:33 PM

#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>

int main (void)
{
	using std::cout;
	using std::cin;
	using std::time;
	using std::srand;
	using std::rand;
	using std::string;

	string name;

	srand((unsigned int) time(0));

	bool end = false;
	bool lost = false;

	int archers = 50;
	int cats = 100;
	int swords = 30;

	int aDead = 0;
	int cDead = 0;
	int sDead = 0;
	int aGDead = 0;
	int cGDead = 0;
	int sGDead = 0;

	int menuChoice;

	int gArchers = rand() % 51 + 20;
	int gSwords = rand() % 41 + 10;
	int gCats = rand() % 101 + 50;

	int archerMenu, catMenu, swordMenu, fightMenu;

	cout << "Halt! What is your name, soldier?\n";
	cin >> name;
	cout << "Oh! General " <<name <<"! I apologize for my insolence.\n"
		<< "Come, the troops are waiting.\n\n";

	while (end == false)//main game loop
	{
		int aSent = 0;
		int sSent = 0;
		int cSent = 0;
		cout << "You have " <<archers <<" archers, " <<swords <<" swordsmen, and " <<cats <<" catapults.\n"
			<< "The enemy has " <<gArchers <<" archers, " <<gSwords << " swordsmen, and " <<gCats <<" catapults.\n";

		do{
			int i = 1;

			if (archers > 0 && archers - aSent != 0)
			{
				archerMenu = i;
				cout << "[" <<i <<"] - Send Archers.\n";
				i++;
			}
			else archerMenu = 0;

			if (swords > 0 && swords - sSent != 0)
			{
				swordMenu = i;
				cout << "[" <<i <<"] - Send Swordsmen.\n";
				i++;
			}
			else swordMenu = 0;

			if (cats > 0 && cats - cSent !=0)
			{
				catMenu = i;
				cout <<"[" <<i <<"] - Send Catapults.\n";
				i++;
			}
			else catMenu = 0;

			fightMenu = i;
			cout << "[" <<i <<"] - Go Fight!\n";
			cin >> menuChoice;

			if (menuChoice == archerMenu)
			{
				do{
				cout << "How many archers would you like to send?\n";
				cin >>aSent;
				} while (!(aSent > -1 && aSent <= archers));
			}
			else if (menuChoice == swordMenu)
			{
				do{
					cout <<"How many swordsmen would you like to send?\n";
					cin>> sSent;
				} while (!(sSent > -1 && sSent <= swords));
			}
			else if (menuChoice == catMenu)
			{
				do{
					cout <<"How many catapults would you like to send?\n";
					cin>> cSent;
				}while (!(cSent > -1 && cSent <= cats));
			}
		}while (menuChoice != fightMenu);

		cout << "Entering Battle...\n\n";

		//Each catapult kills 2 archers.
		aDead = (gCats * 2);
		aGDead = (cSent * 2);

		//Each archer kills 3 swordsmen.
		sDead = (gArchers * 3);
		sGDead = (aSent * 3);

		//Each swordsman kills 1 catapult.
		cDead = (gSwords);
		cGDead = (sSent);

		aDead = (aDead > aSent) ? aSent: aSent - aDead;
		sDead = (sDead > sSent) ? sSent: sSent - sDead;
		cDead = (cDead > cSent) ? cSent: cSent - cDead;

		gSwords = (gSwords > sGDead) ? gSwords - sGDead: 0;
		gCats = (gCats > cGDead) ? gCats - cGDead: 0;
		gArchers = (gArchers > aGDead) ? gArchers - aGDead: 0;

		cout << "It was a long battle...\n"
			<<aDead << " archers died,\n"
			<<sDead << " swordsmen died, and\n"
			<<cDead << " catapults were broken.\n\n"

			<<"You killed " <<aGDead <<" archers,\n"
			<< sGDead << " swordsmen,\n"
			<< "and you broke " <<cGDead <<" catapults.\n\n";

		if ((archers + swords + cats) == 0)
			end = lost = true;

		else if ((gArchers + gSwords + gCats) == 0)
		{
			end = true;
			lost = false;
		}

	}//End of main game loop.

	if (lost == true)
		cout << "You and your friends are dead.\n No one likes you.\n Your pets are being skinned alive.\n You suck.\n Game Over.\n\n";

	else if (lost == false)
		cout << "Our invasion was successful!\n" <<"Kudos, General.\n" <<"You win!\n The end\n\n";

	return(0);
}


That's the whole code. At the point right after the logic statements, either everyone is dead ( if there were no soldiers to kill) or it's just a few of your soldiers.

I know I'm doing something stupid, but I just can't find it. this is actually my second go, I rewrote it quickly after having the problem before.
It doesn't help that my variables are a nightmare, but I just meant to whip it together quickly.
Was This Post Helpful? 0
  • +
  • -

#5 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3100
  • View blog
  • Posts: 10,889
  • Joined: 08-August 08

Re: Problems with setting a variable to itself with if statements?

Posted 17 February 2011 - 05:52 PM

This looks wrong:
		aDead = (gCats * 2);
		aGDead = (cSent * 2);

		//Each archer kills 3 swordsmen.
		sDead = (gArchers * 3);
		sGDead = (aSent * 3);


I would think you'd do something like this:
		aDead += 2;
		aGDead += 2);

		//Each archer kills 3 swordsmen.
		sDead +=  3;
		sGDead += 3;


Was This Post Helpful? 0
  • +
  • -

#6 leafdj  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 43
  • Joined: 05-February 11

Re: Problems with setting a variable to itself with if statements?

Posted 19 February 2011 - 10:05 AM

View PostCTphpnwb, on 17 February 2011 - 05:52 PM, said:

This looks wrong:
		aDead = (gCats * 2);
		aGDead = (cSent * 2);

		//Each archer kills 3 swordsmen.
		sDead = (gArchers * 3);
		sGDead = (aSent * 3);


I would think you'd do something like this:
		aDead += 2;
		aGDead += 2);

		//Each archer kills 3 swordsmen.
		sDead +=  3;
		sGDead += 3;



No luck. Those values make it so that it increases by the amount each time the loop goes through, but not for every soldier I send.

It definitely has something to do with the way I initialize my variables before or during the loop. Looks like it's time to do some math!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1