Plasma pistol output coming out wrong.

I'm having diffuculty figuring out whats wrong.Its showing no errors but the output is wrong for the second pistol.Heres what i got so far.If anyone can help me i would appreciate it.

#include <iostream>
using namespace std;

class PlasmaPistol{
private:
//properties
int theAmmo;
int theRateOfFire;
int theDestructivePower;
public:
//properties
bool safetyOn;
int maxAmmo;

//member function prototypes

void pressTrigger(void);
void load(int);
void setDestructivePower(int powerSetting);
int showDestructivePower(void);
void setRateOfFire (int boltsPerTriggerPress);
int showRateOfFire(void);
int ammoRemaining(void);
//constructors
PlasmaPistol(); //a default constructor that assigns default values to both
PlasmaPistol(int, int); //the private and public properties.
//An overload constructor that allows the destructive
//power and maxAmmo properties to be set when the object is instatiated
};

int main(void){

PlasmaPistol plasmaPistol1;//uses the default constructor
PlasmaPistol plasmaPistol2(7, 50);//uses the overloaded constructor

cout << "Plasma Pistol 1's Destructive Power : " << plasmaPistol1.showDestructivePower() << endl;
cout << "Plasma Pistol 1's Number of bolts left : " << plasmaPistol1.ammoRemaining() << endl;
cout << "Plasma Pistol 1's Rate of fire : " << plasmaPistol1.showRateOfFire() << endl;
cout << "Shooting " << plasmaPistol1.showRateOfFire() << " bolts " << endl;
cout << "Shooting " << plasmaPistol1.showRateOfFire() << " bolts " << endl;
cout << endl;
cout << endl;
cout << "Number of bolts left: " << plasmaPistol1.ammoRemaining() << endl;
cout << endl;
cout << endl;
cout << "Plasma Pistol 2's Destructive Power : " << plasmaPistol2.showDestructivePower() << endl;
cout << "Plasma Pistol 2's Number of bolts left : " << plasmaPistol2.ammoRemaining() << endl;
cout << "Plasma Pistol 2's Rate of fire : " << plasmaPistol2.showRateOfFire() << endl;
cout << "Shooting " << plasmaPistol2.showRateOfFire() << " bolts " << endl;
cout << "Shooting " << plasmaPistol2.showRateOfFire() << " bolts " << endl;
cout << endl;
cout << endl;
cout << "Number of bolts left: " << plasmaPistol2.ammoRemaining() << endl;

return 0;
}

PlasmaPistol::PlasmaPistol(){
theAmmo = 50;
theRateOfFire = 5;
theDestructivePower = 5;
safetyOn = false;
maxAmmo = 100;
}

PlasmaPistol::PlasmaPistol(int powerSetting, int rate){
setDestructivePower(powerSetting);
setRateOfFire(rate);
}
//these are the plasmaPistols methods
void PlasmaPistol:: pressTrigger(void){ //fires the plasma pistol and decreases the ammo count based
if (safetyOn != true){
cout << "Plasma Pistol Fired " << theRateOfFire << " bolts" << endl;;
theAmmo = theAmmo - theRateOfFire;
}
else //on the rateOfFire property. The pistol should not fire if //theSafetyOn property is true.
cout << "Plasma Pistol does not have enough ammo to fire or the plasma pistols safety is on" << endl; // the safety is on
}

void PlasmaPistol::load(int nmbrOfBolts){
theAmmo = theAmmo + maxAmmo;
if(theAmmo < 0){
theAmmo = 50;
}
else if(theAmmo > maxAmmo){
theAmmo = maxAmmo;
}
} //adds ammo bolts to the plasma pistol. The amount of ammo bolts
//in the plasma pistol can not exceed theMaxAmmo property

void PlasmaPistol::setDestructivePower(int powerSetting){
theDestructivePower = powerSetting;
if (theDestructivePower < 1){
theDestructivePower = 1;
}
else if (theDestructivePower > 10)
theDestructivePower = 10;
}
//changes the destructive power of the plasma pistol

int PlasmaPistol::showDestructivePower(void){
return theDestructivePower;
} //returns the value of theDestructivePower property

void PlasmaPistol::setRateOfFire(int boltsPerTriggerPress){
theRateOfFire = boltsPerTriggerPress;
if (theRateOfFire < 1){
theRateOfFire = 1;
}
else if(theRateOfFire > 10)
theRateOfFire = 10;
}

int PlasmaPistol::showRateOfFire(void){
return theRateOfFire;
} //returns the value of theRateOfFire property

int PlasmaPistol::ammoRemaining(void){
return theAmmo;
} //returns the amount of ammo remaining in the pistol




Please put your code in code tags.

Also can you be more specific as to what the problem is? What kind of output are you expecting, and what output are you getting?
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include <iostream>
using namespace std;

class PlasmaPistol{
private:
//properties
int theAmmo;
int theRateOfFire;
int theDestructivePower;
public:
//properties
bool safetyOn;
int maxAmmo;

//member function prototypes

void pressTrigger(void);
void load(int);
void setDestructivePower(int powerSetting);
int showDestructivePower(void);
void setRateOfFire (int boltsPerTriggerPress);
int showRateOfFire(void);
int ammoRemaining(void); 
//constructors
PlasmaPistol();                                        //a default constructor that assigns default values to both
PlasmaPistol(int, int);                                            //the private and public properties.
                                            //An overload constructor that allows the destructive
                                               //power and maxAmmo properties to be set when the object is instatiated
};

