if/else program. cannot figure out what's wrong

I cannot figure out why this program is not running. It says that discount wasnt initialized.
Last edited on
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
#include <iostream>
using namespace std;

int main ()
{
    int quantity_shirts;
    int price=12;
    double discount;
    double cost=quantity_shirts * price;
    double total_cost=cost-(cost*discount);
    double cost_shirt=total_cost/quantity_shirts;

    cout <<"How many shirts would you like?\n"<<endl;
    cin >> quantity_shirts;
    if(quantity_shirts>=0)
    {
        if (quantity_shirts>=5 && quantity_shirts<=10)
            discount=.10;
        cout <<"The cost per shirt is"<<cost_shirt<< "and the total cost is $"<<total_cost<<endl;
        if (quantity_shirts>=11 && quantity_shirts<=20)
            discount=.15;
        cout <<"The cost per shirt is"<<cost_shirt<< "and the total cost is $"<<total_cost<<endl;
        if (quantity_shirts>=21 && quantity_shirts<=30)
            discount=.20;
        cout <<"The cost per shirt is"<<cost_shirt<< "and the total cost is $"<<total_cost<<endl;
        if (quantity_shirts>=31)
            discount=.10;
        cout <<"The cost per shirt is"<<cost_shirt<< "and the total cost is $"<<total_cost<<endl;
    }
    else
        cout << "Invalid Input: Please enter a nonnegative integer"<<endl;

    system("PAUSE");

    return 0;
}


You are calculating cost before asking the user to input quantity_shirts.
closed account (o3hC5Di1)
Hi there,

Consider following lines:

1
2
3
double discount;
double cost=quantity_shirts * price;
double total_cost=cost-(cost*discount);


You declare discount as a double, but it has no value as of yet.
Then you declare total_cost, which is calculated using discount - which has no value.

You need to calculate total_cost after discount has been given a value, so in this case in every if statement.

Hope that helps.

All the best,
NwN
so I have to put discount and total_cost (and cost_shirt) in every if before the cout?
closed account (o3hC5Di1)
Here's an example:

1
2
3
4
5
6
7
8
9
if (quantity_shirts>=11 && quantity_shirts<=20)
{
            discount=.15;
            total_cost=cost-(cost*discount);  //now we can calculate it because discount has a value
}

//.. other if's
//below only needs to be done once, it's  the values of the variables that need to be changed in the if's
cout <<"The cost per shirt is"<<cost_shirt<< "and the total cost is $"<<total_cost<<endl;


You have the same issue with double cost_shirt=total_cost/quantity_shirts; - quantity_shirts has no value at that moment.

All the best,
NwN
i tried this but its still not running
Last edited on
No just do this..
cout <<"How many shirts would you like?\n"<<endl;
cin >> quantity_shirts;
int quantity_shirts;
int price=12;
double discount;
double cost=quantity_shirts * price;
double total_cost=cost-(cost*discount);
double cost_shirt=total_cost/quantity_shirts;

And after this start your if or else...
Here, this should work:-

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
#include <iomanip>
#include <iostream>
using namespace std;

int main ()
{
    int quantity_shirts;
    int price=12;
    double cost, total_cost, cost_shirt;

    cout << setprecision(2) << fixed << showpoint; 

    cout <<"How many shirts would you like?\n"<<endl;
    cin >> quantity_shirts;
    if(quantity_shirts>=0)
    {
        if (quantity_shirts>=5 && quantity_shirts<=10)
        {
            cost=quantity_shirts*price;
            total_cost=cost-(cost*0.10);
            cost_shirt=total_cost/quantity_shirts;
            cout <<"The cost per shirt is"<<cost_shirt<< "and the total cost is $"<<total_cost<<endl;
        }
        else if (quantity_shirts>=11 && quantity_shirts<=20)
        {
            cost=quantity_shirts*price;
            total_cost=cost-(cost*0.15);
            cost_shirt=total_cost/quantity_shirts;
            cout <<"The cost per shirt is"<<cost_shirt<< "and the total cost is $"<<total_cost<<endl;
        }
        else if (quantity_shirts>=21 && quantity_shirts<=30)
        {
            cost=quantity_shirts*price;
            total_cost=cost-(cost*0.20);
            cost_shirt=total_cost/quantity_shirts;
            cout <<"The cost per shirt is"<<cost_shirt<< "and the total cost is $"<<total_cost<<endl;
        }
        else if (quantity_shirts>=31)
        {
            cost=quantity_shirts*price;
            total_cost=cost-(cost*0.25);
            cost_shirt=total_cost/quantity_shirts;
            cout <<"The cost per shirt is"<<cost_shirt<< "and the total cost is $"<<total_cost<<endl;
        }
    }
    else
        cout << "Invalid Input: Please enter a nonnegative integer"<<endl;

    system("PAUSE");

    return 0;
}


As you see you forgot to wrap your ifs with {}.
Topic archived. No new replies allowed.