write a C++ program that will calculate the miles per gallon a car gets given the gallons of gas used and the total miles driven. Assume that the car holds 15 gallons of gas and travels a total of 350 miles on a full tank. The formula for miles per gallon (MPG) is given by
MPG = total miles driven/total gallons used.
(so we already know the answer: 23.33 mpg). Your output should be one line:
“The car gets xxx miles per gallon.”, where the xx is the MPG computed by the program (should be about 23.33)
I keep trying and it won't run. this was my latest attempt:
// This program calculates the miles per gallon a car gets given the gallons of gas used and the total miles driven.
okay thank you! i added the double and the little red lines underneath gallons, miles and MPG disappeared, however, it still gives me an error when i run it. Any idea what else im doing wrong?
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main()
{
double gallons, miles, MPG;
// Get the number of gallons the car holds.
cout << "How many gallons of gas does your car hold? ";
cin >> gallons;
// Get total miles driven.
cout << "How many miles does it travel on a full tank? ";
cin >> miles;
// Calculate the miles per gallon.
MPG = miles / gallons;
// Display the pay.
cout << "The car gets xx miles per gallon" << MPG << endl ;
return 0;
}
Output
How many gallons of gas does your car hold? 15
How many miles does it travel on a full tank? 350
The car gets xx miles per gallon23.3333
1>------ Build started: Project: ConsoleApplication8, Configuration: Debug Win32 ------
1>C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\VC\VCTargets\Platforms\Win32\PlatformToolsets\v141\Toolset.targets(34,5): error MSB8036: The Windows SDK version 8.1 was not found. Install the required version of Windows SDK or change the SDK version in the project property pages or by right-clicking the solution and selecting "Retarget solution".
1>Done building project "ConsoleApplication8.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
but first i get this: C:// name of file is not recognized as an internal or external command, oprable program or batch file. press any key to continue....
thank you thomas, that seemed to be the problem. however, i'm still having trouble with it.
this is my current code:
and when i run the program, it works perfectly up to how many moles does it travel on a full tank, after i input the number and press enter, it takes me out. any suggestions?
so i click start without debugging, and it takes me to a black screen and it asks how many gallons of gas....then i put 15, enter and it asks me how many miles does it travel...then i enter 350 and enter, and it takes me to my homescreen
What happens is this. The program reaches the end. Then the console window (the black screen) closes immediately. There are lots of ways to deal with this, some better than others.
Here you might try, just before the return 0;, just above that line, add these two lines:
1 2
cin.ignore(1000, '\n');
cin.get();
Now it should wait for you to press the 'enter' key.