Domanda

This has just turned into a flame war. I will ask the question again later if I dont figure it out.

The values are changed inside the function, but once the function ends the values go back to what they were set to originally.

class inventory
 {
private:
struct instock {

    double everything[4]; //Gets set to 0-3. Then user changes values.

};

public:
instock stock;
void changegas(double userinput);
void stocking(); //populates the array
void output();  //outputs the array
};

.

void inventory::stocking(){
for(int i=0; i<4; i++){
    stock.everything[i]=i;
 }
}

void inventory::changegas(double input){
double a;
a = input;
stock.everything[0] += a;
std::cout << "Gas remaining: " << stock.everything[0];
}

void inventory::output(){
std::cout << std::endl;
    for(int i=0; i<4; i++){
 std::cout << stock.everything[i];
 }
}

void inventory::changegas(double input){
double a;
a = input;
stock.everything[0] += a;
std::cout << "Gas remaining: " << stock.everything[0];
}

How can I make it so this function changes the value of the array stored in everything permanently?

   int main()
{


backroom.stocking();

int choice;

cout << "What would you like to change? \n1.)Gas\n2.)Tires\n3.)Soda\n4.)Snacks"<<endl;
cin >> choice;
menu(choice, backroom);
backroom.output();




return 0;
}

void menu(int zed, inventory a){
 int z = zed;
 int c = 0;
switch (z){//start switch

case 1:
    cout << "Enter the amount of Gas you want to change: ";
    cin >> c;
    a.changegas(c);
    break;
case 2:
    cout << "Enter the amount of Tires you want to change: ";
    cin >> c;
    a.changetires(c);
    break;
case 3:
    cout << "Enter the amount of Soda you want to change: ";
    cin >> c;
    a.changesoda(c);
    break;
case 4:
    cout << "Enter the amount of Snacks you want to change: ";
    cin >> c;
    a.changesnacks(c);
    break;

    break;
}//endswitch

}// end funct

I know arrays are passed by reference so I thought this would work as is. But clearly I am not understanding something correctly. Still currently learning the language. Maybe I am not passing the array properly?

È stato utile?

Soluzione

You're passing backroom by value. You need to alter menu() to take in either an inventory reference or an inventory pointer, preferably the former. This is because when you pass a class object by value, the entire object is copied to a new instance local to the function being called and any changes made to that instance will not effect the original copy.

This should work:

void menu(int zed, inventory& a) {
    int z = zed; //you don't need this by the way
    int c = 0;

    switch (z){//start switch
    case 1:
        cout << "Enter the amount of Gas you want to change: ";
        cin >> c;
        a.changegas(c);
        break;
    case 2:
        cout << "Enter the amount of Tires you want to change: ";
        cin >> c;
        a.changetires(c);
        break;
    case 3:
        cout << "Enter the amount of Soda you want to change: ";
        cin >> c;
        a.changesoda(c);
        break;
    case 4:
        cout << "Enter the amount of Snacks you want to change: ";
        cin >> c;
        a.changesnacks(c);
        break;
    }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top