Help with this simple program


just starting out with programming and need some help figuring this out

Miles Per gallon
A car holds 12 gallons of gasoline and can travel 350 miles before refueling.
Write a program that calculates the number of miles per gallon the gar gets.
Display the result on the screen.
Hint: use the following formula to calculate miles per gallon (MPG):
MPG = miles driven/Gallons of Gas used



// Assignment 3 Example
// 6/12/2016
// Program to calculate mpg
// Page 143, #1

#include <iostream>

using namespace std; int main()

{

double gallons = 12 miles = 350;

double MPG = miles/gallons;

cout << "A car that holds " << gallons << "gallons of

gasoline and \n "

<< "travels " << miles << "before refueling \n"

<< "gets" << MPG << "MPG " \n\n ";

system ("pause ");

return 0;

}
closed account (48T7M4Gy)
Please use code tags around your code using Format menu on the right <>

For a start double gallons = 12, miles = 350;

better still use two separate lines, one per variable.

// Assignment 3 Example
// 6/12/2016
// Program to calculate mpg
// Page 143, #1

#include <iostream>

using namespace std; int main()

{
int galGas, milesFullTank, cal;
double average;

cout << "Please enter the number of gallons of gas your car can hold: ";
cin >> galGas;
cout <<milesFullTank the number of miles your car can be driven on a full tank: ";
cin >> milesFulllTank;


cal = milesFullTank / galGas;

cout << "\nThe number of miles per gallon your car gets: " << cal << "." << endl << endl;



return 0;


}


Still getting errors but almost have it
closed account (48T7M4Gy)
Ok well done we look forward to the next edition :)

Tip: int variables not a good move for galGas etc.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

int main()
{
    const double gallons = 12.0 ; // A car holds 12 gallons of gasoline
    const double miles = 350.0 ; // and can travel 350 miles before refuelling

    const double mpg = miles / gallons ; // formula: MPG = miles driven/Gallons of Gas used

    std::cout << "A car that holds " << gallons << " gallons of gasoline and\n"
              << "travels " << miles << " miles before refuelling\n"
              << "gets " << mpg << " miles per gallon.\n";

}
Topic archived. No new replies allowed.