Thread: random number popups....

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    20

    random number popups....

    while i was bored and randomly typing code in and compiling it for no reason, this caught my attention:
    Code:
    #include <stdio.h>
    
    int main()
    {
        int c;
        
        while((c = getchar()) != EOF)
              printf("%d\n", (c = (c - '0') + 2));
    }
    shouldnt this equal the original number added by 2? well it does it show the result but it always has a -36 after it on the next line. i have no idea why this happens , i even tried it in different ways like:

    c = c + 2;
    printf("%d", c);

    can somebody please explain this?

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
        while((c = getchar()) != EOF)
    Search the forum for an answer about why this is not a proper way to control an input loop.
    HINT: you print EOF

    Kurt

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Search the forum for an answer about why this is not a proper way to control an input loop.
    Seems OK to me - you're thinking of feof()

    > HINT: you print EOF
    Mmm, I don't think so.

    > but it always has a -36 after it
    '\n' is 10
    '0' is 48
    2 is 2
    Can you make -36 yet?
    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.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by Salem
    Seems OK to me - you're thinking of feof()
    Guess I did. Sorry.
    Kurt

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    avoid this by the following code

    Code:
    #include <stdio.h>
    
    int main()
    {
        int c;
        
        while((c = getchar()) != EOF)
        {
              printf("%d\n", (c = (c - '0') + 2));
              while((c = getchar())!= '\n' && c != EOF);
        }    
    }
    /*myouptut
    a
    51
    s
    69
    d
    54
    f
    56
    g
    57
    h
    58
    j
    60
    k
    61
    */
    ssharish2005

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Avoid what with that code? You write the oddest "fixes" I have ever seen.


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

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > avoid this by the following code
    Ah, you mean
    Code:
    while((c = getchar()) != EOF && c != '\n' )
    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.

  8. #8
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    well, my intention here was to clear the input buffer when it enters the loop back again so that it wont agian read the '\n' char which was left in the input buffer. which actually avoids the printing the -36

    and i am sorry i havn't included return value in the main. that was my bad

    ssharish2005

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Your bad was swallowing the entire input line after the first character, thus only printing the first line of each character.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #10
    Registered User
    Join Date
    Jul 2006
    Posts
    20
    i know if i dont use eof as a condition then it works and it doesnt count the '\n'. but how can i make the thing loop and not just terminate only after 1 input.

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well for starters, you could take the time to actually explain what it is you're trying to do. See, something like this:

    "I want to read a line of text, and display each character's decimal, hex, and character representation on a seperate line. I want to repeat prompting for and display output until a blank line or EOF is entered."

    Or whatever it is you actually are trying to do. I hate having to teach people how to think or ask a simple question. Go read your two posts. They are in no way related, and your second post isn't even a complete thought.


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

  12. #12
    Registered User
    Join Date
    Jul 2006
    Posts
    20
    ok heres exactly what im trying to do. adding 2 to a number coming from input. i want this thing to loop so it will always add 2 to the input number and i wanna know how to stop the -36 from always showing up after the result.

  13. #13
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    There are other, better ways to do it. What if the input doesn't even happen to be a number? Try this:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
      char buf[BUFSIZ];
      int num;
    
      while(fgets(buf, sizeof(buf), stdin))
      {
        if(sscanf(buf, "%d", &num) == 1)
          printf("%d\n", num + 2);
        else
          fputs(buf, stdout);
      }
    
      return 0;
    }
    That will add 2 to any input number, but if the input isn't a number it'll just regurgitate what you typed.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Random number in range generation.
    By hebali in forum C Programming
    Replies: 19
    Last Post: 03-04-2008, 10:46 AM
  2. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 12:56 PM
  3. How exactly does the random number generator work?
    By Finchie_88 in forum C++ Programming
    Replies: 6
    Last Post: 08-24-2007, 12:46 AM
  4. random number between negative and positive number
    By anomaly in forum C++ Programming
    Replies: 6
    Last Post: 12-06-2003, 08:40 AM
  5. Random Number Generator
    By Ikurik in forum C++ Programming
    Replies: 16
    Last Post: 08-17-2003, 07:34 PM