Thread: array as one input...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    51

    array as one input...

    how would you assign values into an array with one single input.

    for example, if the user input 123456789 [enter]

    how would I get 1 into array[0], 2 into array[1] .. and so on...

    so far I have this

    Code:
    for (i=0; i<10; i++)
    {
    printf("Enter value #");
    scanf("&#37;d", &value);
    array[i] = value;
    num++;
    }
    but this takes the input as individual inputs...

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    How do you know what the answer is supposed to be? Why shouldn't it be 12, 34, etc.? If the answer is "they're always single digits", then you need to read one character at a time.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    51
    Quote Originally Posted by tabstop View Post
    How do you know what the answer is supposed to be? Why shouldn't it be 12, 34, etc.? If the answer is "they're always single digits", then you need to read one character at a time.
    digits are 0-9 only, so yea....always single digits


    so there's no way to break up 0123456789 and place each digit into an array?

    there must be

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Yes there is, but you're flirting with disaster: you can store up to 2147483647 in an int, and 4294967295 in an unsigned int, but you can't get 9876543210. What's wrong with reading one character at a time?

    Edit: Yes I know that "most people" can get those numbers, and some can get different ones. Not the point.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    51
    Quote Originally Posted by tabstop View Post
    Yes there is, but you're flirting with disaster: you can store up to 2147483647 in an int, and 4294967295 in an unsigned int, but you can't get 9876543210. What's wrong with reading one character at a time?

    Edit: Yes I know that "most people" can get those numbers, and some can get different ones. Not the point.

    I just browsed through my reference book to confirm the limitation you mentioned.

    how does a program that calculates PI to a million decimal places store the value?
    there has to be a way to store 9,999,999,999.......

    excuse my ignorance, I'm pretty new to this.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    51
    Quote Originally Posted by tabstop View Post
    What's wrong with reading one character at a time?
    from my marking scheme, one character at a time is fine...

    I just think it would seam a lot more sensible to enter it as one digit.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by ktran03 View Post
    I just browsed through my reference book to confirm the limitation you mentioned.

    how does a program that calculates PI to a million decimal places store the value?
    there has to be a way to store 9,999,999,999.......

    excuse my ignorance, I'm pretty new to this.
    Custom data types. And if it's not supposed to be one number, why read it in as one?

  8. #8
    Registered User
    Join Date
    Oct 2008
    Posts
    51
    Quote Originally Posted by tabstop View Post
    Custom data types. And if it's not supposed to be one number, why read it in as one?
    it is suppose to be one number... kind of like a credit card number...


    so there's no easy way of doing this huh?

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I'm still thinking one character at a time.

  10. #10
    Registered User
    Join Date
    Oct 2008
    Posts
    51
    is see it as an inconvenience...

  11. #11
    Registered User lattica's Avatar
    Join Date
    Aug 2008
    Location
    Spacelike Hyperplane
    Posts
    16
    Why not read the number as a string of digits?

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If it's a credit card number, then it is not a "number", it's a string consisting of digits - because you will NEVER use it for calculations ("what's 1000 + your credit card number" isn't one of the sensible questions to ask about a credit card number).

    By the way, I'm pretty sure all credit card numbers are 16 digits, which is decidedly more than a 32-bit integer can handle (about 11 digits). So if that's what you are working with, then a string is the right solution. Note also that to separate 10 digits out of a string requires about 3 clock-cycles per item, a 10 digit integer to be separated out to individual digits will require something in the order of 10-20 cycles per digit on a modern processor, and many more cycles on an older generation processor, so there is no benefit in storing it as a number if you are going to do lots of messing around with individual digits (e.g. validating that it's a valid credit card number by adding the digits together, checking which main credit card organization (mastercard, visa, american express, etc) issued the card by checking the first digit, etc, etc).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. Problem getting the input from a temp variable into the Array
    By hello_moto in forum C++ Programming
    Replies: 3
    Last Post: 03-16-2006, 01:50 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Replies: 2
    Last Post: 03-12-2002, 02:32 PM