1

I need to be able to update these variables.

  • Base Damage
  • Accuracy
  • Rate of fire
  • Range

I need to be able to add to the base damage or accuracy or the rate of fire or the range, once I have already added I need to be able to continuously update the variable by adding more points to the variable if I so desire to do so.

The points total needs to be subtracted but when the variable is first subtracted if you continue to buy within the menu the variable does not change at all.

Once you have the variables updated you should be able to then look at the total of the points value and the weapon stats and see a changes after repetitive buying.

The variables after 1 purchase continue to remain stagnant.

I am sorry for this large amount of text I am just not sure what to take away from these segments of code.

The main question is how to modify the value of a variable after it had been added or subtracted to.

#include <iostream>
#include <string>
using namespace std;
int i; 
int x;
int g;
class shotgun
{
private:
int a = 40, b = 15, c = 10, d = 6, j = 10, p = 10, v = 5, n = 2, o = 5, k = 5;
int pointstotal = p -= v;
int baseDamage = a += j;
int accuracy = b += k;
int range = c += o;
int rateOffire = d += k;
public:
    shotgun(){
        do {
            cout << "You have 10 points at your disposal" << endl;
            cout << "Your shotgun stats are.." << endl;
            cout << "Base Damage: " << a  << endl;
            cout << "Accuracy:" << b << endl;
            cout << "Range:" << c << endl;
            cout << "Rate of Fire:" << d << "s" << endl;
            cout << "1)Would you like to choose something that 
                                       upgrades your damage output?" << endl;
            cout << "2)Would you like something for accuracy?" << endl;
            cout << "3)Would you like something for range?" << endl;
            cout << "4)Would you like some thing for your rate of fire?" << endl;
            cout << "5)Would you like to see your new weapon's stats and your points." << endl;
            cout << "6)Would you like to exit the program?" << endl;
            cin >> i;
            switch (i) {
            case 1:
                cout << "Here are some slugs for your damage output.." << endl;
                 a + j;
                 p - v;
                break;
            case 2:
                cout << "Here is a longer stock for your shotgun." << endl;
                 b + k;
                 p - v;
                break;
            case 3:
                cout << "Here is a longer barrel for your shotgun." << endl;
                 c + o;
                 p - v;
                break;
            case 4:
                cout << "Here is better break action barrel for your shotgun." << endl;
                 d + n;
                 p - v;
                break;
            case 5:
                cout << "You now have" <<  pointstotal << "points at your disposal." << endl;
                cout << "Your shotgun stats are now.." << endl;
                cout << "Base Damage:" << baseDamage  << endl;
                cout << "Accuracy:" << accuracy  << endl;
                cout << "Range:" << range << endl;
                cout << "Rate of Fire:" << rateOffire << "s" << endl;
                break;
            case 6:
                cout << "Exiting Program" << endl;
                g = 5;
            }
        } while (g != 5);
    }
};
int main()
{
    cout << "Welcome to the weapon customization system!" << endl;
    cout << "Choose your Weapon to customize you have ten points" << endl;
    cout << "*************************************************" << endl;
    cout << "1)Choose the double barreled shotgun?" << endl;
    cout << "2)Choose the assault rifle?" << endl;
    cout << "3)Choose the 44. Magnum?" << endl;
    cout << "4)Choose the combat shotgun?" << endl;
    cin >> x;
    switch (x)
    {
    case 1:
        shotgun e;
         break;
    }

    cout << "\n\n";
    system("Pause");
}

The variables will stay the same even after you have used the previous options to modify the stats within the switch case if you press 5 as an option after upgrading the weapon.

