Shipping charges assignment

Below are the rules everything works except the segment modulus formula isn't working which is causing the code for the total cost to not calculate. Also set precision isn't working as well. Please assist

In this assignment, you will develop both a C++ program and a Raptor program that calculates shipping charges for a freight shipping company. Write a program that asks the user to enter the weight of the package and the distance it is to be shipped, and then displays the shipping charges. Do not accept any packages that are over 50 pounds. Also, do not accept any packages that are to be shipped a distance of less than 10 miles and over 3,000 miles.
Below is the chart to use to calculate the shipping charges.

Freight Shipping Company Rates
______________________________________________________________
Weight of Package (pounds) Rate per 500 miles shipped
_______________________________________________________________
5 pounds or less $1.10
Over 5 pounds but not more than 15 $2.20
Over 15 pounds but not more than 30 $3.70
Over 30 pounds but not more than 50 $4.80
_______________________________________________________________
NOTICE: We do not ship packages over 50 pounds
We do not ship less than 10 miles or more than 3,000 miles


Requirements for your program:

1. The output must be labeled and easy to read as shown in the sample output screens included in this document.
2. When the packages are over 50 pounds or the distance is less than 10 miles and more than 3,000 then flag the input and display a description error message.
3. Use a logical operator when testing the distance to be shipped.
4. Use an if-else statement when determining the rate per 500 miles to be shipped given the weight.
5. Calculate the number of segments (a segment is 500 miles). Round uneven segments up to next whole number. Hint use modulus.
6. Multiply the number of segments * the shipping rate.


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

using namespace std;

int main()
{
	double weight, rate, totalCost;
	int segment, segmentLeft, distance;
	cout << "\nFreight Shipping Company Rates";
	cout << "\n________________________________________________________________________";
	cout << "\nWeight of Package <Pounds/Lbs>                  Rate per 500 miles shipped";
	cout << "\n________________________________________________________________________";
	cout << "\n     5 pounds or less                                 $1.10";
	cout << "\n     Over 5 pounds but not more than 15               $2.20";
	cout << "\n     Over 15 pounds but not more than 30              $3.70";
	cout << "\n     Over 30 pounds but not more than 50              $4.80";
	cout << "\n________________________________________________________________________";
	cout << "\n     NOTICE: We do not ship packages over 50 pounds";
	cout << "\n     We do not ship less than 10 miles or more than 3,000 miles" << endl;
	cout << "\nPlease enter the weight in pounds of your package: ";
	cin >> weight;
	if (weight > 50)
	{cout << "\nERROR!!! this package is over the allowed limit " << endl;
		system("pause");
		return (0);}
	cout << "\nPlease enter the distance in miles of your shipment: ";
	cin >> distance;
	if (distance < 10 || distance > 3000)
	{cout << "\nERROR!!! The entered distance is too under/over allowed distance" << endl;
		system("pause");
		return (0);}
	if (weight <= 2){
		rate = 1.10;}
	else if (weight > 5 && weight < 15){
		rate = 2.20;}
	else if (weight > 15 && weight < 30){
		rate = 3.70;}
	else if (weight > 30 && weight < 50){
		rate = 4.80;}

// The total cost and the segment isn't calculating

	segment = distance / 500;
		distance % 500;
		
	totalCost = segment * rate; 

// I'm unable to get setprecision to work

        cout << setprecision(2) << fixed;
	cout << "\nYour shipping weight is: " << weight;
	cout << "\nYour shipping distance is: " << distance;
	cout << "\nYour shipping rate is: $" << rate;
	cout << "\nNumber of 500 mile segments = " << segment;
	cout << "\nYour total cost is: $" << totalCost;
	cout << "\nThank you for using Freight Shipping Company. Have a nice day!";
	cout << endl << endl;
	system("pause");
	return(0);
}
It's because of integer division.

int segment, segmentLeft, distance; // Change segment to type float or double

segment = distance / 500; should be : segment = distance / 500.0 // note 500.0 instead of 500, 500 is an integer which messes things up

http://bfy.tw/4MzN
Last edited on
Thanks for the response, when I use that code and enter distance as 1200 then have that divided by 500.0 it makes the segment show up as 2.4 the instructions require the segment to be rounded up. I'm not sure how to make the code round up.
Thanks for the help TarikNeaj!
Topic archived. No new replies allowed.