Im trying to write a program thats uses call function, but for some reason the program.
Can someone help point out what i might be missing in the code?

#include <iostream>
#include <cmath>

using namespace std;

void timeoffall();
void velatimpact();

int main ()
 {
	double h;
	cout << "Please enter the height from which the ball was dropped" << endl;
	cin >> h;
	
 	 timeoffall();
	 velatimpact();

 
	return 0;
 
}

void timeoffall(double h)
{	
	double timeoffall;
	timeoffall = sqrt (h/4.9);
	cout << "the ball was in the air for " << timeoffall << "seconds" <<endl;
}

void velatimpact(double timeoffall)
{
	double velatimpact;
	velatimpact = (-9.8 * timeoffall);
	cout << "the velocity at impact was: " << velatimpact << "m/s" << endl;
}

Recommended Answers

All 4 Replies

Your prototype functions need to match your full functions below main() unless you are trying to overload the functions (I don't think you are).
Also the timeoffall value that you get out of the function stays in the function because you aren't passing a variable by reference or making it so your function has a return value.

I would change your prototypes to:

double timeoffall(double);
void velatimpact(double);

And then in your main() you can go:

velatimpact(timeoffall(h));

or split it up by making a variable store the value of timeoffall() and passing that into velatimpact().

And your timeoffall() function should be changed to something like this:

double timeoffall(double h)
{	
	double timeoffall;
	timeoffall = sqrt (h/4.9);
	cout << "the ball was in the air for " << timeoffall << "seconds" <<endl;
	return timeoffall;
}
commented: Very Helpful, advice worked wonderfully +2

Your prototype functions need to match your full functions below main() unless you are trying to overload the functions (I don't think you are).
Also the timeoffall value that you get out of the function stays in the function because you aren't passing a variable by reference or making it so your function has a return value.

I would change your prototypes to:

double timeoffall(double);
void velatimpact(double);

And then in your main() you can go:

velatimpact(timeoffall(h));

or split it up by making a variable store the value of timeoffall() and passing that into velatimpact().

And your timeoffall() function should be changed to something like this:

double timeoffall(double h)
{	
	double timeoffall;
	timeoffall = sqrt (h/4.9);
	cout << "the ball was in the air for " << timeoffall << "seconds" <<endl;
	return timeoffall;
}

now when the program runs, why does the second cout statement reads "nanm/s"?

**EDit** - didnt see the last part of your post

I didn't actually test with your code but nan means not a number. Give me a few min to copy your code and test it out.

I didn't actually test with your code but nan means not a number. Give me a few min to copy your code and test it out.

it works now thanks for your help

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.