1. ## Infinite loop.

Hello. I made a C program and used while(1) to create loop and defined condition is if(a<0)break; . But when I by mistake typed an alphabet, it continued as an infinite loop. I searched Google but couldn't understand. Please tell me how do I cure this ? If anyone has time, may I have one example ? 2. We're not mindreaders, you know.

Try providing the smallest possible sample of code you can that exhibits your main problem.

I can make an informed guess, but you need to learn how to ask questions that don't waste people's time unnecessarily. 3. Code:
```#include<stdio.h>
#include<math.h>
int main()
{
double zeta5 = 24.88626612344087823195277167496882003336994206804590748738062 , a , b , c , d ,
e = 2.718281828459045235360287471352662497757247093699959574966967 , f , term , integral , p , v;

printf("\nHello.")

while(1)
{
while(1)
{

printf("\nEnter the upper limit (>= 0) : ");
scanf("%lf",&p) ;
if(p<0)printf("please enter a whole number !\n");
else break;

}
.
.
.```
.
.
. 4. Code:
```#include<stdio.h>
#include<math.h>

int main()
{
const double zeta5 = 24.88626612344087823196;
double a, b, c, d;
const double e = 2.718281828459045235360287;
double f, term, integral, p, v;

printf("\nHello.")

while(1)
{
while(1)
{
printf("\nEnter the upper limit (>= 0) : ");
scanf("%lf",&p) ;
if (p < 0)
printf("please enter a whole number !\n");
else
break;
}
/* . . .
The rest of the outer loop
. . .
*/
}
/* . . .
After the outer loop
. . .
*/
}```
What were your inputs for the given code, and what problem did it give you? 5. The problem is obvious.

scanf("%lf", &p) will return if it encounters a character (such as 'X') that would not be expected when reading a floating point value. It will not modify p AND it will leave the character in the stream. The next call of scanf() will encounter the character, so will return immediately leaving p unmodified and the character in the stream. And so on.

Check the return value of scanf() to see if it successfully read the required value. Your choices - if it hasn't - are to either back out of the loop (as long as the outer loops don't rely on a value of p being read) or to read a single character from the stream (e.g. using "scanf("%c", &some_character)") and then try to read the floating point value again.

I won't even want to ask why you're describing a non-negative value as a "whole number". 6. Originally Posted by CodeMonkey Code:
```#include<stdio.h>
#include<math.h>

int main()
{
const double zeta5 = 24.88626612344087823196;
double a, b, c, d;
const double e = 2.718281828459045235360287;
double f, term, integral, p, v;

printf("\nHello.")

while(1)
{
while(1)
{
printf("\nEnter the upper limit (>= 0) : ");
scanf("%lf",&p) ;
if (p < 0)
printf("please enter a whole number !\n");
else
break;
}
/* . . .
The rest of the outer loop
. . .
*/
}
/* . . .
After the outer loop
. . .
*/
}```
What were your inputs for the given code, and what problem did it give you?
When I typed a variable, it kept me asking whether I want to quit the program and the program did not terminate even if I typed N (for no). 7. Is there any command in C language which goes like this :
if(a=real number)continue;
else break; 8. Originally Posted by Ubuntu2014 Is there any command in C language which goes like this :
if(a=real number)continue;
else break;
Nope. If you look at my previous post, you'll see an explanation for your problem, and some possible solutions. 9. Originally Posted by Ubuntu2014 Hello. I made a C program and used while(1) to create loop and defined condition is if(a<0)break; . But when I by mistake typed an alphabet, it continued as an infinite loop. I searched Google but couldn't understand. Please tell me how do I cure this ? If anyone has time, may I have one example ?
use atoi() standard function. 10. Originally Posted by marque use atoi() standard function.
atoi() is insufficient.

atoi("xxc") will return zero, true. So will atoi("0").

Furthermore, atoi("22x") will return 22, not zero. Popular pages Recent additions 