int main(void){

PlasmaPistol plasmaPistol1;//uses the default constructor
PlasmaPistol plasmaPistol2(7, 50);//uses the overloaded constructor 

cout << "Plasma Pistol 1's Destructive Power : "   << plasmaPistol1.showDestructivePower() << endl;
cout << "Plasma Pistol 1's Number of bolts left : "  << plasmaPistol1.ammoRemaining() << endl;
cout << "Plasma Pistol 1's Rate of fire : "  << plasmaPistol1.showRateOfFire() << endl; 
cout << "Shooting " << plasmaPistol1.showRateOfFire() << " bolts "  << endl;
cout << "Shooting " << plasmaPistol1.showRateOfFire() << " bolts "  << endl;
cout << endl;
cout << endl;
cout << "Number of bolts left: " << plasmaPistol1.ammoRemaining()  << endl;
cout << endl;
cout << endl;
cout << "Plasma Pistol 2's Destructive Power : "   << plasmaPistol2.showDestructivePower() << endl;
cout << "Plasma Pistol 2's Number of bolts left : "  << plasmaPistol2.ammoRemaining() << endl;
cout << "Plasma Pistol 2's Rate of fire : "  << plasmaPistol2.showRateOfFire() << endl; 
cout << "Shooting " << plasmaPistol2.showRateOfFire() << " bolts "  << endl;
cout << "Shooting " << plasmaPistol2.showRateOfFire() << " bolts "  << endl;
cout << endl;
cout << endl;
cout << "Number of bolts left: " << plasmaPistol2.ammoRemaining()  << endl;

return 0;
}

PlasmaPistol::PlasmaPistol(){
     theAmmo = 50;
     theRateOfFire = 5;
     theDestructivePower = 5;
     safetyOn = false;
     maxAmmo = 100;
}

PlasmaPistol::PlasmaPistol(int powerSetting, int rate){
     setDestructivePower(powerSetting);
     setRateOfFire(rate);
}
//these are the plasmaPistols methods
void PlasmaPistol:: pressTrigger(void){                               //fires the plasma pistol and decreases the ammo count based                                       
    if (safetyOn != true){
            cout << "Plasma Pistol Fired " << theRateOfFire << " bolts" << endl;;
            theAmmo = theAmmo - theRateOfFire;
        }
        else                                                                    //on the rateOfFire property. The pistol should not fire if                                                                                //theSafetyOn property is true.
            cout << "Plasma Pistol does not have enough ammo to fire or the plasma pistols safety is on" << endl;         // the safety is on
        }
                                           
    void PlasmaPistol::load(int nmbrOfBolts){
        theAmmo = theAmmo + maxAmmo;
        if(theAmmo < 0){
            theAmmo = 50;
    }
        else if(theAmmo > maxAmmo){
            theAmmo = maxAmmo;
        }
    }                                                    //adds ammo bolts to the plasma pistol. The amount of ammo bolts
                                                       //in the plasma pistol can not exceed theMaxAmmo property

    void PlasmaPistol::setDestructivePower(int powerSetting){
        theDestructivePower = powerSetting;
        if (theDestructivePower < 1){
                theDestructivePower = 1;
        }
            else if (theDestructivePower > 10)
                theDestructivePower = 10;
            }
                                                    //changes the destructive power of the plasma pistol

    int PlasmaPistol::showDestructivePower(void){
        return theDestructivePower;
    }                                                    //returns the value of theDestructivePower property

    void PlasmaPistol::setRateOfFire(int boltsPerTriggerPress){ 
        theRateOfFire = boltsPerTriggerPress;
        if (theRateOfFire < 1){
            theRateOfFire = 1;
        }
        else if(theRateOfFire > 10)
                theRateOfFire = 10;
    }                                                

    int PlasmaPistol::showRateOfFire(void){ 
        return theRateOfFire;
    }                                                    //returns the value of theRateOfFire property

    int PlasmaPistol::ammoRemaining(void){
        return theAmmo;
    }                                                    //returns the amount of ammo remaining in the pistol


Here's the output i'm suppose to have or close to.

Plasma Pistol 1's Destructive Power : 5
Plasma Pistol 1's Number of bolts left : 30
Plasma Pistol 1's Rate of fire : 5

Shooting 5 bolts
Shooting 5 bolts

Number of bolts left: 20

Plasma Pistol 2's Destructive Power : 7
Plasma Pistol 2's Number of bolts left : 30
Plasma Pistol 2's Rate of fire : 10

Shooting 10 bolts
Shooting 10 bolts

Number of bolts left: 25

Here's the output that i'm recieving.

Plasma Pistol 1's Destructive Power : 5
Plasma Pistol 1's Number of bolts left : 30
Plasma Pistol 1's Rate of fire : 5

Shooting 5 bolts
Shooting 5 bolts

Number of bolts left: 20

Plasma Pistol 2's Destructive Power : 7
Plasma Pistol 2's Number of bolts left : -858993460
Plasma Pistol 2's Rate of fire : 10

Shooting 10 bolts
Shooting 10 bolts

Number of bolts left: -858993460

pistol B is using the overloaded constructor, But you have not set value for theAmmo in this constructor.

PlasmaPistol::PlasmaPistol(int powerSetting, int rate){
setDestructivePower(powerSetting);
setRateOfFire(rate);
//Please assign value for ammo**
}


So you are getting some garbage values.
Last edited on
Topic archived. No new replies allowed.