C++/mpg

Advertisement


Question
I can figure this out as a math problem but I can't seem to write it as a program.  I know I asked someone about this before but a "friend" crashed my computer and I've lost everything.

Write a program that calculates the miles per gallon that the user gets on her car during a vacation trip. The program should ask the user to enter the number of miles traveled and the number
of gallons of fuel consumed. The program should display the number of miles per gallon achieved on the trip.


Answer
Hello Jonathan, thanks for the question.

Programming this is no different from the math problem solution. It's the same forumla and everything. Hopefully this code sample should have you pointing in the right direction:

#include <iostream>
using namespace std;

int main()
{
float numMiles, numGallons

cout << "How many miles: ";
cin >> numMiles;

cout << "How many gallons: ";
cin >> numGallons;

cout << The miles per gallon achieved was "
    << numMiles / numGallons << '\n';

return 0;
}

This should do the trick for you, if you have any other questions, please do not hesitate to ask.

I hope this information was helpful.

- Eddie

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Eddie

Expertise

I can answer questions about the C++ language, object oriented design and architecture. I am knowledgable in a lot of the math that goes into programming, and am certified by ExpertRating.com. I also know a good deal about graphics via OpenGL, and GUIs.

Experience

I have completed numerous games and demos created with the C++ programming language. Currently employed as a software engineer in the modeling and simulation field. I have about 7 years experience.

©2016 About.com. All rights reserved.