What is wrong with my loop?

I'm trying to get it where a user can keep entering the weights/distance of shipment of an item until the user enters zero. The loop will then add up the sum to ship all items entered. Before I add the loop I'm getting the correct calculations. When I add the loop, my total gets to zero no matter what. Whats wrong with my code? I have a calculate charge function I didn't display that I'm calling from.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 int main()
{
	double kilograms = 1;
	double miles = 0;
	double total = 0;
	

	

	while (kilograms <= 0)
	{

		cout << "Please enter weight for item" << endl;
		cin >> kilograms;
		cout << "Please enter distance for shippment" << endl;
		cin >> miles;

		total = calculateCharge(kilograms, miles, total);
	}
Last edited on
boordman29 wrote:
until the user enters zero.
while (kilograms <= 0)
Yeah, I agree with integralfx.

What do you mean?

What do you want your code to do, again?

~ Hirokachi
Last edited on
The main function should loop to handle multiple packages until a weight of zero is entered, does that make sense?
closed account (LA48b7Xj)
while(kilograms != 0)

Would mean perform the loop while kilograms is not equal to 0 but maybe you would want to check if kilograms was 0 after you read into kilograms.
kilogram =1 so the while loop will never execute,
either change the while loop or change the kilograms.
I have initialized kilograms to 1 and 0, but I'm still getting the same results, am I using the right type of loop?
can you show us the


calculateCharge


function maybe the math is wrong in that
here it is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14

double calculateCharge(double weight, double distance, double cost)
{
	if (weight <= 2)
		cost = 3.10 * (distance / 500);
	else if (weight >= 3 && weight <= 6)
		cost = 4.20 * (distance / 500);
	else if (weight >= 7 && weight <= 10)
		cost = 5.30 * (distance / 500);
	else if (weight > 10)
		cost = 6.40 * (distance / 500);
	return cost;
	
}
Maybe you want to not compare the value of kilograms to zero (0) or greater for that condition to be true but compare the value of kilograms to only that is greater then zero (0) for the condition to be true.

So for instance it would look like:
while (kilograms > 0) {...}
not:
while (kilograms => 0) {...}
or:
while (kilograms <= 0) {...}
or:
while (kilograms != 0) {...}

Would that work better?

~ Hirokachi
I've used
while (kilograms > 0)
and
while (kilograms !=0)

Still getting $0.00 as my output.
Still getting $0.00 as my output.

Your total variable isn't a total. It is the result of the last calculation. When you enter a kilograms value of 0 I am guessing you also enter a miles value of 0 and the resulting calculation's result is 0.
Hello boordman29,

Your code is 90+% there. This is what I did to make it work. Compare it to what you have. The changes are all commented.
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
double kilograms = 0;  //  Can be 0. Makes no difference
double miles = 0;
double total = 0;

while (true)  //  Changed from <= 0. If kept should be kilograms > 0
{

	CLS;  //  A #define CLS system("cls")

	cout << "Please enter weight for item" << endl;
	cin >> kilograms;

	if (kilograms == 0) break;  // Needs to be chedked here. Otherwise you will have to enter miles
	                            // before the while condition would check for zero. Otherwise the output would be 0.00.
		
	cout << "Please enter distance for shippment" << endl;
	cin >> miles;

	total = calculateCharge(kilograms, miles, total);

	//  Added to see the output. Add setpersion(0) before miles to leave off decimal point.
	std::cout << fixed << setprecision(2) << "\n Total charge is: " << total << " for a shipment of " << kilograms << " kilograms for a distance of " << setprecision(0) << miles << " miles." << endl;

	// Added to pause program to see output before CLS changee to what you like.
	cout << "\n\n\n\nFin Press any key to continue -- > ";
	_getch();
}

and
1
2
3
4
5
6
7
8
9
10
11
12
13
double calculateCharge(double weight, double distance, double cost)
{
	if (weight <= 2)
		cost = 3.10 * (distance / 500.0);  //  500.0 not needed, but a more proper to write the formula.
	else if (weight >= 3 && weight <= 6)
		cost = 4.20 * (distance / 500.0);
	else if (weight >= 7 && weight <= 10)
		cost = 5.30 * (distance / 500.0);
	else if (weight > 10)
		cost = 6.40 * (distance / 500.0);
	return cost;

}


Hope that helps,

Andy
Last edited on
Topic archived. No new replies allowed.