Gene Z. Ragan
  • 2,643
  • 2
  • 31
  • 41
  • 1
    I am surprised you did not get a compile error on the variable created inside the switch case. I mean this: https://stackoverflow.com/questions/92396/why-cant-variables-be-declared-in-a-switch-statement – drescherjm Sep 10 '19 at 00:48
  • 4
    @drescherjm because there no tags to jump to other than `case 1`, so nowhere you can jump to skip initialization of `e` – ph3rin Sep 10 '19 at 00:51
  • 1
    As a note, you may want a more general `Weapon` class, so you don't need to duplicate most or all of the code implementing the other guns. Depending on implementation specifics (primarly, whether there are any weapon-specific behaviours, whether the double-barreled and combat shotguns have any common code/values not shared with the other two, and how much you want e.g., individual shotguns to be customisable), it might or might not make sense to implement some or all of the weapons with subclasses as well. – Justin Time - Reinstate Monica Sep 10 '19 at 00:53
  • 2
    `int a = 40, b = 15, c = 10, d = 6, j = 10, p = 10, v = 5, n = 2, o = 5, k = 5;` will do you no favours when debugging. Use descriptive names. They make it easier to read the code and infer meaning from it, make it harder to accidentally use `b` where you meant to use `d`, and make it a lot easier to spot the mistake when you use `b` instead of `d`. – user4581301 Sep 10 '19 at 00:55
  • 2
    You should be using descriptive variable names, not one-letter names. The code is not only harder to debug and follow, you can potentially get into name clash issues by using those one-letter "gems". Things like "why isn't k equal to ?" and you happened to use two different `k` variables by mistake. – PaulMcKenzie Sep 10 '19 at 00:55
  • I am surprised too this is some odd jury rigged program. –  Sep 10 '19 at 01:04
  • I will use more descriptive names when writing variables I keep making that mistake. –  Sep 10 '19 at 01:07
  • You guys are awesome! –  Sep 10 '19 at 01:25
  • Thank you for the edit! –  Sep 10 '19 at 22:22

2 Answers2

5

Your problem is here:

switch (x)
{
case 1:
    shotgun e;
     break;
}

... you're declaring an object of type shotgun inside the case statement, which means that a new shotgun object is created whenever case 1 is entered, and destroyed when that scope is exited (i.e. when the break command is executed).

If you want the state of your shotgun object to persist, you'll need to declare it at an earlier/bigger scope (e.g. at the beginning of main() would be a good spot for it)

Also, the attempted-variable-modifications inside your do...while() loop are wrong; instead of:

        case 1:
            cout << "Here are some slugs for your damage output.." << endl;
             a + j;
             p - v;
            break;

... you probably meant to do:

        case 1:
            cout << "Here are some slugs for your damage output.." << endl;
             a += j;
             p -= v;
            break;

.... note the += means "increase by" and the -= means "decrease by" (whereas a+j and p-v merely compute a value that gets immediately discarded, so they have no effect)

As an aside, you should probably move the do...while() loop out of the shotgun constructor and into a separate method that you can call later on; otherwise it will be executed every time a shotgun object is created, which is probably not what you want.

Jeremy Friesner
  • 70,199
  • 15
  • 131
  • 234
  • Does that mean I should put the main menu within the shotgun constructor. Then place the shotgun customizable menu inside a void then wrap the do while loop around the void function? –  Sep 10 '19 at 00:59
  • Also thank you very much for the response! I can see what you mean there! –  Sep 10 '19 at 00:59
  • Thank you very much you are awesome you helped me make definite progress!! –  Sep 10 '19 at 01:26
  • 1
    I wouldn't put anything inside the shotgun constructor except whatever code you need (if any) to set the initial state of its member-variables. All of the menu stuff you could put inside a `void doMenuStuff()` (or similar) public-method instead, and then `main()` can call `e.doMenuStuff()` at the appropriate times. – Jeremy Friesner Sep 10 '19 at 02:31
  • It is just for this particular case I need to use constructors. –  Sep 10 '19 at 22:20
0

HERE IS THE FINAL RESULT. I completed this code with the advice given thank you for the advice. For those who need extra help. :)

#include <iostream>
#include <string>
using namespace std;
int i; 
int x;
int g;
int j = 5;

