Open the project or solution named mpg in this folder: ex_starts\ch07_ex1_mpg Review the code and run the program to refresh your memory on how it works. Notice that, unlike the program in this chapter, this program lets the user perform more than one calculation, it stores the values the user enters in a text file, and it displays the total miles, total gallons, and average miles per gallon when the program starts and after each entry. Add a function that calculates the miles per gallon Define a function named calculate_mpg() before the main() function that calculates the miles per gallon. This function should accept two double values for the miles and gallons, round the result to two decimal places, and return the result as a double type. Modify the code in the main() function so it uses the calculate_mpg() function. Note that the miles per gallon is calculated in three different places. Move the definition for the calculate_mpg() function after the main() function. When you run the program, you’ll get a compile-time error that the function is not found. To fix this problem, add a declaration for the calculate_mpg() function before the main function. Add a function that displays the totals Declare a function named display_file_data() that will display the data in the text file. This function won’t accept any parameters or return any data. Define the display_file_data() function after the main() function. To do that, you can copy one of the occurrences of the code in the main() function that defines and opens the file and processes the data. Adjust the code as needed so it works within the function. Modify the code in the main() function so it uses the display_file_data() function.
- Open the project or solution named mpg in this folder: ex_starts\ch07_ex1_mpg
- Review the code and run the program to refresh your memory on how it works. Notice that, unlike the program in this chapter, this program lets the user perform more than one calculation, it stores the values the user enters in a text file, and it displays the total miles, total gallons, and average miles per gallon when the program starts and after each entry.
Add a function that calculates the miles per gallon
- Define a function named calculate_mpg() before the main() function that calculates the miles per gallon. This function should accept two double values for the miles and gallons, round the result to two decimal places, and return the result as a double type.
- Modify the code in the main() function so it uses the calculate_mpg() function. Note that the miles per gallon is calculated in three different places.
- Move the definition for the calculate_mpg() function after the main() function. When you run the program, you’ll get a compile-time error that the function is not found. To fix this problem, add a declaration for the calculate_mpg() function before the main function.
Add a function that displays the totals
- Declare a function named display_file_data() that will display the data in the text file. This function won’t accept any parameters or return any data.
- Define the display_file_data() function after the main() function. To do that, you can copy one of the occurrences of the code in the main() function that defines and opens the file and processes the data. Adjust the code as needed so it works within the function.
- Modify the code in the main() function so it uses the display_file_data() function.
Algorithm: Calculate Miles Per Gallon (MPG)
1. Include necessary header files: iostream, fstream, iomanip, cmath.
2. Declare function prototypes:
- double calculate_mpg(double miles, double gallons)
- void display_file_data()
3. Define the main function.
1. Initialize variables miles, gallons.
2. Open a file named "mpg.txt" for appending data (ios::app).
3. If the file fails to open, display an error message and exit.
4. Display a prompt for the user to enter miles driven.
5. Read and store the user input in the miles variable.
6. Enter a loop until the user enters -1.
1. Display a prompt for the user to enter gallons used.
2. Read and store the user input in the gallons variable.
3. Calculate MPG using the calculate_mpg function.
4. Write the miles, gallons, and MPG to the "mpg.txt" file.
5. Display the calculated MPG.
6. Display a prompt for the user to enter miles driven.
7. Read and store the user input in the miles variable.
7. Close the "mpg.txt" file.
8. Call the display_file_data function to display the data from the file.
9. Return 0 to indicate successful program execution.
4. Define the calculate_mpg function:
1. Accept two double parameters: miles and gallons.
2. Calculate MPG (miles divided by gallons).
3. Round the result to two decimal places using the round function from cmath.
4. Return the rounded MPG.
5. Define the display_file_data function:
1. Open the "mpg.txt" file for reading.
2. If the file opens successfully:
1. Initialize variables totalMiles, totalGallons, and entryCount to zero.
2. Display a header for the data in the file.
3. Enter a loop to read data from the file:
1. Read miles, gallons, and MPG from the file.
2. Increment entryCount.
3. Add miles and gallons to totalMiles and totalGallons.
4. Display the entry number, miles, gallons, and MPG.
4. If there is at least one entry (entryCount > 0), calculate the average MPG.
5. Display the total miles, total gallons, and average MPG.
6. Close the file.
3. If the file fails to open, display an error message.
6. End of the program.
Step by step
Solved in 4 steps with 3 images