can anyone assist me with fixing these errors on a simple C++ assignment?

The following is the assignment

Shipping Charges

The Fast Freight Shipping Company charges the following rates:
Weight of package (in kilograms) Rate Per 500 Miles shipped
2 Kg or less $1.10
Over 2 Kg but not more that 6 Kg $2.20
Over 6 Kg but not more that 10 Kg $3.70
Over 10 Kg but not more that 20 Kg $4.80


Shipping Rules

FFSC will not ship distances less than 10 miles.
FFSC will not ship distances more than 3000 miles.
FFSC will not ship weights more than 20 Kg.
Do not accept weights of 0 Kg or less
The shipping charge is based on weight in Kg per 500 miles shipped.

Write a program which will:

This is what i need to do:
Display the rate information listed above (don’t worry about borders).
Display the shipping rules
Ask the user for the weight of the package and the distance it is to be shipped.
Prompt the user of shipping rule violations and terminate the program for any invalid inputs
If the input is valid, compute the shipping charges.
Display the charges to the user.

· Display all prices with a $ and 2 decimal places of precision.
· Run the program at least two times
· Provide both source listing and all runtime outputs.

Example:
Weight 3.5 Kg distance 600 miles = $4.40
Weight 1 Kg distance 1001 miles = $3.30
Weight 6.1 Kg distance 499 miles =$3.70
Weight 21 KG distance 500 miles - display a message informing user that package is too heavy to be shipped and exit the program.

Hints
Use nested if statements to handle unacceptable values, various weights and distances
Use modulus to round up distance for 500-mile increments

The following is the code that i have written but it isn't correct. What is wrong with it and can anyone help? I've been struggling with this assignment for hours.

#include <iostream>
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{

double shipcharge;
double kg;
int miles;

double sc;
int p5=(miles/500);
if (miles%500>0)p5++;
if(kg<=2)sc=1.1*p5;
else if(kg<6)sc=2.2*p5;
else if(kg<10)sc=3.7*p5;
else if(kg<20)sc=4.8*p5;
// return sc;
//}


//int _tmain(int argc, _TCHAR* argv[]);
//{
int d=0,cnt=0; //, miles;
// double kg ;
double w;

cout<<"The Fast Freight Shipping Company rates: \n"
<<"---------------------------------\n"
<<"Weight of package (in kilograms) Rate Per 500 Miles shipped\n"
<<"2 Kg or less $1.10\n"
<<"Over 2 Kg but not more that 6 Kg $2.20\n"
<<"Over 6 Kg but not more that 10 Kg $3.70\n"
<<"Over 10 Kg but not more that 20 Kg $4.80\n\n";

cout<<"Shipping Rules:\n"
<<"--------------------------------\n"
<<"FFSC will not ship distances less than 10 miles.\n"
<<"FFSC will not ship distances more than 3000 miles.\n"
<<"FFSC will not ship weights more than 20 Kg.\n"
<<"Do not accept weights of 0 Kg or less\n\n";

while(cnt!=4)
{
cout<<"Weight of the package :";
cin>>kg;
w = kg;
if (w <=0 )
{
cout<<"Weight of package is 0 or less, program will exit now.\n";
exit(1);
}
else if ( w > 20)
{
cout<<"Weight of package too heavy, program will exit now.\n";
exit(1);
}
cout<<"Distance to be shipped :";
cin>>miles;
d = miles;
if (d<10)
{
cout<<"Ship distance too short, program will exit now.\n";
exit(1);
}
else if(d>3000)
{
cout<<"Ship distance too long, program will exit now.\n";
exit(1);
}
cout.precision(2);
cout<<"Shipping charge for "<<w<<" kg for "<<d<<" miles is "<<fixed <<shipcharge<<w<<d<<endl;
cnt++;
}

system("pause");
return 0;
}


The following is what my teacher told me i did wrong. He helped me a little bit but it's not complete yet.


You have the opening brace in the wrong place. move it up to right after the int main ()

You are not defining your variables correctly
look at your code below. You can only define one variable on a line the way you have done it.

Why did you start another program in the same file?

If you take out the second program you need to take out the duplicated variable definitions.

You have some bad syntax in the cout at the end of the code.

I have attached the corrections that I made to get the compilation errors out of your code.
Ok.

The code is a little messed up, so seems to me. Did you lose the overview?
The code seems to compile, but you use several variables without initializing them with any value.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
	double shipcharge;
	double kg;
	int miles;
	double sc;
	int p5 = (miles/500);

	if (miles%500 > 0)
		p5++;


In line 11 you use the variable 'miles' to calc 'p5', but this is not a good idea here. Because 'miles' could be nearly anything now. It could have a value like -5189243. Or it is set to 1. But one thing is for sure, we do not know what it is set to, because you never set it! :-/

You do this kind of mistake more than once!

I think if you clean up your code and correct the error of the type i described, you code might work. But not sure, since i do not want to make your homework, i did not wrote any further things nor tested anything more.

Let me know if you need more help.

Greatings, Maikel
Topic archived. No new replies allowed.