You are living off-campus and drive your car to ODU campus everyday of a week. You wonder how many mileages you travel in a week (just to campus and back home) and how much you need to pay for the gas. You log your travel mileage every day during a week (just to campus and back home). This information has been saved in an input file called “mileage.txt”.
Design an algorithm and write a C++ program to do the following:
# include <iostream>
# include <fstream>
std::ofstream output("cost.txt");
template <typename T>
void write(T what) { std::cout << what; output << what; }
int main() {
std::ifstream input("mileage.txt");
const double MPG = 35;
double mileage = 0;
double buffer;
if (input.is_open()) {
write("Mileage for each day\n");
while (!input.eof()) {
input >> buffer;
mileage += buffer;
write(buffer);
write(" ");
}
write("\n");
std::cout << "Enter prise for one gallon of gas\n";
std::cin >> buffer;
write("The fear in the week is ");
write(mileage / MPG * buffer);
}
}
Comments
Leave a comment