Beginner needs help ASAP

Suppose a vehicle needs 4 liters to travel 100 kilometers. Write a C++ program that will calculate and display its fuel consumption in miles per gallon (mpg)

Please help me. This is my first time learning about C++
-This is a very simple problem that hardly requires a calculator.
-What have you tried so far, where is your code?
-We can only help you based on what you've already done, this is not a forum where people will do your homework for you :)

With that in mind, feel free to post any code you've produced and we can go from that!
oh sorry !! Heres what I did so far

#include <iostream>

using namespace std;
//conversion factor - declare a constant
const double li2gal = 0.264172;
const double ki2mi = 0.6214;
// Function Prototypes
double Liter2Gallons (double Liters);
double kilometers2miles (double kilometer);

int main()
{
//variables to hold user input and calculation result
double Gallons;
double Liters;
double Miles;
double Kilometers;
// prompt the user using a for loop
for (int i=1; i < = 1; i++)
{

cout >> "Enter the number of Liters of Gasoline: ";
cin >> Liters;
if (cin.fail())
{
cout << " Wrong input. Please enter numeric values. ";
return cin.fail ();

Gallons = Liters2Gallons(Liters);
cout << Liters << "Liters is "<< Gallons << " Gallons." << '\n';
cout << "Enter the number of Kilometers traveled by car: " ;
cin >>Kilometers << "Kilometers is " << Miles << "Miles." <<'\n';
cout <<Kilometers;
Miles = Kilometers << "Kilometers is " << Miles << "Miles." << '\n';
Thanks for the code.
First, when posting always use code tags ( the <> icon on the right of the text box) it makes code much easier to read!

Second this is your code:
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
#include <iostream>

using namespace std;
//conversion factor - declare a constant
const double li2gal = 0.264172;
const double ki2mi = 0.6214;
// Function Prototypes
double Liter2Gallons (double Liters);
double kilometers2miles (double kilometer);

int main()
{
//variables to hold user input and calculation result
double Gallons;
double Liters;
double Miles;
double Kilometers;
// prompt the user using a for loop
for (int i=1; i < = 1; i++)
{

cout >> "Enter the number of Liters of Gasoline: ";
cin >> Liters;
if (cin.fail())
{
cout << " Wrong input. Please enter numeric values. ";
return cin.fail ();

Gallons = Liters2Gallons(Liters);
cout << Liters << "Liters is "<< Gallons << " Gallons." << '\n';
cout << "Enter the number of Kilometers traveled by car: " ;
cin >>Kilometers << "Kilometers is " << Miles << "Miles." <<'\n';
cout <<Kilometers;
Miles = Kilometers << "Kilometers is " << Miles << "Miles." << '\n';



There are a couple of things to consider:
*lines 5 and 6: avoid using global constants as much as you can, you can keep them in your main, unless you really need not to.

*lines 8, 9 and 14 through 17: avoid using capping the first letter of function and variables, by convention caps are for structs and classes

*lines 8 and 9: you have your function prototypes there but the declarations are nowhere to be found (did you forger to include the end of your code?)

*line 19: (int i=1; i < = 1; i++) there shouldn't be a space between < and =. (int i=1; i <= 1; i++)

*line 22: cout >> "Enter the number of Liters of Gasoline: "; note that the operator ">>" is for cin, and cout goes with "<<"

*Watch out for your curly brackets, your missing three of them, one to close your for loop, your if statement, and the one at the end of your main function
I suggest indenting your code so that you can see those errors more evidently when you look at it


I suppose this is what you were trying to do:

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
#include <iostream>

using namespace std;

//conversion factor - declare a constant
const double li2gal = 0.264172;
const double ki2mi = 0.6214;

// Function Prototypes
double liter2Gallons (double);
double kilometers2miles (double);

int main()
{
	//variables to hold user input and calculation result
	double gallons;
	double liters;
	double miles;
	double kilometers;
	// prompt the user using a for loop
	for (int i=1; i <= 1; i++)
	{
		cout << "Enter the number of Liters of Gasoline: ";
		cin >> liters;
		
		if (cin.fail())
		{
			cout << " Wrong input. Please enter numeric values. ";
			return cin.fail ();
		}

		gallons = liter2Gallons(liters);
		cout << liters << "\nLiters is "<< gallons << " Gallons." << "\n";
		cout << "Enter the number of Kilometers traveled by car: " ;
		cin >> kilometers;
		miles = kilometers2miles(kilometers);
		cout << "\nKilometers is " << miles << "Miles." << '\n';
	}
	
	return 0;
}

double liter2Gallons (double liters)
{
	double gallon = liters * li2gal;
	
	return gallon;
}

double kilometers2miles (double kilometers)
{
	double miles = kilometers * ki2mi;
	
	return miles;
}



Hope this helps,

Regards,

Hugo


EDIT: Now re-reading your task you've successfully calculated the number of miles and gallons, you simply need to divide miles by gallons to find your fuel consumption in mpg. Either making another function or simply doing double consumption = miles / gallons; and then displaying it.
Last edited on
this website is so awesome thank u so much !
My pleasure ;)
Topic archived. No new replies allowed.