In the earlier thread "Square Root Problem", I wrote a little program that contains this loop:
Code:
while (toupper(quit) != 'Y')
{
cout << "Please enter a non-zero number *****:" << flush;
cin >> userinput; //Get the number to be square rooted
if (userinput <= 0 | isascii (userinput)) //Is it a nonvalid number or letter?
{
cout << "The number you entered was invalid. Would you like to quit (y/n)?" << flush;
cin >> quit; //Does the user want to quit?
continue; //If he wants to quit, then quit. Else, continue.
}
a = userinput;
x = a;
/*********Square root loop*************/
lasty = 0; //Null out lasty
for (int i = 0; i <= 15; i++)
{
y = 0.5*(x+a/x); //This is our actual formula
x = y;
if (y == lasty)
{
break; //Our formula has reached its end prematurely (under 15 loops)
}
lasty = y;
}
If I enter a letter (but not an invalid number) I have an infinite loop. THis doesn't make sense as both invalid numbers and letters have the same processing after being detected. When I enter -1, it presents me with the correct prompt. However, the second I enter a letter, it goes into an infinite loop showing the correct prompt over and over again with no time to input anything. Any ideas?