# Inputted values into an array

• 04-10-2003
Panther
Inputted values into an array
I'm having a problem trying to convert a set of inputted values (integers) into an array. Ideally, the user is prompted to input a number (imagine inputting a dollar amount or a number using binary notation) and the number is stored in an array with the first number inputted as array[0]. An example would be:
Then, processing will put the first '1' in array[0], '0' in array[1], '0' in array [2], ..., '1' in array[7]. The array size is known and will be size of 8 elements for binary notation or it could be set of a specific dollar amount.
Sticking with the binary example, a user can input each value and then have it stored in an array using printf()/scanf() statements contained in a for loop. However, ideally, the program should take the value inputted for a question and then created the values in an array instead of asking the same question numerous times and increasing the likelyhood of wrong inputted values by the user. Any suggestions will be beneficial, thank you.
• 04-10-2003
Shiro
Why inputting integers? It would be much easier if you let the user input a number as a string and then access the individual characters, convert them to integer and finally store them in the array.

If you still want to use integers, remember that you can use the modulo-operator, %, to get individual digits. Perform modulo N, where N is a multiple of 10 and you can get them.
• 04-10-2003
Panther
Why integers? For odd reasons in my code, the conversion from string to int changes the original value. If it is possible to perform the tasks as described above using strings, then the original question should be edited to try to find out how to convert an initial string value of integers to be stored in an array defined as int type while maintaining the original inputted values. Please help by either pointing/linking me to the right thread or any other resources to solve my problem. Thank you.
• 04-10-2003
Shiro
How does the conversion change the original value?

Conversion of a single character in a string to an integer value can be done as follows:

int_value = character - '0';
• 04-10-2003
stumon
Use getche() in a for loop, ex: array[i] = getche(); while incrementing i in the for loop. The FAQ has something on this with its "getting a number from the user" try reading all those FAQ's they are very helpful. Here is a link
• 04-10-2003
Panther
Quote:

Originally posted by Shiro
If you still want to use integers, remember that you can use the modulo-operator, %, to get individual digits. Perform modulo N, where N is a multiple of 10 and you can get them.
Please explain this concept in more detail. I'll work on the string conversion but I'm just curious about what is quoted. I know you can use % for output but did not know how to implement it for input [scanf()].
• 04-11-2003
Shiro
I hope the following example will make it a little more clear of how to use the modulo-operator in this situation.

Using scanf() you have retrieved an integer N, which you want to store in an array A. Then you could use the following algorithm:

Code:

```// Determine the first digit M = N % 10 // Store the first digit A [0] = M // Prepare N for the next iteration N = (N - M) / 10 // Note: if N is now 0, this means that N is equal to M. And since // M is a single digit, this means N is also a single digit, so then we // have finished the number. // Now repeat this process until you have finished the number```
Example with N = 123.

Calculate -> 123 % 10 = 3
Store -> A [0] = 3
Calculate -> (123 - 3) / 10 = 12

Calculate -> 12 % 10 = 2
Store -> A [1] = 2
Calculate -> (12 - 2) / 10 = 1

Calculate -> 1 % 10 = 1
Store -> A [2] = 1
Calculate -> (1 - 1) / 10 = 0

Now we are finished.