Thread: Porblem with Scanf

  1. #1
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356

    Porblem with Scanf

    Code:
     #include <stdio.h>
    
    int main( void ) 
    {
        int Ivar;
        
        
        printf( "Enter:" );
        scanf( "%c", &Ivar ) ;
        
        while ( Ivar !=  EOF )
        {    
             
          printf( "\nValue == %d\n", Ivar );
          printf ( "ANSI = %d, Char = %c", Ivar, Ivar );
          
          printf( "\nEnter:" );
          scanf(  "%c" , &Ivar );
         
       }       
        
        system( "pause" );
        return 0;
        
    }
    The output of the Code is
    Enter:s

    Value == 115
    ANSI = 115, Char = s
    Enter:
    Value == 10
    ANSI = 10, Char =

    Enter:

    This is what it prints out . The First sanf works fine . It does what I want it to do . it disoklays the ANSI Value of the Character I typed in . But the scanf in the loop executes without halting for an Input ?

    I would like to know why and how is this problem occurring . Is this a Inheret C problem ?

    Thanks
    "I wish i could wish my wishs away"

    "By indirections find directions out" -- William Shakespears

    "Do what thou wilt shall be the whole of the law" -- Crowley "THE BEAST 666"

    Mizra -> love = Death...
    RDB(Rocks yooo)..

    http://www.cbeginnersunited.com

    Are you ready for the Trix ???

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    When you typed in "s", the string is actually read like this (minus any '\0' chars):

    Code:
    's', '\n'
    So when you asked for a char in the first scanf() it read the 's', but it left the '\n' alone inside a special buffer.

    When the second scanf() executed, this is what the buffer looked like:

    Code:
    '\n'
    So it tries to read from the buffer. There's already a '\n' there, so instead of asking you for more input, it just takes it as a character.

    To work around this, you could just read an extra char and it will read off the '\n' char and then read another one from you.

    In reality, scanf() does not handle input as gracefully as it could.

  3. #3
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356
    Yes . Now I included a a while loop that would throw away all the buffered data .

    while( getchar() != '\n' ) ;

    Here is my new code
    Code:
    #include <stdio.h>
    
    int main( void ) 
    {
        int Ivar= 0;
        int Ivar1 ;
        char ch;
        
        printf( "Enter:" );
        
        
        while ( scanf(  "%c" , &Ivar ) != EOF )
        {    
          printf ( "ANSI = %d, Char = %c\n", Ivar, Ivar );
          printf( "Enter:" );
          
          while ( getchar() != '\n' ) ;
                     
       }       
        
        system( "pause" );
        return 0;
        
    }
    Now it isnt Executing unwated scanf's without my knowing but it dosnt End the Loop on EOF .

    In reality, scanf() does not handle input as gracefully as it could.
    Isnt there a compiler that knows how to handle scanf properly and not allow such things to happen ?
    "I wish i could wish my wishs away"

    "By indirections find directions out" -- William Shakespears

    "Do what thou wilt shall be the whole of the law" -- Crowley "THE BEAST 666"

    Mizra -> love = Death...
    RDB(Rocks yooo)..

    http://www.cbeginnersunited.com

    Are you ready for the Trix ???

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    scanf is fine. It's you who doesn't know how to use it. The f in scanf means it wants its data formatted in a precise way. If your data isn't formatted that way, it's your fault. If you don't like how it works, use a different function for input.


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

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,685
    > while ( scanf( "&#37;c" , &Ivar ) != EOF )
    The format and the variable do not match.

    > but it dosnt End the Loop on EOF .
    There are two places where the input is read, and only one of them checks for EOF.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with a basic scanf procedure.
    By killpoppop in forum C Programming
    Replies: 9
    Last Post: 11-03-2008, 04:39 PM
  2. Replies: 2
    Last Post: 02-20-2005, 01:48 PM
  3. scanf issue
    By fkheng in forum C Programming
    Replies: 6
    Last Post: 06-20-2003, 07:28 AM
  4. Scanf and integer...
    By penny in forum C Programming
    Replies: 3
    Last Post: 04-24-2003, 06:36 AM
  5. 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