question on visual c++ (C) 6.0 - help

This is a discussion on question on visual c++ (C) 6.0 - help within the C Programming forums, part of the General Programming Boards category; Consider the simple following example: do { scanf("%d", &number); if ((number != 1) && (number != 2) && (number != ...

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    7

    question on visual c++ (C) 6.0 - help

    Consider the simple following example:

    do
    {
    scanf("%d", &number);

    if ((number != 1) && (number != 2) && (number != 3) && (number != 4) && (number != 5))
    printf("Your number is out of range\n ");

    } while ((number != 1) && (number != 2) && (number != 3) && (number != 4) && (number != 5));

    - ommit the syntax mistakes --

    Basically you are validating the input of a number which is correct if in range 1 to 5 (inclusive). When a number such as -12 or 15 is entered, an error message is printed and loop repeats.

    The problem is when you input a character (e.g. a) - visual c++ goes into an endless loop and never stops!

    Does anybody know how to correct this - simplest answer please

    It works if number is declared as char, scanf changed to scanf %c and ' ' single quotation marks are used around the numbers.

    Is there any other way ? - if u know sort me out

    This is a very weird case and it only happeneds to me in visual c++ 6.0

    Any suggestions ?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >This is a very weird case and it only happeneds to me in visual c++ 6.0
    This is a common and predictable problem with scanf. The reason it occurs is because if scanf fails to convert input it leaves it in the buffer. The next iteration of the loop reads the same invalid input and an infinite loop is created. The best solution is to avoid scanf, but you asked for the simplest, so you can write a function to clean up the mess in the input stream. It's also a good practice to check the return value of scanf just to be safe:
    Code:
    #include <stdio.h>
    
    static void discard ( FILE *in )
    {
      int c;
    
      while ( ( c = fgetc ( in ) ) != '\n' && c != EOF )
        ;
    }
    
    int main ( void )
    {
      int number;
    
      do {
        if ( scanf ( "%d", &number ) != 1 || number < 1 || 5 < number ) {
          /* Clean up */
          discard ( stdin );
          printf ( "Your number is out of range\n" );
        }
        
      } while ( number < 1 || 5 < number );
    
      printf ( "Your number was %d\n", number );
    
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. more then 100errors in header
    By hallo007 in forum Windows Programming
    Replies: 20
    Last Post: 05-13-2007, 09:26 AM
  2. Visual C++ 6.0
    By pythonusr in forum Windows Programming
    Replies: 10
    Last Post: 04-26-2007, 03:13 AM
  3. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 11:52 PM
  4. Problem with Visual C++ 6.0
    By toonlover in forum C++ Programming
    Replies: 4
    Last Post: 05-25-2005, 10:32 PM
  5. Microsoft Visual C++ 6.0 ** Am I missing something?
    By Intimd8r in forum C Programming
    Replies: 9
    Last Post: 05-15-2002, 04:21 AM

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