Strange If statement? C++

Write your question here.

Ok so I just can't quite seem to get my head around what is going wrong with this if statement. if any one could please point out what I seem to misunderstand about this.

I've made a small store inventory program which should work like this.
Default item stock is 5. if anything less then 5 items are sold it should
then tell you how many you have left. while if you have sold 5 or more it should say that you have sold all remaining stock or that you have sold more then you really have...

only it always seems to be ignoring the first part of the IF statement and going to else and posting even negative numbers. Now if I put in any number with a dash in it like "-1" it will run the IF statement only it will Add more stock items.

I seem to be oblivious to how this really works and I can not seem to find out what I am doing wrong with any online tuts.
any education would be much appreciated!

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
#include <iostream>

using namespace std;

int main()
{
int zero = 0;
int inventoryStock = 5;
int inventorySales = 0;
int inventoryStockBeforeChange = 0;


cout << inventorySales;
cout << "please enter the number of items sold in sales:";
cin >> inventorySales;
inventoryStock = inventoryStock - inventorySales;


/* I'm not sure why it's skipping all of this unless I put in something like -1/-65... It is counting how many are left it's
just not telling me to check inventory?... */

if(inventorySales <= zero ){
    cout << "You have sold everything you had in stock!. Please check Inventory!: " << inventorySales << endl;
    cout << "Sales claims that you have sold: " << inventorySales << " while you only had: " << inventoryStock << " in stock." << endl;
}
else{
    cout << "you still have: " << inventoryStock << " left." << endl;
}
cout << inventoryStockBeforeChange;
    return 0;
}
Line 22:
1
2
// if(inventorySales <= zero ){
if(inventoryStock <= zero ){


Something like this, perhaps:
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
#include <iostream>

int main()
{
    const int zero = 0;
    int stock = 5;
    int sales = 0;

    std::cout << "current stock: " << stock << '\n'
              << "please enter the number of items sold in sales: ";
    std::cin >> sales;

    if( sales < 0 ) std::cout << "can't sell less than zero items\n" ;

    else {

        const int original_stock = stock ;
        stock -= sales;

        if( stock == zero ) std::cout << "You have sold everything you had in stock!\n" ;

        else if( stock < zero )
        {
            std::cout << "Sales claims that you have sold: " << sales
                      << " while you only had: " << original_stock << " in stock.\n" ;
        }

        else std::cout << "you still have: " << stock << " left.\n" ;
     }
}
Ok this was clearly better written then mine. and it works to-boot as well! Thanks.
I'm still at a loss though what was going on with mine that was making it fail to work though :S
LordDeek: you were checking the wrong variable in your if statement... see the very top of JLBorges reply. If you change the variable yours should work as expected.
Oh my mistake! I over looked that at the top! Thanks! :)
Topic archived. No new replies allowed.