Reading in single characters

This is a discussion on Reading in single characters within the C++ Programming forums, part of the General Programming Boards category; So I'm working on a program to compute the first positive integer with a numerical persistence of 3 (persistence being ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    1

    Reading in single characters

    So I'm working on a program to compute the first positive integer with a numerical persistence of 3 (persistence being the number of times the digits of a number are multiplied together until the product is a single digit ex: 614 -> 6*1*4=24 ->2*4=8, where 8 is the desired single-digit product and the persistence is 2).

    The instructions ask for there to be no explicit input, so I assume that it means the program will generate the numbers to evaluate the persistence of (generated by a counter I'm guessing).

    What I'm stuck on is how to read the integers created by the counter into my program. When the counter=1, it should be able to read in "1". When the counter gets to 614, it should read in "6" "1" "4", and so on for all the numbers. So far I have only used external data files to read information into programs, and have never read in single digits like this.

    Wondering if you guys have any tips on reading in.


    My first guesses were to use something like cin.get(integer), but since all the numbers are created in the program... not really what I need.

    Thanks for any help
    Pallum

  2. #2
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    435
    Since an integer will be passed to you, you don't need to "read in" anything. What you need to do is break up the number you're given into its individual digits.

    You can do this using division and modulus. For example,
    614%10==4
    614/10%10==1
    614/100%10==6
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. reading multiple strings from single row....
    By satty in forum C Programming
    Replies: 13
    Last Post: 08-06-2010, 07:39 AM
  3. multiple characters within single quotes
    By BattlePanic in forum C Programming
    Replies: 4
    Last Post: 05-06-2008, 02:59 AM
  4. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  5. Reading files with an unkown amount of characters
    By Zahl in forum C++ Programming
    Replies: 13
    Last Post: 10-10-2002, 02:04 PM

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