class assaultrifle
{
private:
    int baseassaultPoints = 10, baseassaultDamage = 35, baseassaultAccuracy = 56, baseassaultRange = 72, baseassaultROF = 78;
    int assaultPoints = 10, assaultDamage = 35, assaultAccuracy = 56, assaultRange = 72, assaultROF = 78;
public:
    int v = 5, n = 2, o = 5, k = 5, j = 5;
    assaultrifle()
    {
        do {
            cout << "These are your base stats for your gun.." << endl;
            cout << "Your weapon has a base damage of:" << baseassaultDamage << endl;
            cout << "Your weapon has an accuracy of: " << baseassaultAccuracy << endl;
            cout << "Your weapon has a range of:" << baseassaultRange << endl;
            cout << "Your weapon has a rate of fire of:" << baseassaultROF << endl;
            cout << "**************************************" << endl;
            cout << "You have " << assaultPoints << "assaultpoints.." << endl;
            cout << "1)Would you like to choose something that upgrades your damage output?" << endl;
            cout << "2)Would you like something for accuracy?" << endl;
            cout << "3)Would you like something for range?" << endl;
            cout << "4)Would you like some thing for your rate of fire?" << endl;
            cout << "5)Would you like to exit the program?" << endl;
            cin >> i;
            system("Pause");
            system("cls");
            if (assaultPoints == 0) {
                cout << "You have" << assaultPoints << "points at your disposal" << endl;
                cout << "Your upgraded M16 now has better stats." << endl;
                cout << "Your final assault rifle stats are.." << endl;
                cout << "Base Damage: " << assaultDamage << endl;
                cout << "Accuracy:" << assaultAccuracy << endl;
                cout << "Range:" << assaultRange << "meters" << endl;
                cout << "Rate of Fire:" << assaultRange << "s" << endl;
                cout << "**************************************" << endl;
            }
            switch (i) {
            case 1:
                cout << "Here are some armor piercing rounds for your damage output.." << endl;

                assaultDamage += j;
                assaultPoints -= v;

                break;
            case 2:
                cout << "Here is a foregrip for your assault rifle." << endl;
                assaultAccuracy += k;
                assaultPoints -= v;

                break;
            case 3:
                cout << "Here is a heavy barrel for your assault rifle." << endl;
                assaultRange += o;
                assaultPoints -= v;

                break;
            case 4:
                cout << "Here is taped magazine  for your assault rifle." << endl;
                assaultROF += n;
                assaultPoints -= v;

                break;

            case 5:
                cout << "Exiting Program" << endl;
                g = 5;
            }
        } while (g != 5);
    }
};
class shotgun {
private:
    int baseShotgunDamage = 50, baseShotgunAccuracy = 15, baseShotgunRange = 10, baseShotgunROF = 6, baseShotgunPoints = 10;
    int shotgunDam = 50, shotgunAccuracy = 15, shotgunRange = 10, shotgunROF = 6, j = 10, shotgunPoints = 10;
    int pointstotal = shotgunPoints - v;
    int baseDamage = shotgunDam + j;
    int accuracy = shotgunAccuracy + k;
    int range = shotgunRange + o;
    int rateOffire = shotgunROF + k;
public:
    int v = 5, n = 2, o = 5, k = 5;
    shotgun()
    {
        do {
            cout << "These are your base stats for your gun.." << endl;
            cout << "Your weapon has a base damage of:" << baseShotgunDamage << endl;
            cout << "Your weapon has an accuracy of: " << baseShotgunAccuracy << endl;
            cout << "Your weapon has a range of:" << baseShotgunRange << endl;
            cout << "Your weapon has a rate of fire of:" << baseShotgunROF << endl;
            cout << "**************************************" << endl;
            cout << "You have " << shotgunPoints << "sawed off shotgun points.." << endl;
            cout << "1)Would you like to choose something that upgrades your damage output?" << endl;
            cout << "2)Would you like something for accuracy?" << endl;
            cout << "3)Would you like something for range?" << endl;
            cout << "4)Would you like some thing for your rate of fire?" << endl;
            cout << "5)Would you like to exit the program?" << endl;
            cout << "**************************************" << endl;
            cin >> i;
            system("Pause");
            system("cls");
            if (shotgunPoints == 0) {
                cout << "You have" << shotgunPoints << "points at your disposal" << endl;
                cout << "Your final double barreled shotgun stats are.." << endl;
                cout << "Base Damage: " << shotgunDam << endl;
                cout << "Accuracy:" << shotgunAccuracy << endl;
                cout << "Range:" << shotgunRange << "meters" << endl;
                cout << "Rate of Fire:" << shotgunROF << "s" << endl;
                cout << "**************************************" << endl;
            }
            switch (i) {
            case 1:
                cout << "Here are some slugs for your damage output.." << endl;
                shotgunDam += j;
                shotgunPoints -= v;
                break;
            case 2:
                cout << "Here is a longer stock for your shotgun." << endl;
                shotgunAccuracy += k;
                shotgunPoints -= v;
                break;
            case 3:
                cout << "Here is a longer barrel for your shotgun." << endl;
                shotgunRange += o;
                shotgunPoints -= v;
                break;
            case 4:
                cout << "Here is better break action barrel for your shotgun." << endl;
                shotgunROF += n;
                shotgunPoints -= v;
                break;

            case 5:
                cout << "Exiting Program" << endl;
                g = 5;
            }
        } while (g != 5);

    }
};
class handgun
{
private:
    int basehandgunDam = 15, basehandgunAccuracy = 55, basehandgunRange = 25, basehandgunROF = 19, j = 10, basehandgunPoints = 10;
    int handgunDam = 15, handgunAccuracy = 55, handgunRange = 25, handgunROF = 19, handgunPoints = 10;
    int pointstotal = handgunPoints - v;
    int baseDamage = handgunDam + j;
    int accuracy = handgunAccuracy + k;
    int range = handgunRange + o;
    int rateOffire = handgunROF + k;
public:
    int v = 5, n = 2, o = 5, k = 5; 
        handgun()
        {
            do {
                cout << "These are your base stats for your gun.." << endl;
                cout << "Your weapon has a base damage of:" << basehandgunDam << endl;
                cout << "Your weapon has an accuracy of: " << basehandgunAccuracy << endl;
                cout << "Your weapon has a range of:" << basehandgunRange << endl;
                cout << "Your weapon has a rate of fire of:" << basehandgunROF << "s" << endl;
                cout << "**************************************" << endl;
                cout << "You have " << handgunPoints << "glock points.." << endl;
                cout << "1)Would you like to choose something that upgrades your damage output?" << endl;
                cout << "2)Would you like something for accuracy?" << endl;
                cout << "3)Would you like something for range?" << endl;
                cout << "4)Would you like some thing for your rate of fire?" << endl;
                cout << "5)Would you like to exit the program?" << endl;
                cout << "**************************************" << endl;
                cin >> i;
                system("Pause");
                system("cls");
                if (handgunPoints == 0) {
                    cout << "You have" << handgunPoints << "points at your disposal" << endl;
                    cout << "Your final Glock 17 stats are.." << endl;
                    cout << "Base Damage: " << handgunDam << endl;
                    cout << "Accuracy:" << handgunAccuracy << endl;
                    cout << "Range:" << handgunRange << "meter" << endl;
                    cout << "Rate of Fire:" << handgunROF << "s" << endl;
                    cout << "**************************************" << endl;
                }
                switch (i) {
                case 1:
                    cout << "Here are some hollow points for your damage output.." << endl;
                    handgunDam += j;
                    handgunPoints -= v;
                    break;
                case 2:
                    cout << "Here is a foldable stock for your pistol." << endl;
                    handgunAccuracy += k;
                    handgunPoints -= v;
                    break;
                case 3:
                    cout << "Here is a longer muzzle for your glock 17." << endl;
                    handgunRange += o;
                    handgunRange -= v;
                    break;
                case 4:
                    cout << "Here is a full auto function for you glock 17." << endl;
                    handgunROF += n;
                    handgunROF -= v;
                    break;

                case 5:
                    cout << "Exiting Program" << endl;
                    g = 5;
                }
            } while (g != 5);
        }
};
class combatshotgun
{

private:
    int basecombatDam = 40, basecombatAccuracy = 55, basecombatRange = 25, basecombatRecoil = 20, j = 10, basecombatPoints = 10;
    int combatDam = 40, combatAccuracy = 55, combatRange = 25, combatRecoil = 20, combatPoints = 10;
    int pointstotal = combatPoints - v;
    int baseDamage = combatDam + j;
    int accuracy = combatAccuracy + k;
    int range = combatRange + o;
    int recoil = combatRecoil + k;
public:
    int v = 5, n = 2, o = 5, k = 5;

