Answer to Question #294556 in C++ for Microsoft

Question #294556

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:

  • Output the mileage for each day.
  • Prompt user to enter the price for one-gallon gas.
  • Calculate and output the total cost of your travel in the week. Your car's Miles per Gallon (MPG) is 35.
  • Save the output in a file named “cost.txt”.
1
Expert's answer
2022-02-08T07:54:22-0500

# 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);

}

}


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog