Thread: scanf with switch statement, weird behavior

  1. #1
    Registered User
    Join Date
    Feb 2006
    Location
    Philadelphia, PA
    Posts
    27

    scanf with switch statement, weird behavior

    Below is the code that I'm having trouble with:
    Code:
    while(TRUE){
            
            printf("Type in one of options below:\n1. Create File in root\n2. Navigate to Directory\n3. Quit\n");
            printf(">>");
            scanf("%d", &option);
            fflush(stdin);
            switch(option){
                case 1: 
                        createFile(disk,0);
                        break; 
                case 2: 
                        navDir(disk, 0);
                        break; 
                case 3: 
                        writeStructs(disk);
                        exit(1);
                        break;
                
                default: break;
                
            }
        }
    On the first run everything works, but after one of the functions returns back to a switch statement scanf doesn't wait for input and I get the output as below:
    Code:
    Type in one of options below:
    1. Create File in root
    2. Navigate to Directory
    3. Quit
    >>1
    Enter file name and extension (file.doc): test.doc
    Enter Content terminated by %>>this is a test%
    Type in one of options below:
    1. Create File in root
    2. Navigate to Directory
    3. Quit
    >>Enter file name and extension (file.doc): Enter Content terminated by %>>
    I tried flushing the buffer and using if statements instead of switch, but nothing seems to help. Any idea why this is happening?

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Never fflush stdin.

    That may not be your only problem though. It's impossible to tell with what you have posted at this stage.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mutli Switch statement help
    By elwad in forum C Programming
    Replies: 9
    Last Post: 05-09-2009, 03:19 AM
  2. Switch statement / default:
    By kcpilot in forum C Programming
    Replies: 4
    Last Post: 12-02-2008, 03:14 PM
  3. Getting the switch statement to work.
    By mtymightymike in forum C Programming
    Replies: 7
    Last Post: 10-15-2008, 06:32 PM
  4. Stack operations from switch statement elements
    By mlsrar in forum C Programming
    Replies: 15
    Last Post: 10-02-2008, 01:12 PM
  5. weird scanf()
    By trekker in forum C Programming
    Replies: 1
    Last Post: 06-24-2002, 08:35 AM