Thread: Small problem with century

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    9

    Small problem with century

    Code:
    #include <stdio.h>
    #include <math.h>
    
    void exercise (void);
    
    int century (int y)
    {
        int centur = y / 100;
        y &= 100;
        if (y > 0)
            y = centur + 1;
        else
            y = centur;
        return y;
    }
    
    void exercise (void)
    {
      int y;
      while (scanf ("%d", &y) != EOF)
      {
        int z = century (y);
        printf ("%d\n", z);
      }
    }
    
    
    int main (void)
    {
      exercise ();
      return 0;
    }
    Input:
    1900
    out:
    19 (i need 18)

    Input:
    1901
    Out:
    19

    i also tested with
    Code:
    if (y > 0)
            y = centur - 1;
    and nope... wrong

    note:
    i cannot change the "void exercise (void)" and main funtion

  2. #2
    Registered User
    Join Date
    Sep 2013
    Posts
    9
    error:
    Code:
    y &= 100;
    correct:
    Code:
    y %= 100;
    5 and 6 are very close to each other


    //lock or delete thread

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    while (scanf ("%d", &y) != EOF)
    should be

    Code:
    while (scanf ("%d", &y) == 1)
    if user enters something not parsable as decimal integer - for example "abcd" scanf will stop parsing and returns 0
    Next call to scanf will encounter same input still present in the input stream and fail again with the same result - so you'll get the infinite loop
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 21st Century C -- new book just arrived
    By Zobeid in forum C Programming
    Replies: 1
    Last Post: 11-09-2012, 04:05 PM
  2. Need help with a small problem
    By Freddy92 in forum C++ Programming
    Replies: 5
    Last Post: 12-11-2010, 04:03 PM
  3. A small problem with a small program
    By Wetling in forum C Programming
    Replies: 7
    Last Post: 03-25-2002, 09:45 PM
  4. I have a small problem...
    By SyntaxBubble in forum Windows Programming
    Replies: 0
    Last Post: 12-01-2001, 04:05 PM
  5. Greatest Author of the 20th Century
    By EvenFlow in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 10-13-2001, 07:55 AM