0

I tried to change a Price value of the object (to increace it by one), but i got a problem as it stay the same (no changes at all). It works fine inside of the method ,but It doesn't work when i display it in the main function. I would appreciate any help. method

void Tovar::increasingPriceValue(vector<Tovar> nameOfTheVector){
    int i;
    Tovar x;
    cout << "Enter number of element from array:";
    cin >> i;
    x.Price = nameOfTheVector[i].Price + 1;
    nameOfTheVector[i].SetPrice(x.Price);
}

And here is main

int main() {
    vector<Tovar>product;
    string menu = "Enter the option which you want to choose:\n1) Input inforamtion\n2) Displaying on the screen\n3) Exit\n";
    int option,incdec, DayOfReceipt_, MounthOfReceipt_, YearOfReceipt_, NumberOfRecievedProducts_, CurrentDay_, CurrentMounth_, CurrentYear_, NumberOfProductsThatLeft_;
    string Name_;
    float Price_;
    Tovar x,y,z;
    x =  Tovar();
    y=Tovar();
    while (true) {
        cout << menu;
        cin >> option;
        switch (option) {
        case 1: //Input 
            cout << "Enter the name of the product: ";
            cin >> Name_;
            x.SetName(Name_);
            cout << "Enter the price of the product: ";
            cin >> Price_;
            x.SetPrice(Price_);
            cout << "Enter the date when you received products:\n";
            cout << "Day: ";
            cin >> DayOfReceipt_;
            cout << "Mounth: ";
            cin >> MounthOfReceipt_;
            cout << "Year: ";
            cin >> YearOfReceipt_;
            x.SetDateOfReceipt(DayOfReceipt_,MounthOfReceipt_, YearOfReceipt_);
            cout<<"Enter the number of recieved Products: ";
            cin>>NumberOfRecievedProducts_;
            x.SetNumberOfReceivedProducts(NumberOfRecievedProducts_);
            cout << "Enter the current date:\n";
            cout << "Day: ";
            cin >> CurrentDay_;
            cout << "Mounth: ";
            cin >> CurrentMounth_;
            cout << "Year: ";
            cin >> CurrentYear_;
            x.SetCurrentDate(CurrentDay_,CurrentMounth_,CurrentYear_);
            cout << "Enter the number of products that left: ";
            cin >> NumberOfProductsThatLeft_;
            x.SetNumberOfProductsThatLeft(NumberOfProductsThatLeft_);
            z=Tovar(Name_,DayOfReceipt_,MounthOfReceipt_,YearOfReceipt_,NumberOfRecievedProducts_,CurrentDay_,CurrentMounth_,CurrentYear_,NumberOfProductsThatLeft_,Price_);
            product.push_back(z);
            product.push_back(x);
            y=Tovar(x);
            product.push_back(y);
            cout<<"Enter 1 to increase price:\nEnter 2 to decreace price: \nEnter 3 to not change price:\n";
            cin>>incdec;
            switch(incdec){
                case 1:{
                    x.increasingPriceValue(product);
                    break;
                }
            
            
        case 2: //display on the screen
            
            PrintTab(product);
Perun
  • 11
  • 4
  • `vector nameOfTheVector` is pass by value. You're operating on a copy. – user4581301 Oct 26 '20 at 23:56
  • _doesn't work_ is a rather vague description for a problem, can you be more precise please? – πάντα ῥεῖ Oct 26 '20 at 23:57
  • the problem is that it doesnt change the value. Thats what i mean – Perun Oct 26 '20 at 23:59
  • And that's because you're modifying a copy. The caller has no clue that anything happened to the copy inside `increasingPriceValue`. [Give this link a read for details.](https://stackoverflow.com/questions/373419/whats-the-difference-between-passing-by-reference-vs-passing-by-value) – user4581301 Oct 27 '20 at 00:02
  • I'm being negligent. You need to pass the `vector` by reference if you want to change the `vector` inside the function: `void Tovar::increasingPriceValue(vector & nameOfTheVector)` – user4581301 Oct 27 '20 at 00:09
  • I get it. Thank you so much!! – Perun Oct 27 '20 at 00:14

0 Answers0