### for loop

Hey I'm having trouble getting the for loop to work, it's pretty similar to another for loop I did earlier only the previous worked fine. The count list is one throught ten, but will show the same distance output for five iterations if 5 is entered as seconds. Any tips will be greatly appreciated.
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344`` ``````#include #include using namespace std; //prototype int fallingdistance(); //main function int main() { int count = 1 ; int time; double distance ; cout << "Please enter time in 1 through 10 seconds.\n\n"; time = fallingdistance(); while ( time < 1 || time > 10) { cout << "Must enter between 1 and 10 seconds, please re-enter.\n"; time = fallingdistance(); } cout <<"\nSeconds falling distance\n"; cout <<"---------------------------------------\n"; for ( count = 1; count <= time; count++) { distance = .5 * 9.8 * pow(time, 2.0); cout << count << " " << distance <<" meters"<< endl; } system ("pause"); return 0; } // falling distance function for a return value in seconds transfer to time int fallingdistance () { int seconds; cin >> seconds; return seconds; } ``````
It might be my noob eyes, what are you trying to do with this code? Whats wrong with the for loop?

Distance = 4.9 * time^2
where time = a number between 1-10 entered by the user.

What is the purpose of `fallingdistance()` Seems like a lot of extra work just to say
 ``12`` ``````cout << "enter number"; cin >> time;``````
The problem is that every time the loop iterates, say with time=5, the value of distance will always be calculated using the same time value.
If you change the distance calculation to:

distance = .5 * 9.8 * pow(count,2.0);

then each time you cycle through the loop, the value of distance will change based on the value of count, from 1 to time.
Last edited on
Yes cerburos it is more work , however I have to learn to how to use many functions as opposed to just using one function. This prblem called for a function to take input and send back to the main function.
I used [distance = .5 * 9.8 * pow(count,2.0);] instead of [distance = .5 * 9.8 * tim(count,2.0);] and the runs nice and clean, thanks for the help.
Topic archived. No new replies allowed.