Modify the supplied program (functionLab.cpp) to make it work properly.
/* functionLab.cpp Name: CIS 150 Date: CIS 150 Chapter 6 Function Lab */ /* Place your name and section at the top in comments. Read the instructions for each section and then implement your solution within this program. You can comment out sections as you work. Declare variables if necessary. You do not need to get input from the user unless otherwise specified or that code is already written. For this lab you will use the code in the file and break it into functions. Code for functions has been marked for you. To do this you will: 1. Cut the code from main() and paste it below the closing } for main. 2. Declare the local variables in the function. 3. Create the prototypes for the function. 4. In main(), determine which variable declarations need to remain there and which variables should be isolated to the various functions. 5. Call the functions, passing values as needed and using assignment statements for any values returned from the functions. 6. Run and debug your program. 7. When the program is running correctly, upload the completed file to D2L. */ #include <iostream> #include <iomanip> #include <string> using namespace std; /*******************************Prototypes******************************/ /*******************************main line*******************************/ int main() { double x, miles, gallons, mpg, time; bool valid; /* The following while loop should become a function to get and validate input from the user. The function should take three parameters: a prompt, a minimum value, and a maximum value. The minimum and maximum values in the loop below are the values 1 and 600. Use the function to get a value for miles. */ valid = false; while (!valid) { cout << "Enter the miles driven (1-600): "; if (cin >> x) { if (x < 1) { cout << "Error: The minimum value is 1\n"; } else if (x > 600) { cout << "Error: The maximum value is 600\n"; } else { valid = true; } } else { cout << "Error: Invalid number\n"; } } miles = x; /* Reuse the function you created for the previous input validation to get the value for gallons. The same function should work for both. */ valid = false; while (!valid) { cout << "Enter the gallons used (1-50): "; if (cin >> x) { if (x < 1) { cout << "Error: The minimum value is 1\n"; } else if (x > 50) { cout << "Error: The maximum value is 50\n"; } else { valid = true; } } else { cout << "Error: Invalid number\n"; } } gallons = x; /* The following should become one function to calculate the mpg. The function should take miles and gallons as input and return the mpg. */ mpg = miles / gallons; /* The following should become one function to calculate the time (in hours) to drive the given miles going 60 miles per hour. The function should take miles and return miles / 60. */ time = miles / 60; /* The following should become one function to display the mileage information to the user. The function should take miles, gallons, mpg, and time as input. */ cout << "Your mileage specs are\n"; cout << fixed << showpoint << setprecision(2) << "Miles: " << setw(6) << miles << '\n' << "Gallons: " << setw(6) << gallons << '\n' << "MPG: " << setw(6) << mpg << '\n' << "Time: " << setw(6) << time << '\n'; /* Hold the screen open */ /* You can delete the next three lines if using Quincy */ cout << "Press enter to continue..."; cin.ignore(); cin.get(); return 0; }
Enter the miles driven (1-600): -2 Error: The minimum value is 1 Enter the miles driven (1-600): 650 Error: The maximum value is 600 Enter the miles driven (1-600): 345 Enter the gallons used (1-50): 14.5 Your mileage specs are Miles: 345.00 Gallons: 14.50 MPG: 23.79 Time: 5.75 Press enter to continue...