While Loop: invalid data does not loop out..and request a new integer.

Hello all, Sorry for the newbie post but I am trying to complete an assignment. My code does for the most part what the assignment asks.
My code should compile a program that computes how many meters an object falls in 1 second, 2 seconds, etc., up to 10 seconds.

Entering data either under 1 or over 10, spins this into an infinite loop and even though it is re-prompting for a new integer, it does ask, but it loops at that point.



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



//main function
int main()
{

int runningCount = 1;
int time = 0;
double FallingDistance(time);

cout << "Please enter a time between 1 and 10 seconds.\n\n";
cin >> time;

while ( time < 1 || time > 10)

{
cout << "Must enter between 1 and 10 seconds, please re-enter.\n";


}
cout <<"\nSeconds Falling Distance\n";
cout <<"---------------------------------------\n";

for ( runningCount = 1; runningCount <= time; runningCount++)

{
FallingDistance = .5 * 9.8 * pow(runningCount,2.0);
cout << runningCount << " " << FallingDistance <<" meters"<< endl;
}



//Pause the program
cout << "Press ENTER to quit";
cin.sync();

cin.get();


}

Any help you can give me I appreciate it..


1
2
3
4
5
6
7
while ( time < 1 || time > 10)

{ 
cout << "Must enter between 1 and 10 seconds, please re-enter.\n";


}


You tell the user to re-enter the time, but never actually ask the user for anything again. The loop is entered, and the time variable is never changed, so it runs infinitely.

Add cin >> time; into the loop after the cout.
Thank you so very much for your help on this.. I have been reading and re reading that chapter since yesterday.. It is very much obliged..
Topic archived. No new replies allowed.