Hi!

I am doing a C++-program which I have to use functions.

The program is the following:

Write a function that calculates the fuelconsumption, it takes two numbers, litres and kilometers, and gives the consumption in litre per 100 km. The functions must write on screen in main.

Here is what I have now:

#include <iostream>
using namespace std;

double fuelconsumption(int litre, kilometer);

---------------------------------------------------------------------------

int main()
{	

int gas;
cout << "How much gas has been used: " << endl;
cin >> liter;
cout << "How many kilomoters have you driven: " << endl;
cin >> kilometer;
double gas = fuelconsumption(liter, kilometer);
cout << "The fuelconsumption is " << gas << " litre per 100 km.";
cout << endl << endl;

return 0;
}

---------------------------------------------------------------------------

double fuelconsumption(int litre, kilometer)
{

double gas = litre / kilometer;

return gas;
}

Thanks in advance!

//Adam

Recommended Answers

All 9 Replies

>>litre per 100 km.

means :

litre / (100 * km )

Hi!

Thanks for your answer!

I cant get the program to work, can any of you see anything wrong or is everything wrong with this code?

I really need help with this as quick as possible.

//Adam

It could be the -----'s you have. Unless its commented in your actual code but not in the post.

You have no data type on kilometer in your function definition, you must put (int litre, int kilometer). Also, you probably need to cast litre to double double gas = (double)litre/(kilometer*100);

C++ should automatically convert the int into a double, if I'm not mistaken.

C++ should automatically convert the int into a double, if I'm not mistaken.

Not in this case.

@OP , You have this prototype :

double fuelconsumption(int litre, kilometer);

Its wrong. It should be :

double fuelconsumption(double litre, double kilometer);

Notice you need to specify each variable type.
Make the same change to its definition. And add what jonsca said.

Hmm, I must be rusty, int/int = int correct?

Hmm, I must be rusty, int/int = int correct?

If by that you mean an integer number divided by an integer
number will result in the integer division, so :

float area = 1/2;

Above area is 0 since 1 is an integer and so is 2.

I think you mean something like this :

float foo( float left){ return   left/2; }

and a cal like this :

foo(1);

That code should produce 0.5 because the parameter is expecting
a float but a int is passed. So the compiler implicitly promotes
the int to a float. So the division is just like doing this : 1.0f/2 = 0.5

There is no liter defined in int main() and you are still using it.
Similar for kilometers.

Hi!
int main()
{
int liter;
int kilometer;

int gas;
cout << "How much gas has been used: " << endl;
cin >> liter;
cout << "How many kilomoters have you driven: " << endl;
cin >> kilometer;
cout << "The fuelconsumption is " << (liter/(kilometer*100)) << " litre per 100 km.";
cout << endl << endl;

return 0;
}

//Adam

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.