Two small errors - Help appreciated

Hey there, I'm a C++ noob but I think I'm slowly beginning to understand it.

A task I've been set asks me to put together a little program which will calculate shipping cost given that;
Up to 100 miles = £5 per mile
101 to 500 miles = £4 per mile
501+ = £3 per mile

I wrote my code for this in C++ but I'm getting two errors, which I can't seem to solve even with some Googling, here's my code:

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950`` ``````/* Shipping cost calculation program */ #include using namespace std; //constants int fixedShippingCost = 50; //variables float variableShippingCost; float ShippingDistance; float ShippingCost; //function prototypes void calculateShippingCost(); int main() { cout << "Enter the distance in miles: "; cin >> ShippingDistance; if (ShippingDistance <= 0); cout << "Error: The distance should be a positive value."; else void calculateShippingCost(); cout << "The cost of shipment is: £ "; ShippingCost; "."; return (0); } /*Calculate the cost of shipment*/ void calculateShippingCost { if ShippingDistance <=100; variableShippingCost = (shippingDistance * 5); ShippingCost = variableShippingCost+fixedShippingCost; else if (500 <= shippingDistance > 100); variableShippingCost = (((shippingDistance-100)*4)+500); ShippingCost = variableShippingCost + fixedShippingCost; else if (shippingDistance > 500); variableShippingCost = (((shippingDistance-500)*3)+2100); ShippingCost = variableShippingCost + fixedShippingCost; }``````

The errors I'm getting are as follows:

 line 27 error C2181: illegal else without matching if line 38 error C2470: 'calculateShippingCost' : looks like a function definition, but there is no parameter list; skipping apparent body

For the first error, I have an "if" three lines above, and for the second error I have another task of mine which I used as a reference for the syntax and it worked there.

I'm probably missing something obvious, so I'm sorry for that but anyone willing to help would be doing me a huge favour :).
Last edited on
 ``2425262728`` `````` if (ShippingDistance >= 0); cout << "Error: The distance should be a positive value."; else void calculateShippingCost();``````
The semicolon after the if's parenthesis counts as a statement, so the cout is not part of the if statement and the else can't find the if. You also need to remove "void" from your call to calculateShippingCost, the compiler thinks this is a declaration instead of a function call.
 ``3738`` ``````void calculateShippingCost {``````
You forgot the parameter list! The () thing. The compiler error is pretty clear on this.

Also, you're using global variables, which is like cussing out the prime minister on public television. You probably want to keep them in main and pass them by reference to calculateShipingCost.
Last edited on
Never use semicolon after "if".
How if else should look like:
 ``123456789101112`` ``````if(conditions) { code; } else if // you can skip this one, or use as many of these as you want. { more code; } else { and more code }``````

So it's a syntax error.

Line 38:
Every function has parameter list.
Let's say you want to make a function that would take 2 numbers and return a result of addition of these numbers.

You would write something like this:

 ``1234`` ``````int addition(int first, int second) { return first + second; }``````

So, how function declaration looks like:
 ``1234`` ``````return_type function_name(parameter_type parameter_name, parameter_type parameter_name) { code to execute }``````

In your program, you just have to write () after calculateShippingCost.

I hope you understand what you did wrong. Try to go through your C++ source once more and write programs along, even the easy and pointless programs, like adding two numbers or just printing something to screen depending on what user typed, etc. You just miss syntax.
As a side note, the syntax is the easy part of C++ ;)
Thanks for the help guys, as I said I'm a total noob - not really done any programming before and I just have a few Powerpoints to go by so I do apologise for wasting anyone's time on such simple stuff.

I should be fine with it now, cheers :).
Topic archived. No new replies allowed.