Is this correct?

I obviously don't want anyone to do my work for me here, but did I read this and do it correctly? The problem I had to do was:

A liter is 0.264179 gallons. Write a program that will read in the number of liters of gasoline consumed by the user’s car and the number of miles traveled by the car and will then output the number of miles per gallon the car delivered. Your program should allow the user to repeat this calculation as often as the user wishes. Define a function to compute the number of miles per gallon. Your program should use a globally defined constant for the number of liters per gallon.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#define MPG 0.264179 

using namespace std;

int main(void)
{


double liters = 0;
double distance = 0.0;
double mpg = 0.0;
char redo; 

do
{
cout << "Enter the amount of gasoline in liters: ";
cin >> liters;
cout << "\n";

cout << "Enter the number of miles traveled: ";
cin >> distance;
cout << "\n";


mpg = distance / (liters * MPG);



cout << "The gas mileage of this travel is " << mpg << endl;

cout << "\n";

cout << "Do you want to run this program again(yes=1, no=0)? ";
cin >> redo;
cout << "\n";

}while(redo == '1');

return 0;
}
Looks alright to me, but usually when i think of a globally defined constant I think of a variable defined int the global namespace, like this.

1
2
3
4
5
6
7
8
9
10

int global; //This is in global space

int main()
{
int local variable //this is in local space

}

Last edited on
I am a bit new to c++... but i don't see any mistakes in your math, or the code. The only issue i could see is this: "Define a function to compute the number of miles per gallon."

You are using a loop to do this not a function.
Ok yeah I definitely read that wrong then. I am completely confused by function at the moment. I have this now, but it obviously isn't right.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>


using namespace std;

double MPG (double gas,double distance){
    double sum = 0.264179;
    return sum;
    }

int main(void)
{


double gas;
double distance;
double mpg;
char redo; 

do
{
cout << "Enter the amount of gasoline in liters: ";
cin >> gas;
cout << "\n";

cout << "Enter the number of miles traveled: ";
cin >> distance;
cout << "\n";


mpg = distance / (gas * MPG);



cout << "The gas mileage of this travel is " << mpg << endl;

cout << "\n";

cout << "Do you want to run this program again(yes=1, no=0)? ";
cin >> redo;
cout << "\n";

}while(redo == '1');

return 0;
}
closed account (j3Rz8vqX)
Undo what you just did, because you already had a globally defined const of liters per gallon:
#define MPG 0.264179
The naming convention isn't quite correct but this is the global litters to gallons const.

Unless you want:
const double lToG = 0.264179;//Liters to gallon conversion
Being above your main function.


Other than that, when testing redo, you'd want to test it against an integer value, not a character value.

1 != '1'

1==1

In your case, you'd want it to exit on 0 and repeat on 1:
Either:
}while(redo == 0);
or
}while(redo != 1);
Last edited on
Topic archived. No new replies allowed.