4

I am trying to use scanf to capture a string of numbers and convert it into a corresponding array. For example, a user would enter in 1234, then enter, and the following would be set:

array[0]=1
array[1]=2
array[2]=3
array[3]=4

Here is my code so far:

    void user_input()
{
  int cardarray[16];
  int i;
  char number;
  printf("Enter in the number:");
  for (i=0; i<16; i++)
{
  scanf("%c", &number);
  number = cardarray[i] - '0';
    }


  printf("The number is %d\n", /*some value*/);
}

I know you have to read characters and convert each into an integer digit, but I'm not exactly sure how.

3
  • Your code seems to accomplish what you want it to do. You did read characters and you did convert them each into an integer digit. Commented Feb 2, 2012 at 5:28
  • Is there a specific reason why you're storing the digits of a number as individual chars as opposed to using an int and the %d format specifier? Commented Feb 2, 2012 at 5:31
  • always check the result of scanf, other wise you may end up dealing with indeterminate data. Commented Feb 2, 2012 at 5:43

4 Answers 4

2

Should the line

number = cardarray[i] - '0'; 

read

cardarray[i] = number - '0'; 

Then does as David says to compute the answer

Sign up to request clarification or add additional context in comments.

Comments

0

You're looking for atoi()

cardarray[i] = aoti(number);

http://www.codingunit.com/c-reference-stdlib-h-function-atoi-convert-a-string-to-an-integer

That being said, the method you're using which is to subtract the charset value of the character 0 will also work fine if you assign to the right variable:

cardarray[i] = number - '0';

1 Comment

atoi is not bad, but strtol is more useful especially if you are not sure what your input contains.
0

I'm guessing you want:

printf("The number is %d\n",
   cardarray[0]*1000 + cardarray[1]*100 + cardarray[2]*10 + carrarray[3]);

You can also use:

printf("The number is %d%d%d%d\n",
    cardarray[0], cardarray[1], cardarray[2], cardarray[3]);

1 Comment

I apologize for it being unclear. I would like to scan in the number into an array, and then use printf to display the number in sequence once again using the values populated from that array.
0

Here is some code may be helpful:

#include <stdio.h>
int main(void)
{
    int i = 0;
    /* firstly, capture the input */
    scanf("%d", &i);

    /* secondly , extract for each number:
       1234 -> 4,3,2,1
    */
    int ia[256] = {0};
    int len = 0;
    while(i){
        ia[len++] = i%10;
        i /= 10;
    }

     /* thirdly, reverse the array */
     int j = 0;
     while(j < len-j-1){
         int t = ia[j];
         ia[j] = ia[len-j-1];
         ia[len-j-1] = t;
         j++;
     }

     /*let's see if it works */
     for (j=0; j < len; j++){
        printf("%d ", ia[j]);
        }
     putchar('\n');

     return 0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.