false if statement executed

Dec 31, 2013 at 11:17am
I wrote an invoice class code but I seem to have some problem with the if statement of which the conditions are false but they are still executed.
Can someone please tell me what I did wrong ?


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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//Invoice.cpp
#include <iostream>
#include <string>
#include "Invoice.h"
using namespace std;

Invoice::Invoice ( string pNumber , string pDescription , int quantity ,  int price )
{
    setPartNumber ( pNumber );
    setPartDescription ( pDescription) ;
    setQuantityOfItem( quantity );
    setPricePerItem( price );
}
void Invoice::setPartNumber( string number )
{
    partNumber = number;
}
string Invoice::getPartNumber()
{
    return partNumber;
}
void Invoice::setPartDescription( string description )
{
    partDescription = description ;
}
string Invoice::getPartDescription()
{
    return partDescription;
}
void Invoice::setQuantityOfItem( int quantity )
{

    if ( quantity <= 0 )

    {
        quantityOfItem = 0;

        cout << " The quantity entered is invalid,so 0 is stroed as a default value " << endl;
    }

    quantityOfItem = quantity ;

}
int Invoice::getQuantityOfItem()
{
    return quantityOfItem;
}
void Invoice::setPricePerItem( int price )
{
    if ( price > 0 )

        pricePerItem = price;
    else
        {
            pricePerItem = 0;

                cout << " The price entered is invalid,so 0 is stored as a default value " << endl;
        }
}
int Invoice::getPricePerItem()
{
    return pricePerItem;
}
int Invoice::getInvoiceAmount( )
{
     return getQuantityOfItem() * getPricePerItem();
}



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//Invoicemain.cpp
#include <iostream>
#include <string>
#include "Invoice.h"
using namespace std;

int main()
{
    int quantity;
    int price;
    
    Invoice bo ( "AD" , "Smart" , 2 , 10 );
  

    cout << " The invoice amount is: " << bo.getInvoiceAmount() << endl;
    return 1;
}



In the code above,when I execute,.both statement in both of the if statements are executed.Can someone shed some light please.
Dec 31, 2013 at 12:27pm
Looking at your entered values, the output must be 20. So what was the output you got, and specify the if statements you are talking of.

Edit: @line 41, there must be an else before
quantityOfItem = quantity ;


Aceix.
Last edited on Dec 31, 2013 at 12:31pm
Jan 1, 2014 at 4:44am
My output is:

The quantity entered is invalid,so 0 is stroed as a default value
The price entered is invalid,so 0 is stored as a default value
The invoice amount is: 20

I don't understand why those first two line are printed???

NB:I know I could use "else" but according to the book am studying,.am supposed to use "if"
Jan 1, 2014 at 5:26am
Your code works fine for me.

Can you post your Invoice.h file?
Jan 1, 2014 at 6:49am
alright here it is:

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
//Invoice.h

#include <string>
using namespace std;

class Invoice
{
public:
    Invoice ( string , string , int , int );
    void setPartNumber ( string );
    string getPartNumber ();
    void setPartDescription ( string );
    string getPartDescription ();
    void setQuantityOfItem ( int );
    int getQuantityOfItem ();
    void setPricePerItem ( int );
    int getPricePerItem ();
    int getInvoiceAmount ();

private:
    string partNumber;
    string partDescription;
    int quantityOfItem;
    int pricePerItem;
};
Jan 1, 2014 at 8:12am
Dont see any problem with the code.

Aceix.
Topic archived. No new replies allowed.