• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 601
  • Last Modified:

I have a C++ plasma Pistol for a class I am getting errors

I have a code that I am doing for my class and I don't know were I am messing up at.  I have the following code.  Just so you know I am very new to programming so please take it easy on me.  Just kidding, I am new to programming and could use help to see were I went wrong.

The idea of the code is object oriented programming and I have to create a plasma pistol that will show the destructive power of the gun, the number of bolts in the gun, the number of bolts that the gun is shooting.  Then its suppose to say how many shots the gun has shot then finally subtract how many shots are left after firing it a couple of times.  
PlasmaPistol.txt
0
Moneybiz
Asked:
Moneybiz
  • 3
  • 2
1 Solution
 
jkrCommented:
You had a few typos and such, try the code below (and see the comments I added) - compiles fine here.
#include <iostream>
using namespace std;
class PlasmaPistol
{
 
private: 
    int ammo;  //an integer value indicating the number of plasma bolts left in the plasma pistol 
    int rateOfFire; //an integer value indicating the number of bolts fired with one trigger pull. The max value is 10 the minimum value is 1. Ensure that the minimum and max values are not exceeded. 
    int destructivePower; //an integer from 1 to 10 indicating the destructive effect of the weapon. Ensure that the minimum and max values are not exceeded. 
 
 
public:  
  bool safetyOn;  //a boolean value indicating if the pistols safety feature is activated 
  int maxAmmo;  //an integer value indicating the maximum number of ammo bolts the plasma pistol can hold 
  
  PlasmaPistol();
  PlasmaPistol(int, int, int);
 
    void pressTrigger(void);  //fires the plasma pistol and decreases the ammo count based on the rateOfFire property. The pistol should not fire if the safetyOn property is true. 
    void load(int nmbrOfBolts);  //adds ammo bolts to the plasma pistol.  The amount of ammo bolts in the plasma pistol can not exceed the maxAmmo property  
    void setDestructivePower(int powerSetting);  //changes the destructive power of the plasma pistol  
    int showDestructivePower(void);  //returns the value of the destructivePower property.  
    int setRateOfFire(int boltsPerTriggerPress);  //changes the number of plasma bolts fired by the plasma pistol. Ensures that the max and minimum values are not exceeded.  
    int showRateOfFire(void);  //returns the value of the rateOfFire property  
    int ammoRemaining(void);  //returns the amount of ammo remaining in the pistol  
 
};
 
 
int main ()
{
	PlasmaPistol gun1; //uses the default contructor
        int ammo = 42;
        int rateOfFire = 42;
        int destructivePower =42;
	PlasmaPistol gun2 (ammo, rateOfFire, destructivePower); // one ; too much - and the variables are unknown
    cout << "Gun 1 Destructive Power " << gun1.showDestructivePower() << endl;//Display the value of destructive power, 
    cout << "Gun 1 Number Of Bolts Left " << gun1.ammoRemaining() << endl; //Display the value of rate of fire  
    cout << "Gun 1 Rate of Fire " << gun1.showRateOfFire() << endl;  //Display the value of ammo properties.
    cout << " " << endl;
	cout << "Gun 2 Destructive Power " << gun2.showDestructivePower() << endl;//Display the value of destructive power, 
    cout << "Gun 2 Number Of Bolts Left " << gun2.ammoRemaining() << endl; //Display the value of rate of fire  
    cout << "Gun 2 Rate of Fire " << gun2.showRateOfFire() << endl;//Display the value of ammo properties.
	cout << " " << endl;
    //Set the following properties of one pistol:  rate of fire, max ammo, destructive power and ammo. 
 
    //Fire the plasma pistol several times and display the amount of ammo remaining.  
    //reload the plasma pistol
 
return 0;
}
 
	PlasmaPistol::PlasmaPistol()
{
		ammo = 1;
		rateOfFire = 1;
		destructivePower = 1;
}
 
	PlasmaPistol::PlasmaPistol(int nmbrOfBolts, int boltsPerTriggerPress, int powerSettings) //; < -- no ; here
{
	ammo = nmbrOfBolts; // typo, 'e' not expected
	rateOfFire = boltsPerTriggerPress;
	destructivePower = powerSettings;
}
 
    void PlasmaPistol::pressTrigger(void)
    {
       int pressTrigger = 5;
    }  
    //fires the plasma pistol and decreases the ammo count based on the rateOfFire property. The pistol should not fire if the safetyOn property is true. 
    void PlasmaPistol::load(int nmbrOfBolts)
    {
        ammo = nmbrOfBolts;
        if (ammo < 1)
            ammo = 1;
        else if (ammo > 500)
            ammo = 500;
    } 
    //adds ammo bolts to the plasma pistol.  The amount of ammo bolts in the plasma pistol can not exceed the maxAmmo property  
    void PlasmaPistol::setDestructivePower(int powerSetting)
    {
        destructivePower = 5;
        
 
    }  
    //changes the destructive power of the plasma pistol  
    int PlasmaPistol::showDestructivePower(void)
    {
		destructivePower = 5;
        return destructivePower;
    }
 
    //returns the value of the destructivePower property.  
    int PlasmaPistol::setRateOfFire(int boltsPerTriggerPress){
    rateOfFire = boltsPerTriggerPress;
	boltsPerTriggerPress = 5; // typo, capital T needed here
 
	return rateOfFire;
    }  
    //changes the number of plasma bolts fired by the plasma pistol. Ensures that the max and minimum values are not exceeded.  
    int PlasmaPistol::showRateOfFire(void) 
    {
	if (rateOfFire < 1)
        rateOfFire = 1;
    else if (rateOfFire > 5)
        rateOfFire = 5;
        return rateOfFire;
       
    } 
    //returns the value of the rateOfFire property  
    int PlasmaPistol::ammoRemaining(void)
    {
        int ammoRemaining = 500;
		return ammoRemaining;
        
    } //returns the amount of ammo remaining in the pistol 

Open in new window

0
 
MoneybizAuthor Commented:
Thank you. For your help it compiled for me also.  I just have to figure out how to get it to tell me how many plasma bolts are left after I shoot the pistol.  

That is my next goal.
0
 
jkrCommented:
Well, in 'pressTrigger()' you should be abe to tell from your member variables there...
0
 
jkrCommented:
Actually, to be more precise, you can examine that like

cout << "Ammo left: " << ammo << endl;

See your own declaration:

private:
    int ammo;  //an integer value indicating the number of plasma bolts left in the plasma pistol
0
 
MoneybizAuthor Commented:
very good i will try that and see how it goes it just takes me a while of playing around with the code to get it to work.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now