# Thread: Storing user input into integer array

1. ## Storing user input into integer array

In my program, the user is allowed to enter up to 5000 numbers. The numbers they do enter need to be stored into an array. How would I go about reading these numbers into the array? I'm doing the program in different functions, and so far this is what I have.

Code:
```float enterYourNumber(){
double number = 0;
printf("Enter the number. \n");
scanf("%lf", &number);
}//End enterYourNumber```
After the numbers are in the array, I have to print the numbers.

2. Pass the array into the function. ex>) float enterYourNumber(int array[])
Although if you are using an integer array, why are you having the user enter into doubles?

3. Originally Posted by samii1017
Code:
```float enterYourNumber(){
double number = 0;
printf("Enter the number. \n");
scanf("%lf", &number);
}//End enterYourNumber```
That function does not work. It does prompt the user for a number, and it tries to parse a double from the standard input. You do not check whether the scanf() succeeds or fails (it returns the number of successful conversions it did, or EOF if there is no more input or input does not match fixed characters in the pattern). And, you don't do anything with the parsed number, either.

Assume you rewrite your function to a different logic: Read numbers from input, storing them into an array, repeating until there is no more room in the array, no more input, or you encounter a non-numeric input. Your function could be something like
Code:
```int read_numbers(double *const number, const int numbers, FILE *const in)
{
int  n = 0;

/* Read numbers from in, until no more room in array. */
while (n < numbers) {

/* Try reading the next number. */
if (fscanf(in, " %lf", &number[n]) == 1) {
/* A number was read successfully. */
n++;
} else {
/* Not a number, or no more input. Break out of while loop. */
break;
}
}

/* Return the number of numbers read. */
return n;
}```
In your main(), you'd need something along the lines of
Code:
```#define  NUMBERS_MAX  500

int main(void)
{
double  input[NUMBERS_MAX];
int     inputs;

/* some kind of printf() to ask the user for numbers, and to
* end the list with anything non-numeric, like end or = or * or !
* or just Ctrl+Z (in Windows) or Ctrl+D (in Linux/BSD/Mac OS X).
*/