Returning Values from Functions

Below is my program. I need it to run 1 - 12seconds and the distance in feet. But it only shows the 12th second and distance for the 1st second. I need help to get the full output.

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

int main()
{
double seconds = 1;
double distance;
double distanceFalling = ((.5 * 32.2) * (seconds * seconds));

//Intro Screen - this program requires no input
cout << " I will display a table showing the distance" << endl;
cout << "an object falls in 1 through 12 seconds.\n";

//Output Table
cout << "Seconds Distance\n";
cout << "====================\n";

{
for (seconds = 1; seconds < 12; seconds++);
{
distanceFalling;
cout << seconds << " " << distanceFalling << endl;
}
}

return 0;
}
for (seconds = 1; seconds < 12; seconds++); <- remove semicolon


The calculation of distanceFalling should be in the for loop so it picks up each iteration of seconds from 1 to 11. Right now it only gets calculates once when the variable is declared up top.
distanceFalling = ((.5 * 32.2) * (seconds * seconds));


If you edit your post, highlight your code and click on the <> button in the Format palette at the right side of the post, it'll format your code for the forum. It makes it easier to read and point out specific line numbers. :)
instead of
double distanceFalling = ((.5 * 32.2) * (seconds * seconds));

put
1
2
3
4
5
void distanceFalling (double seconds)
{
double distance = (.5 * 32.2) * (seconds * seconds);
cout << distance;
}


In the for loop, put
1
2
3
4
5
6
7
8
double accuracy = 0.1; //this is if you want it to record the distance for
//every 10th of a second.
//to do so,                       instead of seconds++ like below, put
//seconds = seconds + accuracy
for (seconds = 1; seconds < 12; seconds = seconds++)
{
         cout << seconds << " " << distanceFalling(seconds)
}


Last edited on
I got it, thanks for the response!

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
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

double fallingDistance(double seconds)
{
	return (.5 * 32.2) * (seconds * seconds);
}

int main()
{
	double seconds;	

	//Intro Screen - this program requires no input
	cout << "I will display a table showing the distance" << endl;
	cout << "an object falls in 1 through 12 seconds.\n";

	//Output Table
	cout << "Seconds     Distance\n";
	cout << "====================\n";

		for (seconds = 1; seconds <= 12; seconds++)
		{
						cout << seconds << "          " << fallingDistance(seconds) << endl;
		}

return 0;
}

Topic archived. No new replies allowed.