2 Replies - 123 Views - Last Post: 19 November 2012 - 09:43 AM Rate Topic: -----

#1 prototype4  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 44
  • Joined: 22-October 12

Class Function Do/While Loop Problem

Posted 19 November 2012 - 07:38 AM

ok, so in the past two days i have been working on a text based rpg in C++ and its has run pretty smoothly, unitll yesterday. I had a soldier class that included these functions: fire weapon, reload weapon, drop weapon, pick up weapon, pick up ammo, throw grenade, pick up grenade. I had set a max amo limit at 200 and a max grenade count at 4. I also made it to where it shouldn't have had a negetive number in grenade count or ammo count. Yet, when i compiled it and used fire weapon, use grenade, and reload, these limits were ignored and i had over 200 ammo and -3 grenades. I have no idea what i have done wrong so the code i have is below. If you guys could tell me what you fixed and where, along with some "upgrades" i could use to make it run more smoothly, i would greatly appriciate it. but all i am really asking for is to know why it isn't working so if you could tell me why i would be estactic. Thank you for taking your time to read and remedy my problems!

*Note* I did try to comment some of the lines but i got so frustrated with it i kind of forgot to do the rest. i hope the code explains itself for you guys. if you have any questions feel free to ask!

/*  This project is the Soldier_Class structure
	The code in the project may change when reconstruction of final project begins
	More than likely, most of the code in the project will drastically change
*/

#include<iostream>
#include<vector>
#include<string>

using namespace std;
class Soldier	//creates a soldier class
{
public:	//tells what ints and void functions are public
	int m_soldierSpeed;	//creates a member data named soldierSpeed
	int m_soldierAgility;	//creates a member data named solderAgility
	int m_soldierStrenght;	//creates a member data named solderStrenght
	int m_soldierAccuracy;	//creates a member data named solderAccuracy
	int m_Ammocount;	//creates a member data named Ammocount
	int m_Grenadecount;		//creates a member data named Grenadecount

	void fire_weapon();
	void use_grenade();
	void reload_weapon();
	void drop_weapon();
	void pickup_weapon();
	void pickup_ammo();
	void pickup_grenade();
	void action();
};
void Soldier::fire_weapon()
{
	int weapon_accuracy = m_soldierSpeed + m_soldierAccuracy;
	m_Ammocount = m_Ammocount - 15;
	
		cout<<"You fired your weapon!\n";
		cout<<"Your accuracy was "<<weapon_accuracy<<".\n";
		cout<<"You ammo is now at "<<m_Ammocount<<" bullets left.\n";
	
}
void Soldier::use_grenade()
{
		m_Grenadecount = m_Grenadecount - 1;
		cout<<"You used a grenade!\n";
		cout<<"You have "<<m_Grenadecount<<" grenades left!\n";
}
void Soldier::reload_weapon()
{
		int reload_Speed = m_soldierAgility + m_soldierSpeed - 5;
		m_Ammocount = m_Ammocount + 60;
		cout<<"You reloaded your weapon!\n";
		cout<<"Your amo is now at "<<m_Ammocount<<" bullets.\n";
}
void Soldier::drop_weapon()
{
		cout<<"Your weapon is out of amo.\n";
		cout<<"You dropped your weapon.\n";
}
void Soldier::pickup_weapon()
{
		cout<<"You have come across a weapon,\n";
		cout<<" do you wish to pick it up?\n";
		m_Ammocount = m_Ammocount+200;
}
void Soldier::pickup_grenade()
{
		cout<<"Your grenade count is"<<m_Grenadecount<<".\n";
		cout<<"You can pick up a grenade.\n";
		m_Grenadecount=m_Grenadecount +1;
}
void Soldier::pickup_ammo()
{
		cout<<"You picked up ammo!\n";
		m_Ammocount = m_Ammocount + 30;
}
void Soldier::action()
{
	//I haved no idea how to mess with this one....
}
int main()
{
	Soldier char1;

	char1.m_Ammocount = 200;
	char1.m_Grenadecount = 4;
	char1.m_soldierAccuracy = 10;
	char1.m_soldierAgility = 10;
	char1.m_soldierSpeed = 10;
	char1.m_soldierStrenght = 10;

	int choice;

	do
	{
		cout<<"ExoSoldier v1.0\n\n";
		cout<<"0 - Quit\n";
		cout<<"1 - Fire Weapon\n";
		cout<<"2 - Reload Weapon\n";
		cout<<"3 - Drop Weapon\n";
		cout<<"4 - Pick Up Weapon\n";
		cout<<"5 - Use Grenade\n";
		cout<<"6 - Pick Up Ammo\n";
		cout<<"7 - Pick Up Grenade\n";
		cout<<"8 - Interact With Surroundings\n\n"; //This choice for the time being does not work

		cout<<"Choice: ";
		cin>> choice;
		switch (choice)
		{
		case 0:
			cout<<"Seeya Soldier!\n";
			break;
		case 1:
			if ( char1.m_Ammocount !=0)
			{
				char1.fire_weapon();
			}
			else
			{
				cout<<"You're out of ammo son!\n";
			}
			break;
		case 2:
			if ( char1.m_Ammocount !=0)
			{
				char1.reload_weapon();
			}
			else
			{
				cout<<"Your weapon is full!\n";
			}
			break;
		case 3:
			if ( char1.m_Ammocount = 0)
			{
				char1.drop_weapon();
			}
			else
			{
				cout<<"Your weapon still has ammo!\n";
			}
			break;
		case 4:
			if (char1.m_Ammocount = 0)
			{
				char1.pickup_weapon();
			}
			else
			{
				cout<<"You can't hold another weapon son!\n";
			}
			break;
		case 5:
			if (char1.m_Grenadecount !=0)
			{
				char1.use_grenade();
			}
			else
			{
				cout<<"You're out of grenades!\n";
			}
			break;
		case 6:
			if ( char1.m_Ammocount !=200)
			{
			char1.pickup_ammo();
			}
			else
			{
				cout<<"Your ammo is full!\n";
			}
			break;
		case 7:
			if (char1.m_Grenadecount !=4)
			{
				char1.pickup_grenade();
			}
			else
			{
				cout<<"You can't hold any mroe grenades!\n";
			}
			break;
		case 8:
			char1.action();
			break;
		default:
			cout<<"Soldier, are you confused?\n";
			cout<<"What you entered is not valid!\n";
		}
	}while (choice != 0);

	return 0;
}




Is This A Good Question/Topic? 0
  • +

Replies To: Class Function Do/While Loop Problem

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5960
  • View blog
  • Posts: 23,238
  • Joined: 23-August 08

Re: Class Function Do/While Loop Problem

Posted 19 November 2012 - 07:46 AM

if (char1.m_Ammocount = 0)



= is for assignment, == is for comparison.

And you really should have a method that does the ammo check.
Was This Post Helpful? 0
  • +
  • -

#3 prototype4  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 44
  • Joined: 22-October 12

Re: Class Function Do/While Loop Problem

Posted 19 November 2012 - 09:43 AM

ok, and how would i do the ammo assignment sir?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1