Thread: nested if-else forgot how to clear scanf

  1. #1
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350

    nested if-else forgot how to clear scanf

    Can someone please help me jog my memory? It's been a long time since I used C, but I had this problem once before. I figured it out then, but I've forgotten what I did to do so and now it's driving me crazy. Anyway, my problem is when using the same variable in a nested if else.

    char choice;

    blah

    if(sentinel == -1)
    {
    blah
    scanf("%c", &choice);

    if(choice != 'y')
    {
    blah
    }

    here is the stickler

    else
    {
    blah
    scanf("%c", &choice);

    switch(choice)
    {
    blah
    }

    All of this is inside the first if statment. My code does not do what I intended I guess because either choice still is clinging onto its first value or scanf has null in it or something. It must be scanf since the same thing happens even if I use another variable in the second scanf. Any ideas are greatly appreciated.

    TIA
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Here's a novel ideal! Don't use scanf. Use fgetc or something similar.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    Ok I'll look into that too, but I found it,

    _flushall()

    Thanks for the reply quzah.
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The thing about fflushall is that it isn't an ANSI function. This may be no big deal to you, in which case, go ahead and use it.

    Another option is:

    void flushstdin( void ) { while( fgetc( stdin ) != '\n' ); }

    Basicly you then use:

    choice = fgetc( stdin );
    fflushstdin( );

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    Well I'm using an old version of Microsoft C / CPP for dos 1992 ish. fgetc in that version is used for file i/o, which I'll be working with later in the program. Right now I'm just writing a few inline assembler functions for clearing the screen and cursor control. That is why I was using printf instead of cout and that scanf stuff. Cout wouldn't allow me to set the cursor but once. I might need to check into that. : )

    Thanks for this new tip!! I'll paste it into a text file.
    Last edited by ronin; 06-25-2002 at 09:55 PM.
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scanf skips lines of code in DOS using Dev-C++ and Windows XP
    By jenovanomusuko in forum C Programming
    Replies: 9
    Last Post: 12-21-2008, 03:10 AM
  2. Help with a basic scanf procedure.
    By killpoppop in forum C Programming
    Replies: 9
    Last Post: 11-03-2008, 04:39 PM
  3. MFC: Clear Client Area
    By mrafcho001 in forum Windows Programming
    Replies: 2
    Last Post: 06-27-2005, 01:35 PM
  4. scanf - data is "put back" - screws up next scanf
    By voltson in forum C Programming
    Replies: 10
    Last Post: 10-14-2002, 04:34 AM
  5. Quzah's printf and scanf tutorial...
    By quzah in forum C Programming
    Replies: 2
    Last Post: 03-20-2002, 03:59 AM