How to use IF statements?

Hi, I'm new to programming with C++ and need a little help with the following program. I'm trying to create a shipping cost calculator that takes the miles and * by the shipment cost + £50.00 fee. There are 3 variable shipment costs that are dependent on the miles.

First 100 miles = 5.50
Over 100 to 500 miles = 4.00
Over 500 miles = 2.50

I've tested my code and I believe it's working fine but I have to achieve a certain value. 550.50 miles must = £2326.25

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
#include <iostream>		//for cin >> and cout <<
#include <iomanip>		//for set precision, etc.
using namespace std;

//global variable declarations
float miles;
float variableShippingCost;
float cost;

int main() // CaculateShippingCost
{
	void calculateVariableCost();
	calculateVariableCost();
	
        cout << "Enter the distance in miles: ";
	cin >> miles;

	calculateVariableCost();
	cout.setf(ios::fixed);
	cout << setprecision(2);
	if (miles <= 0){
	   cout << "ERROR: The distance should be a positive value! " << endl;	
	}
	else{cout << "The cost of shipment over " << miles << " miles is " << cost << ". " << endl;}

	system("pause");
	return(0);
}

void calculateVariableCost()
{	const float
		RATE1 = 5.50,
		RATE2 = 4.00,
		RATE3 = 2.50;
        if (miles <= 100)
	{       variableShippingCost = RATE1;
		cost = (miles * variableShippingCost) + 50;}
	else if (miles >= 100 || miles < 500)
	{       variableShippingCost = RATE2;
		cost = (miles * variableShippingCost) + 50;}
	else if (miles > 500)
	{       variableShippingCost = RATE3;
		cost = (miles * variableShippingCost) + 50;}
}


The current code displays 550.50 miles as £2252.00. Not the correct sum of £2326.25.
I suggets that you learn to use a debbugger to fix this and similar errors. Visual Studio has the best visual debugger. Debugger GIU in Code::Blocks is also appropriate.
Btw, also, you are using functions in such a terrible way, you are making me cry. I hope it is not your instructor who told you to use void functions for calculations.
The current code displays 550.50 miles as £2252.00. Not the correct sum of £2326.25.

I think the algorithm is not being interpreted correctly.
If the distance is up to 100 miles, the cost is simply miles * RATE1 + 50.

For any greater distance, then more than one rate needs to be applied, e.g. if the distance is between 100 and 500, the cost is 100 * RATE1 + (miles - 100) * RATE2 + 50; and for longer distances, all three rates are used.

At least I think that's how it should work.

Also, since the title of the thread is about IF statements, you should simplify the way this is done.

The first test if (miles <= 100) is correct.
The second, there's no need to test miles >= 100, it must be greater than 100, that's what the 'else' does. Similarly the last condition if (miles > 500) is redundant and should be removed, the final 'else' simply picks up everything not matching the first two tests.
Last edited on
freshprince23 wrote:
Please let me know the correct way of doing it.


I would pass the distance as a parameter and have the function return the resulting charge.
1
2
3
4
5
6
7
8
float calculateVariableCost(float miles)
{
    float cost;

    // do the calculations here

    return cost;
}


... see the examples in the tutorial pages
http://www.cplusplus.com/doc/tutorial/functions/
Topic archived. No new replies allowed.