Calculation problem

Write your question here.
Hi guys! I'm kind of a newbie on C++ and I'm stuck on this problem. It's not that it's not compiling but for some reason it's not doing what I imagined it to do. On Cases 2-5 it's not computing "totalCost = totalPrice - totalDct" it's not calculating the discounts if you buy this certain amount of units. When I run it it only shows the full price without discounts. Could you guys somehow help me?
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
70
71
72
73
74
75
#include <iostream>
#include <iomanip>
using namespace std;

int main(){

int units, CASE;

cout << "\n1 Unit of software costs $99\n\n";
cout << "There is a 20 percent discount if you buy 10-19 units, 30 percent for 20-49 units,\n";
cout << "40 percent for 50-99 units, and a 50 percent discount for 100 and above units" << endl;
cout << "How many units would you like to buy? ";
cin >> units;
cout << endl;

        while(units < 1){
        cout << "INVALID AMOUNT" << "\nPlease enter how many you would like to buy ";
        cin >> units;
        cout << endl;
        }

if(units >= 1)
CASE = 1;
else if(units >= 10)
CASE = 2;
else if(units >= 20)
CASE = 3;
else if(units >= 50)
CASE = 4;
else if(units >= 100)
CASE = 5;

        const double price = 99;
        double totalPrice, totalCost, totalDct;

        switch(CASE){

        case 1:
        totalPrice = units * price;
        totalCost = totalPrice;
        cout << "The total cost you would be paying is " << totalCost << endl;
        break;

        case 2:
        totalPrice = units * price;
        totalDct = totalPrice * 0.2;
        totalCost = totalPrice - totalDct;
        cout << "The total cost you would be paying is " << totalCost << endl;
        break;

        case 3:
        totalPrice = units * price;
        totalDct = totalPrice * 0.3;
        totalCost = totalPrice - totalDct;
        cout << "The total cost you would be paying is " << totalCost << endl;
        break;

        case 4:
        totalPrice = units * price;
        totalDct = totalPrice * 0.4;
        totalCost = totalPrice - totalDct;
        cout << "The total cost you would be paying is " << totalCost << endl;
        break;

        case 5:
        totalPrice = units * price;
        totalDct = totalPrice * 0.5;
        totalCost = totalPrice - totalDct;
        cout << "The total cost you would be paying is " << totalCost << endl;
        break;
        }

return 0;
}
CASE will always = 1 as any number will be larger or equal to 1 and the other conditions will never be checked.

Try:
1
2
3
4
5
6
7
8
9
10
if(units < 10)
CASE = 1;
else if(units < 20)
CASE = 2;
else if(units < 50)
CASE = 3;
else if(units < 100)
CASE = 4;
else if(units >= 100)
CASE = 5;

! I did not know that, my program works perfectly now.

Also is there a way to assign a specific number range to a case statement?
e.g case >= 10
would this work? or is what I did the best possible way to assign a range of numbers to a case statement?
Topic archived. No new replies allowed.