Thread: Getting an integer with only using getchar()

  1. #1
    Registered User
    Join Date
    Nov 2013
    Posts
    2

    Getting an integer with only using getchar()

    I need to get an integer number with only using getchar() and without pre knowing the number of digits in the integer.
    And I cannot use strings or arrays.

    Any ideas?

    Thank you

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Read in a character. if it's a digit, process it; if it's not, you're done.

    Presumably you know how, if you already have 6, and then you see a 4, how to (numerically, not in terms of strings) turn that into 64, and then turn that into 644 when another 4 comes in, etc. (If not, look up "place value".)

  3. #3
    Registered User
    Join Date
    Nov 2013
    Posts
    2
    Thank you tabstop

    I don't know how I have looked for "place value", yield nothing.

    could you please direct me to a link or somethin.

  4. #4

  5. #5
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    I'm going to enter the number 1024 - break down the problem...

    1 Value entered so far is 1
    0 Value entered so far is 10
    2 Value entered so far is 102
    4 Value entered so far is 1024

    Have a look at the number entered so far - There is a algorithm for adding a new digit. Ask yourself what is happening to the number each time a new digit is entered.

    Every time a new character is entered, you need to valadate it (make sure it is a number), and then enter it into your algorithm for adding a new digit.
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getchar() help
    By Zachary May in forum C Programming
    Replies: 3
    Last Post: 02-27-2013, 09:38 AM
  2. Storing an integer in an array using getchar
    By Interista in forum C Programming
    Replies: 9
    Last Post: 11-04-2011, 12:37 PM
  3. New program to read three numbered integer using getchar
    By Interista in forum C Programming
    Replies: 3
    Last Post: 11-03-2011, 01:45 PM
  4. Replies: 5
    Last Post: 06-12-2007, 02:18 PM
  5. determining if an integer is a legal integer
    By LiquidBlaze33 in forum C++ Programming
    Replies: 2
    Last Post: 10-09-2005, 07:06 PM