Thread: Array question

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

    Array question

    Hi! I am trying to read in a 16 digit number into an array and at the same time check and make sure it is in fact 16 digits(display an error message if it is any more or any less)

    I know how to read it in as an integer and check the digits from there, but I don't know how to convert it into a array from there.

    Thanks!

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Code:
    if((number < smallest 16 digit number) ||( number > largest 16 digit number))
      printf("\n Not a 16 digit number, try again\n");
    That should work for a check. What's the problem with the array -- more details?

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    3
    It's not really a problem, I'm just not quite sure how to do it.

    Essentially I want to read in a number, say: 1234567890123456
    First, I want to check the number is 16 digits long.
    Then, I need it to be stored as an array, so stored in the array are the numbers: 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You could just read it as a string and then check the string length.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Ok, it's base 10, so:
    Code:
    index assign 15
    while number16 greater than 0
      assign n number16 % 10
      (which gives n the right most digit from number16)
      now divide number16 by 10, to remove that digit, and 
      assign array[index] to the value of n
      decrement index
    end loop
    should get it into array[0]-[15].

  6. #6
    Registered User
    Join Date
    Nov 2010
    Posts
    3
    Ok, I'll see how that works.

    Thanks both of you!

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Good luck, you're welcome, and

    Welcome to the forum, DarkTemplar!

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    By the way, if you want to use Adak's suggestion, you should use long long or some other 64 bit integer type. Frankly, I think it is just easier to bypass that and read as a string.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array Question
    By Alecroy in forum C++ Programming
    Replies: 4
    Last Post: 09-26-2010, 03:22 PM
  2. Dynamic Mutli dimensional Array question.
    By fatdunky in forum C Programming
    Replies: 6
    Last Post: 02-22-2006, 07:07 PM
  3. array question?
    By correlcj in forum C++ Programming
    Replies: 1
    Last Post: 11-08-2002, 06:27 PM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM