Extracting Integers From a Single Variable

This is a discussion on Extracting Integers From a Single Variable within the C Programming forums, part of the General Programming Boards category; I'm currently working on an example in the book C: How to Program that I can't seem to figure out ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    9

    Extracting Integers From a Single Variable

    I'm currently working on an example in the book C: How to Program that I can't seem to figure out on my own

    I need to write a program that will let me input, say, a 5-digit number, and then print each digit of the number separated by 3 spaces. I could make it work if each digit was stored in it's own variable, but that would require the user to enter each digit individually, and it seems they want me to do it using a single number/variable.

    All the answers I've found so far for this problem require either strings, arrays, or something of that nature, but I get the impression the book wants me to only use what was learned in chapter 2, so that means no arrays or strings, just basic printf, scanf, and maybe a solo if statement, so there must be a very simple way to do this that I'm overlooking?

    Any help would be appreciated!

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    use / and % operators, search forum - this was discussed several times
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    For integers, x = x / 10 will eventually reach 0 if you do it enough.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    9
    Thanks for the replies, I searched on vart's advice of the % operator and found this thread, with the answer from wise_ron near the bottom

    6 digit number

    Thanks for your help!

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,803
    Quote Originally Posted by parx86 View Post
    I could make it work if each digit was stored in it's own variable, but that would require the user to enter each digit individually
    Nitpicking (don't take too seriously): They will always enter each digit individually... unless you can magically type something like 12345 with a single keypress... maybe a keyboard with a numpad with 100000 individual keys for each value from 0 to 99999.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    > I searched on vart's advice of the % operator and found this thread
    So did you actually learn anything, other than persistent searching may find you the answer?
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assignment HELP!!
    By cprogrammer22 in forum C Programming
    Replies: 35
    Last Post: 01-24-2009, 01:24 PM
  2. Replies: 15
    Last Post: 11-12-2008, 05:12 PM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 01:45 PM
  5. fancy strcpy
    By heat511 in forum C++ Programming
    Replies: 34
    Last Post: 05-01-2002, 04:29 PM

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