Thread: Infinite loop.

  1. #1
    Registered User
    Join Date
    Aug 2014
    Posts
    11

    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. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    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.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Aug 2014
    Posts
    11
    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; 
     
                }
                  .
                  .
                  .
    .
    .
    .
    Last edited by Ubuntu2014; 12-15-2014 at 02:45 AM.

  4. #4
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    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?
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    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".
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    Registered User
    Join Date
    Aug 2014
    Posts
    11
    Quote Originally Posted by CodeMonkey View Post
    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. #7
    Registered User
    Join Date
    Aug 2014
    Posts
    11
    Is there any command in C language which goes like this :
    if(a=real number)continue;
    else break;

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Ubuntu2014 View Post
    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.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  9. #9
    Registered User
    Join Date
    Dec 2014
    Posts
    8
    Quote Originally Posted by Ubuntu2014 View Post
    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.
    Last edited by marque; 12-17-2014 at 08:31 PM.

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by marque View Post
    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.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why am I getting an infinite loop?
    By sameertelkar in forum C Programming
    Replies: 14
    Last Post: 01-01-2013, 01:26 PM
  2. Replies: 3
    Last Post: 10-14-2011, 11:33 PM
  3. Infinite Loop
    By bolivartech in forum C Programming
    Replies: 3
    Last Post: 10-25-2009, 01:31 PM
  4. stays in loop, but it's not an infinite loop (C++)
    By Berticus in forum C++ Programming
    Replies: 8
    Last Post: 07-19-2005, 11:17 AM
  5. Infinite Loop!!!
    By catmom in forum C++ Programming
    Replies: 9
    Last Post: 12-11-2001, 10:44 AM