    combatshotgun()
    {

        do {
            cout << "These are your base stats for your gun.." << endl;
            cout << "Your weapon has a base damage of:" << basecombatDam << endl;
            cout << "Your weapon has an accuracy of: " << basecombatAccuracy << endl;
            cout << "Your weapon has a range of:" << basecombatRange << endl;
            cout << "Your weapon has a rate of fire of:" << basecombatRecoil << endl;
            cout << "**************************************" << endl;
            cout << "You have " << combatPoints << "combat shotgun points.." << endl;
            cout << "1)Would you like to choose something that upgrades your damage output?" << endl;
            cout << "2)Would you like something for accuracy?" << endl;
            cout << "3)Would you like something for range?" << endl;
            cout << "4)Would you like something for the recoil?" << endl;
            cout << "5)Would you like to exit the program?" << endl;
            cout << "**************************************" << endl;
            cin >> i;
            system("Pause");
            system("cls");
            if (combatPoints == 0) {
                cout << "You have" << combatPoints << "points at your disposal" << endl;
                cout << "Your final shotgun stats are.." << endl;
                cout << "Base Damage: " << combatDam << endl;
                cout << "Accuracy:" << combatAccuracy << endl;
                cout << "Range:" << combatRange << "meters" << endl;
                cout << "Recoil:" << combatRecoil << endl;
                cout << "**************************************" << endl;
            }
            switch (i) {
            case 1:
                cout << "Here are some dragons breath for your Remington 12 gauge.." << endl;
                combatDam += j;
                combatPoints -= v;
                break;
            case 2:
                cout << "Here is a longer stock for your Remingonton 12 guage." << endl;
                combatAccuracy += k;
                combatPoints-= v;
                break;
            case 3:
                cout << "Here is a longer barrel for your Remington." << endl;
                combatRange += o;
                combatPoints -= v;
                break;
            case 4:
                cout << "Here is a foregrip for your remington 12 gauge." << endl;
                combatRecoil -= n;
                combatPoints -= v;
                break;

            case 5:
                cout << "Exiting Program" << endl;
                g = 5;
            }
        } while (g != 5);
    }





};
int main()
{

    cout << "Welcome to the weapon customization system!" << endl;
    cout << "Choose your Weapon to customize you have ten points" << endl;
    cout << "*************************************************" << endl;
    cout << "1)Choose the double barreled shotgun?" << endl;
    cout << "2)Choose the M16?" << endl;
    cout << "3)Choose the Glock 17?" << endl;
    cout << "4)Choose the Remington 12 guage?" << endl;
    cout << "**************************************" << endl;
    cin >> x;

    switch (x)
    {
    case 1:

        shotgun();
        break;
    case 2:
        assaultrifle();
        break;
    case 3:
        handgun();
        break;
    case 4:
        combatshotgun();

        break;
    }

    cout << "\n\n";
    system("Pause");


}