Thread: Copying variable to array or splitting input

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    3

    Copying variable to array or splitting input

    okay im having some problems taking a whole number from input say 1234 and spliting it up in to individuality numbers and then storing them in a variable. For example i though i could do something like this
    Code:
    int numberinputbyuser;
    int numberarray [5];
    
    scanf("%d"numberinputbyuser);
     strcpy(numberarray ,numberinputbyuser);
    and maby that will alow me to refrence each part of the numbers inputed from say array space 0 would be 1 and number 1 would be 2 so on but i dont think it works like that. either way some help would be great

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    After you have the number to split up you're supposed to use math to get the digits and assign them into an array.

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    26
    you computers memory doesn't work like that. computers do not understand base 10 number systems. they only understand binary, a base 2 number system. when you declare an int data type and send a number to it the numbers are not stored in individual sequential blocks.

    for instance, if you declared;

    int x[10];

    your telling the computer too set aside 10 blocks of memory with the size of int data type.

    when you declare;

    int y;

    your telling the computer to set aside 1 block in memory with a size of 1 int data type.

    when you send an int data type like 123456 too x[];

    x[0]= 123456

    all of that data is stored in the first element of x[]; which is x[0],
    none of the other blocks of data are altered (x[1] --> x[9]).

    instead of thinking of the numbers as numbers you could treat them as characters. for instance;

    char x[10] = "123456789";
    char y;
    y= x[3];

    in this way y is now equal too '4', that is the character 4, not the number 4. the draw back of this is that you can't easily do math using the data array x[0] because when you use it as an operand your CPU runs the calculation using the byte code of '4' which is not equal to the number 4.

  4. #4
    Registered User
    Join Date
    Jan 2012
    Posts
    2

    Using Functions

    Since the input is an integer, you can use the itoa() function to convert into a string and to store it into an integer array, by using a loop you can use string[i]-'0' to get the individual integer digits. Store them in an int array( in the same loop).

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by BeNNs View Post
    okay im having some problems taking a whole number from input say 1234 and spliting it up in to individuality numbers and then storing them in a variable. For example i though i could do something like this
    Code:
    int numberinputbyuser;
    int numberarray [5];
    
    scanf("%d"numberinputbyuser);
     strcpy(numberarray ,numberinputbyuser);
    and maby that will alow me to refrence each part of the numbers inputed from say array space 0 would be 1 and number 1 would be 2 so on but i dont think it works like that. either way some help would be great
    Code:
    char x[10];
    int y;
    
    scanf(" %9s", x);
    
    printf("The digits are...");
    for (y = 0; y < strlen(x); y++)
      { x[y] -= '0';
       printf("  %d", x[y]); }
    
    printf("\n\nthe third digit is %d", x[3]);
    If you know they are all single digit numbers, you can convert them from ascii and use them as actual numbers in your program.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 11-06-2011, 11:38 AM
  2. Taking a variable and input each value of it into an array
    By jammysammy in forum C Programming
    Replies: 4
    Last Post: 05-17-2011, 04:46 AM
  3. Replies: 5
    Last Post: 02-25-2008, 06:35 AM
  4. Copying a file from a variable?
    By civix in forum C++ Programming
    Replies: 16
    Last Post: 01-27-2008, 02:21 PM
  5. 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