Thread: scanf issue

  1. #16
    Registered User
    Join Date
    Feb 2019
    Posts
    500
    Another piece of information for @cooper1200. Keep in mind that reading from stdin (via scanf) isn't the same as reading directly from keyboard (which is more difficult to do). You are reading a "file" through a FILE pointer called stdin. Take a look at the code below:

    Code:
    #include <stdio.h>
    
    int main( void )
    {
      char c;
      int count = 3;
    
      puts( "Trying to read Y or N 3 times..." );
    
      while ( count-- )
      {
        // this (or printf, puts, putchar) writes
        // to stdout file stream.
        fputs( "Type Y or N. ", stdout ); 
    
        // stdout is 'line buffered'. It means that
        // it can retain the chars until a '\n' is found.
        // flushing the buffer garantees all chars will
        // be dumped. The previous fputs didn't write
        // the final \n, hence the flushing.
        fflush( stdout );
    
        // scanf reads data from stdin file stream.
        // Note the space before %c will ignore all "spaces"
        // as defined by isspace macro on ctype.h
        scanf( " %c", &c );
        switch (c)
        {
          case 'Y':
          case 'y':
            puts( "\nYes!" ); break;
          case 'N':
          case 'n':
            puts( "\nNo!" ); break;
          default:
            printf( "\nFound '\\x%hhx' char. Hey! Y or N, remember?\n", c );
        }
      }
    }
    Here are three ways to execute this code:
    $ ./test
    Trying to read Y or N 3 times...
    Type Y or N. y

    Yes!
    Type Y or N. x

    Found '\x78' char. Hey! Y or N, remember?
    Type Y or N. n

    No!

    $ echo -e " y x \r\n\t\v\f n" | ./test
    Trying to read Y or N 3 times...
    Type Y or N.
    Yes!
    Type Y or N.
    Found '\x78' char. Hey! Y or N, remember?
    Type Y or N.
    No!

    $ echo -e " y x \r\n\t\v\f n" > ./input.txt
    $ ./test < input.txt
    Trying to read Y or N 3 times...
    Type Y or N.
    Yes!
    Type Y or N.
    Found '\x78' char. Hey! Y or N, remember?
    Type Y or N.
    No!
    When you don't provide data for stdin (through pipe or redirection), functions as scanf() will wait for data coming from keyboard...
    So, thinking abour stdin, stdout and stderr as "files" is useful...

    PS: And yes... ignoring "spaces" before is better than after, in this case...

  2. #17
    Registered User
    Join Date
    Apr 2019
    Posts
    662
    thanks all for your help much appreciated I wound that "scanf(" %c"&yn) worked but will experiment with all the other solutions.
    many thanks
    coop

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Scanf Issue with Segmented input(?)
    By capitalCORN in forum C Programming
    Replies: 4
    Last Post: 10-22-2014, 02:07 PM
  2. scanf issue - character in buffer
    By demps in forum C Programming
    Replies: 2
    Last Post: 01-19-2010, 07:51 PM
  3. Issue with scanf and char
    By Covalent in forum C Programming
    Replies: 6
    Last Post: 11-03-2008, 12:06 AM
  4. 'cin' and 'scanf()' issue
    By Brain Cell in forum C++ Programming
    Replies: 4
    Last Post: 11-07-2004, 10:53 AM
  5. scanf issue
    By fkheng in forum C Programming
    Replies: 6
    Last Post: 06-20-2003, 07:28 AM

Tags for this Thread