Like Tree3Likes
  • 1 Post By stahta01
  • 1 Post By Matticus
  • 1 Post By Shurik

Extra loop being executed in do-while loop.

This is a discussion on Extra loop being executed in do-while loop. within the C Programming forums, part of the General Programming Boards category; 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: ...

  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:

    Name:  c11.PNG
Views: 244
Size:  11.4 KB

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    2,613
    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.
    Krabiki likes this.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  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
    Krabiki likes this.

  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
    2,435
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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);
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

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, 09:17 AM
  2. Replies: 1
    Last Post: 12-26-2011, 06: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21