Like Tree2Likes
  • 2 Post By CommonTater

Do while loop problem.

This is a discussion on Do while loop problem. within the C Programming forums, part of the General Programming Boards category; First please view my code below. Code: #include <stdio.h> #include <stdlib.h> int main(void) { int choice; printf("\n===================================================="); printf("\nReport List"); printf("\n===================================================="); ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    67

    Do while loop problem.

    First please view my code below.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void) 
    {
        int choice;
    
                printf("\n====================================================");
                printf("\nReport List");
                printf("\n====================================================");
                printf("\n[1]    Examination Report");
                printf("\n[2]    Student Details Report"); 
                printf("\n[9]    Exit\n");
    
                do {
    
                    fflush(stdin);
                    
                    printf("\nChoice: ");
                    scanf("%d", &choice);
    
                    fflush(stdin);
                        
                    switch(choice)
                    {
                        case 1: 
                            
                            
                            printf ("\nExam record has been created successfully.\n" );    
    
                            break;
                            
                        
                        case 2:
    
                            printf ("\nStudents record has been created successfully.\n" );
                                
                            break;
    
            
                        case 9: printf("Quitting program!\n");
                            exit(0);
                            break;
    
                        default: printf("Invalid choice!\n");
                            break;
                    
                    }
    
     
                } while (choice != '9');
    
        return 0;    
    
    
    
    }
    Now, please view the image below.

    Name:  untitled.JPG
Views: 232
Size:  33.9 KB

    As you can see, when I first input an invalid input it could detect error, but after I input the right value and loop back it again it doesn't detect any error...any guidance?

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Yep... you need to check the return value from scanf() ... look it up in your C library documentation for more details.

    The first time through the conversion failed, choice was some wild random number that just happened to be sitting in memory... thus an invalid choice.

    The second time through you gave it a valid choice and it worked.

    The third time through you gave it a letter, scanf() could not convert it so the previous choice remained and fell into your switch statement.

    Also... lose the fflush(stdin); depending which compiler you are using this can produce some very unpredictable results. Flush is for output streams, not inputs.
    Siaw Ys and iMalc like this.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    67
    Thanks you so much ~ your comment help me a lots !!!

    Here is my new code and its is now working ~

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void) 
    {
        int choice;
    
                printf("\n====================================================");
                printf("\nReport List");
                printf("\n====================================================");
                printf("\n[1]    Examination Report");
                printf("\n[2]    Student Details Report"); 
                printf("\n[9]    Exit\n");
    
                do {
    
                    choice = 0;
                    
                    printf("\nChoice: ");
                    scanf("%d", &choice);
    
                    fflush(stdin);
                        
                    switch(choice)
                    {
                        case 1: 
                            
                            
                            printf ("\nExam record has been created successfully.\n" );    
    
                            break;
                            
                        
                        case 2:
    
                            printf ("\nStudents record has been created successfully.\n" );
                                
                            break;
    
            
                        case 9: printf("Quitting program!\n");
                            exit(0);
                            break;
    
                        default: printf("Invalid choice!\n");
                            break;
                    
                    }
    
     
                } while (choice != '9');
    
        return 0;    
    
    
    
    }
    That weird but after I set choice equal to 0 it works again ^^ Thanks ~

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You did not follow my advice AT ALL... so why are you thanking me?

    1) Check the return value of scanf()... means that when you call scanf() it returns a value that has meaning to your program. You should not ignore this. You should be testing it in a conditional statement so you know whether the conversion (keyboard to int) worked or not... Seriously, look it up!

    2) You are still using fflush(stdin) ... What part of "may cause weird behaviour" did you not get?

    No offense... but really, you did ask for advice...

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    67
    You advised me to check the return value from scanf(). So, I figure that the return choice value may have problem so I set it to 0. So, this has fixed my problem. Regarding the fflush(stdin), I have no choice but to use that because if I ignored fflush my choice will keep looping non stop.

  6. #6
    Registered User
    Join Date
    Nov 2011
    Posts
    63
    Using flush on stdin is very bad because it has undefined results. If you're interested in doing it a different way, it's a little messier but it doesn't rely on undefined behavior:

    Code:
        
    .
    .
    .
    char choice [2]; /* Second slot is for '\0' */
    int choice_size = sizeof(choice) / sizeof(choice[0]);
    .
    .
    .
    printf("\nChoice: ");
                 while( fgets(choice, choice_size + 1, stdin) )
                 {
                     int choice_int = atoi(choice); 
                     switch(choice_int)
                     {
                         case 1: printf ("\nExam record has been created successfully.\n" ); break;
                         case 2: printf ("\nStudents record has been created successfully.\n" ); break;
                         case 9: printf ("\nExiting...\n"); break;
                         default: printf("Invalid choice!\n"); break; 
                     }
                     
                     if(choice_int == 9) { break; }
                     printf("\nChoice: ");
                 }
                  
        return 0;
    .
    .
    .

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Siaw Ys View Post
    You advised me to check the return value from scanf(). So, I figure that the return choice value may have problem so I set it to 0. So, this has fixed my problem. Regarding the fflush(stdin), I have no choice but to use that because if I ignored fflush my choice will keep looping non stop.
    Your choice variable is NOT the return value from scanf() ... LOOK IT UP IN YOUR DOCUMENTATION ... no, don't start typing yet... I mean really look it up and read what it says...

    Code:
    int value;
    int result;
    
    result = scanf("%d", value);
    if (result ...
    We are talking about result... not value... so go do some reading...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Loop problem
    By Tesnik in forum C++ Programming
    Replies: 29
    Last Post: 08-23-2007, 10:24 AM
  2. while loop problem
    By tortan in forum C++ Programming
    Replies: 2
    Last Post: 09-30-2006, 11:11 AM
  3. Problem with for loop
    By irsmart in forum C++ Programming
    Replies: 3
    Last Post: 09-29-2006, 04:26 PM
  4. Loop Problem
    By shawry in forum C Programming
    Replies: 5
    Last Post: 04-01-2006, 08:55 AM
  5. loop problem
    By Noobie in forum C++ Programming
    Replies: 18
    Last Post: 01-29-2003, 01:56 PM

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