Thread: Infinite Loiops

  1. #1
    Registered User
    Join Date
    Nov 2017
    Posts
    4

    Infinite Loiops

    Hi,

    Apologies in advance this is a real newbie question. I tried the search and Youtube with no luck. I'm having a bit of a problem with infinite loops. My problem is what to do when an illegal character has been entered. For example if someone entered "a" in the code below, what is the best method to ask for a re-prompt. I tried doing a "do while" command but it just asked for "value for i" then displayed "press any key to continue". I tried break/continue also with similar results.

    Any help would be appreciated thanks in advance.




    Code:
    #include<stdio.h>
    int main (void)
    {
        float number;
        int i;
            printf( "value for i \n" );
            i = scanf("%f", &number);
                 while (i != 1)
                             {printf("Invalid input ");
                               i = scanf("%f", &number);
                              }
    
    
                        return 0; 
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    It looks like you need to remove the bad entry from the input buffer before the end of the loop. Remember that when scanf() fails the bad data is left in the input buffer and needs to be extracted.

  3. #3
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by deepdrive View Post
    Hi,

    Apologies in advance this is a real newbie question. I tried the search and Youtube with no luck. I'm having a bit of a problem with infinite loops. My problem is what to do when an illegal character has been entered. For example if someone entered "a" in the code below, what is the best method to ask for a re-prompt. I tried doing a "do while" command but it just asked for "value for i" then displayed "press any key to continue". I tried break/continue also with similar results.

    Any help would be appreciated thanks in advance.




    Code:
    #include<stdio.h>
    int main (void)
    {
        float number;
        int i;
            printf( "value for i \n" );
            i = scanf("%f", &number);
                 while (i != 1)
                             {printf("Invalid input ");
                               i = scanf("%f", &number);
                              }
    
    
                        return 0; 
    }
    adding printf to your output to see what is going on with it is a good thing, adding exit(0); from stdlib.h is a good way to stop the loop so you can see that output. break works too.
    Code:
     #include <stdio.h>
     #include <stdlib.h>
     
    int main (void)
    {
        float number;
        int i;
            printf( "value for i \n" );
            i = scanf("%f", &number);
            printf("I = %d\n", i);
        while (i != 1)
       {
            printf("Invalid input\n");
            i = scanf("%f", &number);
            printf("In loop I = %d\n",i);
        //    exit(0);
        }
    return 0; 
    }
    From scanf: On success, the function returns the number of items successfully read.
    This count can match the expected number of readings or fewer, even zero, if a matching failure happens.
    In the case of an input failure before any data could be successfully read, EOF is returned
    .
    functions
    isdigit()

    for digits

    isalpha()

    for letters
    Last edited by userxbw; 11-02-2017 at 03:28 PM.

  4. #4
    Registered User
    Join Date
    Nov 2017
    Posts
    4
    Hi again,

    Thanks for the replies, I'm trying to get the program to re-prompt the user to input the integer again if they use a forbidden character such as a letter. I tried using the break and continue commands to ask the user to re-input the value but it doesn't seem to have triggered the re-prompt.





    Code:
    int main(void)
    {
        float number;
        int i;
        printf("value for i \n");
        i = scanf_s("%f", &number);
        printf("I = %d\n", i);
        while (i != 1)
        {
            printf("Invalid input\n");
            i = scanf_s("%f", &number);
            printf("In loop I = %d\n", i);
            break;
    
    
            continue;
            if (i = 0)
            {
                printf("value for i \n");
                i = scanf_s("%f", &number);
            }
        }
            ;
        return 0;
    }

  5. #5
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Code:
    //assignment
     if (i = 0)
    //comparison
    if ( i == 0)

    Flowchart of continue Statement
    Last edited by userxbw; 11-02-2017 at 05:41 PM.

  6. #6
    Registered User
    Join Date
    Nov 2017
    Posts
    4
    Thanks userxbw for taking the time to reply and explaining the difference between assignment and comparison. Unfortunately even changing the code to use comparison yields the same result. I was just wondering is their a "step command" to bring user back to the beginning of the code to input the parameters again in the event an illegal character or a minus value was entered.

  7. #7
    Registered User
    Join Date
    Jun 2017
    Posts
    88
    I like this code:
    Code:
    #include <stdio.h>
     
    #define SIZE 512
     
    int main(void) {
        printf("%s\n", "Enter a float");
        char buffer[SIZE];
        fgets(buffer, SIZE, stdin);
     
        double value;
        while (sscanf(buffer, "%lf", &value) != 1) {
            puts("Invalid entry! Try Again!");
            fgets(buffer, SIZE, stdin);
        }
     
        printf("You entered %lf\n", value);
     
    }
    or a minus value was entered.
    This code I wrote could be adapted to only take positive by changing value and sscanf to take unsigned instead of signed.

    (There was three posts while I wrote this.)
    Last edited by jack jordan; 11-02-2017 at 06:08 PM.

  8. #8
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by deepdrive View Post
    Thanks userxbw for taking the time to reply and explaining the difference between assignment and comparison. Unfortunately even changing the code to use comparison yields the same result. I was just wondering is their a "step command" to bring user back to the beginning of the code to input the parameters again in the event an illegal character or a minus value was entered.
    I think you forgot to check that link on what the command "break" and "continue" does in a loop.

    C Programming break and continue Statement

    break kicks it out, and continue takes it back up to while ( ... )
    Code:
     #include <stdio.h>
     #include <stdlib.h>
     
    
    int main(void)
    {
        float number;
        int i;
        printf("value for i \n");
        i = scanf("%f", &number);
        printf("I = %d\n", i);
        while (i != 1)
        {
            printf("Invalid input\n");
            i = scanf("%f", &number);
            printf("In loop I = %d\n", i);
          //  break;
     
     
            continue;
            if (i == 0)
            {
                printf("value for i \n");
                i = scanf("%f", &number);
            }
        }
            ;
        return 0;
    }
    gets a return of 0 so because of continue it moves it back up to while not 1 and keep looping because return is 0 (zero);

    that scanf and end line no end line botch job they did on that function screws it up for using scanf in a loop. the post above this worked it out.
    Last edited by userxbw; 11-02-2017 at 07:07 PM.

  9. #9
    Registered User
    Join Date
    Nov 2017
    Posts
    4
    Thanks userxbw for the help so far. That program does indeed work Jack thank you for the example I kind of used that as a guide and went a simpler route with the code as shown below. Just for my own clarity I included a simple equation.


    Code:
    #include <stdio.h> 
    
    int main( void )
    {
       int integer1; 
       int integer2; 
       int sum;  
    
       printf( "Enter first integer\n" );
       scanf( "%d", &integer1 ); 
    
    while (scanf_s("%d", &integer1) < 0  || integer1   != 1)
    
        {
            printf("Invalid input. Please enter a positive number: ");
            scanf_s("%*s");
        }
    
       printf( "Enter second integer\n" ); 
       scanf_s( "%d", &integer2 ); 
    
       sum = integer1 + integer2; 
    
       printf( "Sum is %d\n", sum ); 
    
       return 0; 
    }
    The good news is it recognizes if the user inputs a character however, it gives me errors when I try and input a negative number such as -1.

  10. #10
    Registered User
    Join Date
    Jun 2017
    Posts
    88
    I believe scanf returns the number of values successfully matched, so:
    Code:
    while (scanf_s("%d", &integer1) < 0)
    will never happen.
    **Edit**
    Okay, I see that EOF is often given the value -1, but I've read that this is implementation dependent. It seems it can't be relied upon. I think it would be safer to test it against the expected matches. In this case, that would be 1.

    Also, this is the second successive occurance of scanf. When a scanf is put inside a condition, that scanf is executed. If you are not yet comfortable with this or you feel it to be aesthetically offensive, you could do as you and userxbw were doing above and assign the return value of scanf to an integer before testing the value of that integer in the while condition.

    I used a buffer so that if the user enters in anything unexpected, it doesn't cause confusion in the stdin stream. It is just handled as buffer garbage instead.
    Last edited by jack jordan; 11-03-2017 at 10:46 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 10-14-2011, 11:33 PM
  2. Infinite loop!!!
    By Bizmark in forum C Programming
    Replies: 3
    Last Post: 02-21-2009, 12:09 PM
  3. help with infinite loop please
    By officedog in forum C Programming
    Replies: 4
    Last Post: 11-05-2008, 02:42 PM
  4. infinite loop
    By hlam in forum C Programming
    Replies: 4
    Last Post: 10-29-2008, 01:16 AM
  5. infinite loop
    By ssjnamek in forum C++ Programming
    Replies: 11
    Last Post: 09-25-2005, 05:08 PM

Tags for this Thread