//User inputs date, location, mileage, and gallons; miles per gallon is computed and all data is output to a text file #include <iostream> #include <iomanip> #include <fstream> using namespace std; int main() { char calculate_MPG(); calculate_MPG(); while (calculate_MPG() == 'y' || calculate_MPG() == 'Y') { calculate_MPG(); } } char calculate_MPG() { string date, location; char confirm; float miles, gallons, MPG; cout << "Enter date (DD/MM/YY): "; cin >> date; cout << "Enter location: "; cin >> location; cout << "Enter number of miles: "; cin >> miles; cout << "Enter number of gallons: "; cin >> gallons; MPG = miles / gallons; cout << "\nDate: " << date << "\nLocation: " << location << "\nMiles: " << miles << "\nGallons: " << gallons << setprecision(5) << "\nMPG: " << MPG; ofstream myfile; myfile.open ("MPG.txt", ios::app); myfile << "\n" << setiosflags(ios::left) << setw(11) << date << setw(15) << location << setw(14) << miles << setw(14) << gallons << setprecision(5) << MPG; myfile.close(); cout << "\n\nInput more data? (y/n) "; cin >> confirm; return confirm; } |
while (calculate_MPG() == 'y' || calculate_MPG() == 'Y')
the function will run 2 times.
|
|