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.
#include <iostream>
#include <cmath>
usingnamespace 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;
}
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.
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.