Setter not storing updated value

I tested a cout in the class function itemsOrdered and it prints the updated value, but the setter does not store the updated itemQuantity value in the constructor. Why is this happening?


function in class Inventory and constructor
1
2
3
4
5
6
     private:
    int itemQuantity;
    string dateCounted;
    public:
    //default constructor
  
Last edited on
nikki1337 wrote:
the setter does not store the updated itemQuantity value in the constructor.

Not sure what you mean by "in the constructor".

If you're looking to add the new items to the item quantity variable then you can simplify your function greatly.

1
2
3
4
void itemsOrdered( int newItemsBought )
{
   itemQuantity += newItemsBought;
}


Using the getItemQuantity and setItemQuantity (which I'm assuming is static) seems a little unnecessary given that you have access to the itemQuantity variable directly within the class.
Assuming updatedInventory has type class Inventory, class Inventory has what members?
1
2
3
4
5
Inventory::Inventory()
void Inventory::setItemQuantity(int);
int Inventory::getItemQuantity() const;
void Inventory::itemsOrdered(int);
void Inventory::dateOrdered();

I'd say we'd have to guess way too much, unless you provide a little bit more facts.
Thanks. I will simplify all my methods in the class Inventory.

What my major issue is, is that the itemQuantity always comes back as 7000. Like if the user updated ItemQuantity by 1000 using one of the options in main, I want ItemQuantity to be 8000 Is this because I initialize the initial value incorrectly?
Here is my full code I know there are still other errors and ways to get it more efficient, but I want to first get the program running correctly. I need itemQuantity to update.
Last edited on
Others will not know because they haven't read your previous posts, and you haven't done your homework - you didn't go again through tutorials.

Do you know what scope is?

If you do something like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main(int argc, char* args[])
{//scope of main


if(something() )
{//scope of if

}


{//artificial scope

}
return 0;
}


At the end of every scope, variables are destroyed. If you do something like this:

1
2
3
4
5
6
7
8
9

do{

Inventory someInventory;

something();

}while(true);


someInventory will be:
1)created at the beggining of program
2)destroyed at the end of scope of do while loop
2) because while is true( constant bool here, but doesn't really matter), do while loop will loop again.
3) someInventory is created at the beggining of loop
4) again it's destroyed at the end of scope
5) steps 3-4 are repeated infinitely until while's condition is false.

You indeed fixed a few things, but not all. Again, you haven't really read things about constructors and other things too carefully. As you see, iHutch doesn't know what you mean, because what you said doesn't make sense.

And if you updated that bug and it isn't the case, then you should - again, as I adviced - go through your code and try to debug it yourself. I have no idea why are you learning programming, but I can tell you one thing for sure - lazy programmer is useless programmer. Unless you have will and strength to look through your code and try to understand before asking questions(if you are working on larger project, no one will ever debug your code), and you know what you are doing( if you aren't, you don't know programming language too well, and you should just read tutorials and/or lessons; eventually try to google something you don't understand/have problem with).

And start simple; If you are learning, you should learn on rather small programs(that can eventually grow into larger ones), because if you start building something big, and you lack of knowledge, you will make many mistakes which will make you angry or confused. To avoid it, start simple.

Edit: oh well, you inserted code. Now others will know:)

As I said, remember: this code has only 180 lines of code(and it's not full code), and reading it is already taking some time. If you make a project that consists of few header files, few .cpp files and main file, no one will ever try to guess what you are doing there. YOU should be your best debugger.

Cheers
Last edited on
So essentially:
1
2
3
4
5
do
{
  int x = 42;
}
while ( cond );

Rather than modifying an existing value, each iteration in your loop operates on new value.
Topic archived. No new replies allowed.