Confused on using the switch?

Im writing a program that shows a menu of the choice then ask the user to choice.
So I need to use a switch statement to
o ask the user to enter an integer distance,
o find the distance factor by dividing the distance by 500 and adding 1 to the quotient,
o multiply the distance factor by the corresponding rate, and
o display the charges.
UPDATE: MY PROGRAM WORKS NOW. Any comments?

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

int main()
{
 
    int choice; // To hold a menu choice
    float distance; // to hold distance covered

    // Constants for Shipping Company charges rates
    const double equalORless2LB = 1.10,
        over2Nless6LB = 2.20,
        over6LBnless10LB = 3.70,
        over10LBless20LB = 4.80;

    // Display the menu and get a choice.
    cout << "\t\tFast Freight Shipping Company - Shipping Charges Menu\n\n"
        << "1. 2 lb or less\n"
        << "2. Over 2 lb but no more than 6 lb\n"
        << "3. Over 6 lb but no more than 10 lb\n"
        << "4. Over 10 lb but no more than 20 lb\n"
        << "5. Over 20 lb\n\n"
        << "Enter your choice regarding the weight of your package: ";
    cin >> choice;

    switch (choice)
    {
        // ask the choice and switch to required case and print the cost by choice
    case 1:
        cout << "Enter distance in miles (whole number please): ";
        cin >> distance;
        cout << "Your charges are $" << setprecision(2) << (equalORless2LB / 500 + 1 ) << endl;
        break;

    case 2:
        cout << "Enter distance in miles (whole number please): ";
        cin >> distance;
        cout << "Your charges are $" << setprecision(2) << ((over2Nless6LB / 500) * distance + 1 )  << endl;
        break;

    case 3:
        cout << "Enter distance in miles (whole number please): ";
        cin >> distance;
        cout << "Your charges are $" << setprecision(2) << ((over6LBnless10LB / 500) * distance + 1) << endl;
        break;

    case 4:
        cout << "Enter distance in miles (whole number please): ";
        cin >> distance;
        cout << "Your charges are $" << setprecision(2) << ((over10LBless20LB  / 500) * distance + 1)  << endl;
        break;

    case 5:
        cout << "Sorry, we do not accept weight over 20 lb\n";
        break;

    default:
        cout << "The valid choices are 1 through 5. Run the\n"
            << "program again and select one of those.\n";
    }
    return 0;
}

Last edited on
No, that is not how you use a switch statement.
Try this:
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
switch (choice)
    {
    case 1:  // package_weight <= 2
        total_charges = (distance / 500) * 1.10;
        break;
    case 2: //  package_weight > 2 && package_weight <= 6
        total_charges = (distance / 500) * 2.20;
        break;
    case 3: //  package_weight > 6 && package_Weight <= 10
        total_charges = (distance / 500) * 3.70;
        break;
    case 4: //  etc
        break;
    case 5: //  etc
        break;
    default:
        if (package_weight <= weight_MIN ||
            package_weight > weight_MAX)
            cout << "The valid choices are 1 through 5. Run the\n"
            << "program again and select one of those.\n";
    }


BTW, I think it a poor design to make the user decide which of five shipping rates to use.
A better approach would be to ask for the shipping weight, then use program logic to decide the total charges.
First your compiler should be giving you very big hints as to whether you are doing thing correctly, since there are quite a few compile errors I would say you are not doing the assignment correctly.


When using a switch statement you can not use "ranges", you need discrete values. Also since there are no case statements your switch seems to be malformed.

switches are glorified lookup tables.

consider some advanced ideas, since they already gave you the basic idea.
1) if/else redundancy.
if (package_weight <= 2)
total_charges = (distance / 500) * 1.10;
else if (package_weight > 2 && ///WHAT DO YOU KNOW HERE? You know that it is not <= 2. If it is not <= 2, it must be > 2. So addinga check for > 2 on the nested if inside the else is redundant. The compiler MAY figure that out and ignore it, but its better if you don't put it in, so in case the compiler misses it the extra step is not done.

2)
one of the main features of a switch is the fall-through.
case 0:
case 1:
case 2: cout << "0,1,2 all happened"; break; //same as <= 2 for positive values
case 3:
case 4:
case 5:
case 6: cout << "and this is 3,4,5,6"; break;
this is a tiresome way to implement a small range of integers like you have. It may be a little faster than the if/else but its really annoying to read and write it.




Last edited on
The help others gave you, BlueCOCO1, is good but reviewing switch basics might be beneficial.
https://www.learncpp.com/cpp-tutorial/switch-statement-basics/
@BlueCOCO1 Please don't update the original code like this. The replies now don't correspond to what was originally posted.
Just happen to have the original code.

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
// the item selected from a menu.
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    int choice;       // To hold a menu choice
    const int weight_MAX = 20;
    const int weight_MIN = 0;
    distance_MIN;
    distance_MAX;

    float package_weight,
        distance,
        total_charges;

    // Constants for Shipping Company charges rates
    const double equalORless2LB = 1.10,  // Rate if package is 2lb or less per 500 miles
        over2Nless6LB = 2.20,  // Rate if package is over 2 lb but less than 6 lb per 500 miles
        over6LBnless10LB = 3.70;  // Rate if package over 6 lb but less than 10 lb per 500 miles
    over10LBless20LB = 4.80;  // Rate if package over 10 lb but less than 20 lb per 500 miles

// Constants for menu choices
    const int

        // Display the menu and get a choice.
        cout << "\t\tFast Freight Shipping Company - Shipping Charges Menu\n\n"
        << "1. 2 lb or less\n"
        << "2. Over 2 lb but no more than 6 lb\n"
        << "3. Over 6 lb but no more than 10 lb\n"
        << "4. Over 10 lb but no more than 20 lb\n\n"
        << "5. Over 20 lb\n\n"
        << "Enter your choice regarding the weight of your package: ";
    cin >> choice;
    cout << "\n Enter distance in miles (whole number please) : ";
    cin >> distance;
    cout << "\n Your charges are ";

    // Set the numeric ouput formatting.
    cout << fixed << showpoint << setprecision(2);

    // Respond to the user's menu selection.
    switch (choice)
    {
        if (package_weight <= 2)
            total_charges = (distance / 500) * 1.10;
        else if (package_weight > 2 &&
                 package_weight <= 6)
            total_charges = (distance / 500) * 2.20;
        else if (package_weight > 6 &&
                 package_Weight <= 10)
            total_charges = (distance / 500) * 3.70;
        default:
            if (package_weight <= weight_MIN ||
                package_weight > weight_MAX)
                cout << "The valid choices are 1 through 5. Run the\n"
                << "program again and select one of those.\n";
    }

    return 0;
}


Andy
two comments.
1) just add a new response next time. removing the original question makes it confusing to anyone coming along later.

2) repeated code is bad. why not ask for how many miles out side the switch, before you ask for a weight range choice? You could also simplify by dividing all your constants by 500 up top (not critical, just a little cleaner). eg const double equalORless2LB = 1.10/500.0,
Last edited on
Also the cout... can be after the switch as well. The only thing needed in the switch is setting the required charge.
Topic archived. No new replies allowed.