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?
#include <iostream>
#include <iomanip> // setprecision
usingnamespace std;
int main()
{
int choice; // To hold a menu choice
float distance; // to hold distance covered
// Constants for Shipping Company charges rates
constdouble 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;
}
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.
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.
// the item selected from a menu.
#include <iostream>
#include <iomanip>
usingnamespace std;
int main()
{
int choice; // To hold a menu choice
constint weight_MAX = 20;
constint weight_MIN = 0;
distance_MIN;
distance_MAX;
float package_weight,
distance,
total_charges;
// Constants for Shipping Company charges rates
constdouble 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
constint
// 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;
elseif (package_weight > 2 &&
package_weight <= 6)
total_charges = (distance / 500) * 2.20;
elseif (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;
}
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,