Miles per gallon project

So I'm doing a project and having to write a program. I've written it mostly and I'm really just asking for some peer review. I don't find any problems with it but just want to know if it looks good enough to turn in?

Another thing, since I have 1. done I'm doing the extra credit on 2. I'm thinking about how to do it. Please let me know if I'm on the write track or not.
Would I cin liters and miles for car1, cin liters and miles for car2. then output results for both. Now for announcing which car has the best fuel efficiency would using if else be the right way to display that?

I'm working on this myself but I would just like for someone to double check my work if you don't mind.

1. 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.

2. Modify your program from Practice Program 1 so that it will take input
data for two cars and output the number of miles per gallon delivered by
each car. Your program will also announce which car has the best fuel efficiency
(highest number of miles 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
 #include <iostream>
using namespace std;
const double GALLONS_PER_LITER = 0.264179;
double MilesPerGallon(double miles, double gallons);

int main() {


	int liters;
	double miles;
	double MPG;
	char repeat;
	do
	{
		cout << "Please input how many liters of gasoline is in your vehicle: ";
		cin >> liters;

		cout << "Please input the distance in miles you traveled in your vehicle: ";
		cin >> miles;
		MPG = miles / (liters * GALLONS_PER_LITER);
		//display results
		cout.setf(ios::fixed);
		cout.setf(ios::showpoint);
		cout.precision(2);
		cout << "Consuming " << liters << " liters of fuel to travel "
			<< miles << " miles, means you are getting "
			<< MilesPerGallon(miles, (liters * GALLONS_PER_LITER))
			<< " miles per gallon." << endl;

		cout << "Your vehicle's MPG is: " << MPG << endl;

		cout << "Would you like to repeat the process?\n";
		cout << "Please enter: [Y/N]\n";
		cin >> repeat;
	} while (repeat == 'Y' || repeat == 'y');
	system("pause");
	return 0;
}
double MilesPerGallon(double miles, double gallons) {
	return (miles / gallons);
}
I think it looks alright.

Although I am hoping that you would put more comments on this program like for you do while loop, maybe say like:
1
2
3
4
5
6
7
8
9
//this code allows the user to convert liters to galleons and find MPG multiple times
do{...

//asking user if they want to do this again.
cout << "Your vehicle's MPG is: " << MPG << endl;
cout << "Would you like to repeat the process?\n";
cout << "Please enter: [Y/N]\n";
cin >> repeat;
}while (repeat == 'Y' || repeat == 'y') 


Other than more comments i think your fine. Although, you might also want to do a separate error checking for incorrect input.

Hope that helps,

~Hirokachi

Edit: Thank you, SakurasouBusters. Well, at least now it's fix... Dang it those typos. *mumble* typos *Mumble*. *Walks away muttering to himself about typos*
Last edited on
@Hirokachi
Check this line for possible syntax errors.
} while (repeat == 'Y' || repeat == 'y'
If possible I'd move line 3 & 4 inside your main function.

When creating a variable, int, double and so on I recommend setting the value to 0.

Writing anything more complex than a cout deserves a // comment.

Line 36 is a personal preference, but if you really feel you need it, I'd replace it with cin.
some people will tell you using system is not safe but I just don't like it and don't think new programmers should be encouraged to use it. If your teacher used it in an example then go for it.
Okay I've also tried putting in 2. in for the extra credit
I'm getting no errors and I think everything is fine
what do you all think?

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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
using namespace std;
//Constant definition
const double GALLONS_PER_LITER = 0.264179;
//Function definition
double MilesPerGallon(double miles, double gallons);

int main() {


	int liters1, liters2;
	double miles1, miles2;
	double MPG1, MPG2;
	char repeat;
	do
		//Allow user to read in liters and miles to calculate MPG multipule times
	{
		cout << "Please input how many liters of gasoline is in vehicle 1:\n";
		cin >> liters1;
		
		cout << "Please input how many liters of gasoline is in vehicle 2:\n";
		cin >> liters2;

		cout << "Please input the distance in miles vehicle 1 traveled:\n";
		cin >> miles1;

		cout << "Please input the distance in miles vehicle 2 traveled:\n";
		cin >> miles2;
		//Function Call
		MPG1 = miles1 / (liters1 * GALLONS_PER_LITER);
		MPG2 = miles2 / (liters2 * GALLONS_PER_LITER);
		//display results
		cout.setf(ios::fixed);
		cout.setf(ios::showpoint);
		cout.precision(2);
		cout << "Consuming " << liters1 << " liters of fuel to travel "
			<< miles1 << " miles, means you are getting "
			<< MilesPerGallon(miles1, (liters1 * GALLONS_PER_LITER))
			<< " miles per gallon." << endl;

		cout << "Vehicle 1's MPG is: " << MPG1 << endl;

		cout << "Consuming " << liters2 << " liters of fuel to travel "
			<< miles2 << " miles, means you are getting "
			<< MilesPerGallon(miles2, (liters2 * GALLONS_PER_LITER))
			<< " miles per gallon." << endl;

		cout << "Vehicle 2's MPG is is: " << MPG2 << endl;

		if (MPG1 > MPG2)
			cout << "Vehicle 1 has the best fuel efficiency.";
		else(MPG1 < MPG2);
			cout << "Vehicle 2 has the best fuel efficiency.";
			//Ask user if program should be repeated
		cout << "Would you like to repeat the process?\n";
		cout << "Please enter: [Y/N]\n";
		cin >> repeat;
		//End Loop
	} while (repeat == 'Y' || repeat == 'y');
	system("pause");
	return 0;
}
//Function Definition
double MilesPerGallon(double miles, double gallons) {
	return (miles / gallons);
}
Okay I'm just about done I've talked to my teacher and she gave me pointers and I think I've implemented that pretty well. My last question is my in my else if when both values are equal it preforms correct but I tried putting it to where if both MPGs are 0, I need it to tell me that neither vehicle has moved and unable to determine and when I run it the both values are equal takes over and it never displays the else if for both equal to 0. Sorry but I'm just confused on how to implement both


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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <iostream>
using namespace std;
//Constant definition
const double GALLONS_PER_LITER = 0.264179;
//Function definition
double convert_MilesPerGallon1(double miles1, double liters1);
double convert_MilesPerGallon2(double liters2, double miles2);
int main() {


	int liters1, liters2;
	double miles1, miles2;
	char repeat;
	do
		//Allow user to read in liters and miles to calculate MPG multipule times
	{
		cout << "Please input how many liters of gasoline is in vehicle 1:\n";
		cin >> liters1;
		
		cout << "Please input how many liters of gasoline is in vehicle 2:\n";
		cin >> liters2;

		cout << "Please input the distance in miles vehicle 1 traveled:\n";
		cin >> miles1;

		cout << "Please input the distance in miles vehicle 2 traveled:\n";
		cin >> miles2;
		
		//display results
		cout.setf(ios::fixed);
		cout.setf(ios::showpoint);
		cout.precision(2);
		cout << "Consuming " << liters1 << " liters of fuel to travel "
			<< miles1 << " miles, means you are getting "
			<< convert_MilesPerGallon1( miles1,  liters1)
			<< " miles per gallon." << endl;

		cout << "Vehicle 1's MPG is: " << convert_MilesPerGallon1(miles1, liters1) << endl;
		double MPG1 = convert_MilesPerGallon1(miles1, liters1);
		cout << "Consuming " << liters2 << " liters of fuel to travel "
			<< miles2 << " miles, means you are getting "
			<< convert_MilesPerGallon2(liters2, miles2)
			<< " miles per gallon." << endl;

		cout << "Vehicle 2's MPG is is: " << convert_MilesPerGallon2(liters2, miles2) << endl;
		double MPG2 = convert_MilesPerGallon2(liters2, miles2);
		if (MPG1 > MPG2) {
			cout << "Vehicle 1 has the best fuel efficiency.\n";
		}
		else if (MPG1 < MPG2) {
			cout << "Vehicle 2 has the best fuel efficiency.\n";
		}
		else if (MPG1 == MPG2) {
			cout << "Both are equal, unable to determine which has the better fuel effinciency.\n";
		}
		else if (MPG1 == 0 && MPG2 == 0) {
			cout << "Neither vehicle moved, unable to determine which has the better fuel effinciency.\n";
		}
			//Ask user if program should be repeated
		cout << "Would you like to repeat the process?\n";
		cout << "Please enter: [Y/N]\n";
		cin >> repeat;
		//End Loop
	} while (repeat == 'Y' || repeat == 'y');
	system("pause");
	return 0;
}
//Function Definition
double convert_MilesPerGallon1(double miles1, double liters1) {
	//Function Call
	double MPG1;
		MPG1 = miles1 / (liters1 * GALLONS_PER_LITER);
		
		return MPG1;
}
double convert_MilesPerGallon2(double liters2, double miles2) {
	double MPG2;
		MPG2 = miles2 / (liters2 * GALLONS_PER_LITER);
		return MPG2;
}
Okay I'm just about done I've talked to my teacher and she gave me pointers and I think I've implemented that pretty well.

It is time you should be figuring the final answer yourself. Don't abuse others' help; don't let other people figure out the answer for you.

It is the only way you can advance in your studies.
I've written it all by myself. I've asked for peer review, I asked not how to do it but only if I'm in the right direction. I'm not asking for entire solutions. The program works well except for the last if else which I've tried working at and I'm confused, I'm not abusing anybody. I'm not saying someone do the work for me. This is a forum for beginners and I am trying to learn I'm stumped and asking for help, asking for maybe hints not asking for someone to completely do this for me.
@Aizaia

Starting with line 53..
1
2
3
4
5
6
7
8
else if (MPG1 == MPG2 && MPG1 != 0 && MPG2 != 0) // Check that values are NOT zero
	{
		cout << "Both are equal, unable to determine which has the better fuel efficiency.\n";
	}
else if (MPG1 == 0 && MPG2 == 0) 
       {
		cout << "Neither vehicle moved, unable to determine which has the better fuel efficiency.\n";
	}


When the first else was true, both equal, the second else, was skipped in checking if they were both zeros.

Lines 69, 76: These two functions are identical except for the order of the arguments. You don't need two versions of the function.
1
2
3
4
5
double convert_MilesPerGallon (double liters, double miles) 
{  double MPG;
    MPG = miles / (liters * GALLONS_PER_LITER);
    return MPG;
}


@whitenite1
Oh so just telling the program and using ! for not okay thanks! It really helps me understand.

@AbstractionAnon
I thought about that but if I need the MPG1 and MPG2 would I return both? And would the function still work?

1
2
3
4
5
6
double convert_MilesPerGallon (double liters1, double liters2, double miles1, double miles2){
double MPG1, MPG2;
MPG1= miles1/ (liters1 * GALLONS_PER_LITER);
MPG2 = miles2 / (liters2 * GALLONS_PER_LITER);
return MPG1, MPG2
}

Would that be correct? But if I called the function wouldn't it not be able to tell the difference between if I need MPG1 or MPG2? Or am I just thinking about this wrong?
Would that be correct?

No.

Line 5, you can't return 2 values that way.

Just call the function I posted twice. Once for MPG1 and once for MPG2.
Note that in the function I posted, the return value is just MPG, not MPG1 or MPG2.
Thanks for all your help guys!
Topic archived. No new replies allowed.