scanf question

This is a discussion on scanf question within the C++ Programming forums, part of the General Programming Boards category; I have the following problem int day; int year; char month[12][10]; printf("Enter date (e.g. January 1 2008)"\n"); scanf("s% d% d% ...

  1. #1
    MMu
    MMu is offline
    Registered User
    Join Date
    Mar 2008
    Posts
    4

    Question scanf question

    I have the following problem


    int day;
    int year;
    char month[12][10];

    printf("Enter date (e.g. January 1 2008)"\n");
    scanf("s% d% d% \n",month, &day, &year);


    Now if I enter date with a typo, say January 3 19ww,

    "year" return a value of 19 and it ignor the typo ww.
    On the other hand if I type year, say w990, it returns a garbage value
    as (soemthing like -87293775) expected.

    But if make the same type of typo for the day, it always return a garbage
    value.

    My question is why only 19ww return a value 19 and ignore the characters?
    And how can I make 19ww return a garbage value so that I can catch it
    though a check such as year > 0 || year < 2500 .

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >scanf("s% d% d% \n",month, &day, &year);
    Since you're talking about a runtime issue, I'll assume that the massive errors in this call are simply typos in the post and not in your code.

    >My question is why only 19ww return a value 19 and ignore the characters?
    That's how scanf works. It reads characters until it finds characters that aren't valid for the conversion specifier. Since w clearly isn't part of any valid integer value, scanf stops reading on it.

    >And how can I make 19ww return a garbage value so that I can catch it
    Well, the only real problem is with the year and trailing garbage characters. Otherwise scanf will fail and return less than 3 so that you know the date was invalid. If the date is supposed to be on a line by itself, you can check the next character for a newline, and if it's not a newline, assume that the date is invalid:
    Code:
    if ( scanf ( "%s%d%d", month, &day, &year ) == 3 ) {
      if ( getchar() == '\n' ) {
        /* Good date */
      }
      else {
        /* Invalid date */
      }
      
    }
    else {
      /* Invalid date */
    }
    My best code is written with the delete key.

  3. #3
    MMu
    MMu is offline
    Registered User
    Join Date
    Mar 2008
    Posts
    4

    Thumbs up

    Thanks and sorry about &#37;s reversal
    Last edited by MMu; 03-19-2008 at 10:18 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 05-25-2006, 12:51 PM
  2. Exam Question - Possible Mistake?
    By Richie T in forum C++ Programming
    Replies: 15
    Last Post: 05-08-2006, 03:44 PM
  3. Scanf Question
    By shiyu in forum C Programming
    Replies: 4
    Last Post: 01-31-2003, 07:48 AM
  4. Simple question with scanf()
    By MadStrum! in forum C Programming
    Replies: 3
    Last Post: 01-20-2003, 09:41 AM
  5. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 02:57 PM

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