Mar 22, 2015 at 5:11pm UTC
basically i need to create a function that calculates time in seconds before two objects meats which are moving forward each other
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
#include <cstdlib>
#include <iostream>
#include <cmath>
using namespace std;
double meet_time(double , double , double )
{
double time, kmh1, kmh2, km, ;
time = ((kmh1+kmh2)/km)*3600;
return time;
}
int main()
{
double kmh1, kmh2, km;
cout<< "enter speed one" ;
cin>> kmh1;
cout<< "enter speed two" ;
cin>> kmh2;
cout<< "enter distance" ;
cin>> km;
cout<< meet_time (kmh1, kmh2, km);
system("pause" );
return 0;
}
can someone check for my mistakes ?:)
time formula i guess this one is right time = (km/(kmh1+kmh2))*3600;
Last edited on Mar 22, 2015 at 5:12pm UTC
Mar 22, 2015 at 5:29pm UTC
The formula in your function is different from the formula you do mention.
What does your compiler say about your code? It is good useful to learn to check and interpret what the compiler says.
Should the parameters of a function have names?
Mar 22, 2015 at 5:32pm UTC
seams like compiler doesn't mind but it gives out wrong answers :)
Mar 22, 2015 at 6:16pm UTC
The values that you ask from the user are not used in the function meet_time.
The variables used on line 10 are declared on line 9 and have uninitialized values. In other words the function will return undefined value.
Furhermore, another compiler does not like the last comma of line 9.
Mar 22, 2015 at 7:55pm UTC
a function should have type and parameters. I think you did not pass the values of inputs into function.
for line 7. double meet_time(double x, double y, double y), in this way, it will pass the values of inputs.
And for line 9, set time to 0, else are useless. You should use x,y,z because in the main function, you will assign the value of kmh1, kmh2, km to x,y,z.