Thread: Extra loop being executed in do-while loop.

  1. #1
    Registered User
    Join Date
    Apr 2013
    Location
    Still looking..
    Posts
    35

    Extra loop being executed in do-while loop.

    Code:
    #include <stdio.h>
    #include<ctype.h>
    
    void try5t(){
        
        char choice;
        int choiceint;
        
        do{
            
        printf("1.\tZoo\n2.\tMall\n3.\tExit\n\n");
        printf("Choose a place by entering its numerical value: ");
        scanf("%c", &choice);
        
        choiceint = choice - '0';
        //DEBUG
        printf("\nchoiceint is %d\n",choiceint);
        
            if(!isdigit(choice) && choiceint > 3){
                printf("You did not enter an accepted digit.\n");
            }
            else if(choiceint != 3){
                switch(choiceint){
                        case 1:
                            printf("You chose the Zoo.\n");
                            break;
                        case 2:
                            printf("You chose the Mall.\n");
                            break;
                }// end of switch
            }// end of else if
                else{
                
                }// end of else
        
            }// end of do 
        while(choiceint !=3);
         
    }
    Loop is repeated an additional time as shown in the screenshot:

    Extra loop being executed in do-while loop.-c11-png

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Code:
    scanf("%c", &choice);
    Change to
    Code:
    scanf(" %c", &choice);
    The space before %c means skip white-space; the linefeed/enter is white-space.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3

  4. #4
    Registered User
    Join Date
    Apr 2013
    Location
    Still looking..
    Posts
    35
    Great link, I've read the 2nd part about flushing the buffer. The author raised an important point, what if the user entered more than 1 character, the program will fail. So, flushing the buffer will solve this issue too.

    However, the code snippet provided is kinda lengthy and I am trying to code a simple while loop to remove any number of characters after the first character.

    This is what I've done :
    Code:
    printf("1.\tZoo\n2.\tMall\n3.\tExit\n\n");     
    printf("Choose a place by entering its numerical value: ");     
    scanf("%c", &choice);
    
    // code to "eat up" all chars after the first char
        while(choice !='\n'){
           scanf("%c", &rejectedChar);
        }
    Sadly, it doesn't work, the while loop seems to be stuck in an infinite loop as the program fails to exit out of the input stream.

  5. #5
    Registered User
    Join Date
    Jun 2013
    Posts
    66
    Sadly, it doesn't work, the while loop seems to be stuck in an infinite loop as the program fails to exit out of the input stream.
    choice is used to control the loop, but never modified within the loop body. Here is a function you can use to flush stdin up to the next line break:
    Code:
    void flush_line(void)
        {
        if (!feof(stdin) && !ferror(stdin))
            {
            int c;
    
            while ((c = getchar()) != '\n' && c != EOF)
                {
                /* noop */
                }
    
            /* Clear EOF but leave stream errors */
            if (feof(stdin))
                {
                clearerr(stdin);
                }
            }
        }

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    32
    Try change
    Code:
    if(!isdigit(choice) && choiceint > 3) {
    to
    Code:
    if(!isdigit(choice) || choiceint > 3) {
    Last edited by Shurik; 07-02-2013 at 08:53 AM. Reason: code tags experiments

  7. #7
    Registered User HelpfulPerson's Avatar
    Join Date
    Jun 2013
    Location
    Over the rainbow
    Posts
    288
    Can't you just do this to get up to the newline :


    Code:
    fscanf( stdin, "%1c[^\n]", &your_variable_name );
    That would be more complex, but it would save you a lot of code.

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Can't you just do this to get up to the newline
    Getting up to the newline isn't the issue - it's getting rid of that newline (and any extra characters preceding it) as to not interfere with later code that reads from stdin.

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        char input1;
        char input2;
    
        printf("Enter character for input1:\n");
        fscanf( stdin, "%1c[^\n]", &input1 );    /* your proposed solution */
        printf("Enter character for input2:\n");
        fscanf( stdin, "%1c[^\n]", &input2 );    /* your proposed solution */
    
        printf("input1 character = %c\n",input1);
        printf("input2 character = %c\n",input2);
    
        if(input2 == '\n')
            printf("\nSecond character is a newline\n");
    
        return 0;
    }
    
    /*
    Enter character for input1:
    a
    Enter character for input2:
    b
    input1 character = a
    input2 character =
    
    
    Second character is a newline
    
    */

  9. #9
    Registered User
    Join Date
    Apr 2013
    Location
    Still looking..
    Posts
    35
    After some research, fgets and sscanf is more recommended over scanf.

    I tried implementing in my code, but there's an infinite loop without any chance for me to enter any input . Why is this so ?

    Edit : Hmm I found out the reason for the infinite loop. It is due to the char array size of 1. The minimum allowed seems to be 2. I assumed 1 would be enough since arr[0] is the char entered and arr[1] is for the null char.. Perhaps my understanding is wrong ?

    However, after putting the array size as 2 , the subsequent loops will be stuck in the switch case which is chosen in the first loop. =/

    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <math.h>
    #include <string.h>
    
    void try5t(){
        
        char chArr[1];
        char choice;
        int choiceint, optionint;
        
        do{
            
        printf("1.\tZoo\n2.\tMall\n3.\tExit\n\n");
        printf("Choose a place by entering its numerical value: ");
        //scanf(" %c", &choice);
        // limits the number of characters extracted from the stream to 1
        fgets(chArr,sizeof(chArr),stdin);
        sscanf(chArr, "%c", &choice); 
        
        choiceint = choice - '0';
    
        
            if(!isdigit(choice) || choiceint > 3){
                printf("You did not enter an accepted digit.\n");
            }
            else if(choiceint != 3){
                switch(choiceint){
                        case 1:
                            printf("You chose the Zoo.\n");
                            break;
                        case 2:
                            printf("You chose the Mall.\n");
                            break;
                }// end of switch
            }// end of else if
                else{
                
                }// end of else
        
            }// end of do 
        while(choiceint !=3);
        
    }
    Last edited by Krabiki; 07-03-2013 at 09:08 PM.

  10. #10
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you do not have enough space in your array to read anything. only null-character is placed in it.

    use bigger buffer
    Code:
    char temp[20];
    fgets(temp, sizeof temp, stdin);
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help - Collect data from Switch loop inside While loop
    By James King in forum C Programming
    Replies: 15
    Last Post: 12-02-2012, 10:17 AM
  2. Replies: 1
    Last Post: 12-26-2011, 07:36 PM
  3. Replies: 23
    Last Post: 04-05-2011, 03:40 PM
  4. for loop ignoring scanf inside loop
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-17-2007, 01:46 AM
  5. loop the loop - feeling foolish
    By estos in forum C Programming
    Replies: 2
    Last Post: 04-07-2007, 02:45 AM