Thread: Storing user input into integer array

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    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. #2
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    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. #3
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    Quote Originally Posted by samii1017 View Post
    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).
        */
    
        inputs = read_numbers(input, NUMBERS_MAX, stdin);
    
        /* you now have 'inputs' numbers in 'input',
         * from input[0] to input[inputs-1].
         * Use some kind of a loop -- you'll need a new int
         * variable in the loop -- to print them out.
        */
    
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Storing user inputs into array and splitting into elements.
    By Lina_inverse in forum C Programming
    Replies: 4
    Last Post: 10-05-2012, 11:39 PM
  2. Storing an integer in an array using getchar
    By Interista in forum C Programming
    Replies: 9
    Last Post: 11-04-2011, 12:37 PM
  3. Ensuring user input is integer value (newbie question)
    By melodia in forum C Programming
    Replies: 6
    Last Post: 11-21-2010, 06:43 AM
  4. how to allow user to input only integer?
    By errtime in forum C++ Programming
    Replies: 1
    Last Post: 09-21-2007, 08:56 AM
  5. Validate a user input integer?
    By criticalerror in forum C++ Programming
    Replies: 20
    Last Post: 12-07-2003, 08:30 PM