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.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
  1. Open the project or solution named mpg in this folder: ex_starts\ch07_ex1_mpg
  2. 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

  1. 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.
  2. 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.
  3. 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

  1. 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.
  2. 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.
  3. Modify the code in the main() function so it uses the display_file_data() function.
Expert Solution
Step 1: Algorithm :

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.

steps

Step by step

Solved in 4 steps with 3 images

Blurred answer
Knowledge Booster
File Input and Output Operations
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education