stuck: cab company program, endless loop

I'm trying to get this program to return total taxi cab fair an infinite number of times, however, when I use the while statement after for and try to run the program, it just keeps displaying the first total fair rapidly. any help would be appreciated as it is due tomorrow lol. thank you guys in advance !

specifications:
A cab company needs a program to calculate the cost of a cab ride.

The driver will enter the length of the trip in miles.

The miles entered must be greater than 0.

Create a function to calculate the charge. The function should receive one argument, the length of the trip, and should return the total fare for the ride.

The taxi charges a fixed charge of $2.50, plus an additional charge of $1.85 per mile.

Allow the user to calculate any number of trips.

At the end display the total of all the fares.

here is my code:

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

using namespace std;

//function prototypes
double getLength (); 
int main ()
{
	//declare variables
	double lengthTrip = 0.0;
	double totalFee = 0.0;
	double perMile = 1.85;

	//call function to get length in miles and charge
	lengthTrip = getLength();

	totalFee = 2.50 + (perMile * lengthTrip);

	//display total fee
	do{
		cout << fixed << setprecision(2);
		cout << "total fee: $ " << totalFee << endl;
	}
	while (lengthTrip >= 1);
	 
	if (lengthTrip < 1)
	cout << "end of program";

	return 0;

}	//end of main function

//function definitions
	double getLength()
	{
			double lengthTrip = 0.0;
			cout << "enter length in miles: ";
			cin >> lengthTrip;
			return lengthTrip;
	} //end of getLength function 
Last edited on
Your loop condition depends on the value of lengthTrip but that value doesn't change in the loop. What did you mean to accomplish?

Try moving line 21 to line 14.
Last edited on
I'm trying to write a program that asks for the length in miles, and calculates based on the length. It's supposed to calculate any number of trips, and then calculate the grand total in the end. that's the problem I'm having, I don't know where to start the loop.

I moved line 21 to line 14 and that didn't really work, but thank you~!
Move lines 16-18 to after line 21. You want the prompt for the length of the trip and the calculation inside your loop.

Your if statement at line 27 really does not serve a purpose. If the user enters 0, the code will fall through the do/while loop and simply skip the cout at line 28.

Note: Your program does not meet the following stated requirements:
1) Create a function to calculate the charge.
2) At the end display the total of all the fares.

If I were to code this, I would change it as follows:
1
2
3
4
5
6
    while (lengthTrip = getLength())
    {  fare = calculate_fare(length_trip);
        cout << fixed << setprecision(2);
        cout << "total fee: $ " << fare << endl;
        totalFares += fare;  // Accumulate total
    }

Last edited on
Topic archived. No new replies allowed.