I would like to assign variables in my if statements that will link to my switch statement but I'm not sure how I should do it

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
#include "stdafx.h"
#include <iostream>

using namespace std;

int main()
{
	//Declaring variables
	int package, quantity,sum;
	
	//Initialising variable
	package = 99;

	cout << "How many packages will you be buying? ";
	cin >> quantity;
	sum = quantity*package;


	if (quantity > 0 && quantity < 10)
		{
			1;
		}
	else if (quantity >= 10 && quantity < 20)
		{
			2;
		}
	else if (quantity >= 20 && quantity < 50)
		{
			3;
		}
	else if (quantity >= 50 && quantity < 100)
		{
			4;
		}
	else if (quantity >=100)
		{
			5;
		}
	
	switch (quantity) {
	case 1:
		cout << "Total price is RM" << sum << endl;
		break;
	case 2:
		cout << "Total price is RM" << sum*0.8 << endl;
		break;
	case 3:
		cout << "Total price is RM" << sum*0.7 << endl;
		break;
	case 4:
		cout << "Total price is RM" << sum*0.6 << endl;
		break;
	case 5:
		cout << "Total price is RM" << sum*0.5 << endl;
		break;
	default:
		cout << "You must enter a value which is more than zero"<< endl;
	}
    return 0;
}
this program is to calculate sum of discounted price.
I need to somehow incorporate switch statements in this program as it's part of my homework.
it would be easier if it was only if statements.

Side question: Will nested if statements make the code neater?
^I'm not too familiar with nested if statements.
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>
#include <iomanip>

int main()
{
    const double PACKAGE_COST = 99.00 ;

    int num_packages ;
    std::cout << "How many packages will you be buying? ";
    std::cin >> num_packages ;

    if( num_packages > 0 )
    {
        const double gross_price = num_packages * PACKAGE_COST ;

        double multiplier = 1.0 ;
        if( num_packages > 99 ) multiplier = 0.5 ;
        else if( num_packages > 49 ) multiplier = 0.6 ;
        else if( num_packages > 19 ) multiplier = 0.7 ;
        else if( num_packages > 9 ) multiplier = 0.8 ;

        const double nett_price = gross_price * multiplier ;

        std::cout << std::fixed << std::setprecision(2)
                  << "Gross price: RM " << gross_price << '\n'
                  << " Nett price: RM " << nett_price << '\n' ;
    }

    else std::cout << "number of packages must be greater than zero\n" ;
}
> I need to somehow incorporate switch statements in this program as it's part of my homework.

Opps! Didn't read the second post; so didn't notice this.

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

int main()
{
    const double PACKAGE_COST = 99.00 ;

    int num_packages ;
    std::cout << "How many packages will you be buying? ";
    std::cin >> num_packages ;

    if( num_packages > 0 )
    {
        const double gross_price = num_packages * PACKAGE_COST ;

        int option = 1 ;
        if( num_packages > 99 ) option = 2 ; // multiplier = 0.5 ;
        else if( num_packages > 49 ) option = 3 ; // multiplier = 0.6 ;
        else if( num_packages > 19 ) option = 4 ; // multiplier = 0.7 ;
        else if( num_packages > 9 ) option = 5 ; // multiplier = 0.8 ;

        double nett_price ;
        switch(option)
        {
            case 2 : nett_price = gross_price * 0.5 ; break ;
            case 3 : nett_price = gross_price * 0.6 ; break ;
            case 4 : nett_price = gross_price * 0.7 ; break ;
            case 5 : nett_price = gross_price * 0.8 ; break ;
            default: nett_price = gross_price ;
        }

        std::cout << std::fixed << std::setprecision(2)
                  << "Gross price: RM " << gross_price << '\n'
                  << " Nett price: RM " << nett_price << '\n' ;
    }

    else std::cout << "number of packages must be greater than zero\n" ;
}
Thanks a lot!
Topic archived. No new replies allowed.