Thread: Storing input into an array until not a number is entered

  1. #1
    Registered User
    Join Date
    Sep 2018
    Posts
    15

    Storing input into an array until not a number is entered

    I have an assignment that asks a user to input at most 10 integers to be stored into an array. The code i have now, i believe wants exactly 10 integers.
    How can i manipulate the input of the array (scanf) to stop inputting and move on after only 4 inputs. IE: 1 2 3 4. then move on to next array.

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    
    int main(void)
    {
    int i = 0, A[10];
    
    
    printf("Input a set of up to 10 integers separated by a space to store in Array A: ");
    fflush(stdout);
    
    
    for(i = 0; i<10; i++)
    {
            if((scanf("%d"), &A[i]) >= 0)
            {
                scanf("%d", &A[i]);
                printf( "%d ", A[i]);
            }
            else
            {
                break;
            }
    }
    
    
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2018
    Posts
    15
    Have this set up, how can i only take in four integers instead of all ten from the user?

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    int main(void)
    {
    int i = 0, A[10];
    
    printf("Input a set of up to 10 integers separated by a space to store in Array A: ");
    fflush(stdout);
    
    for(i = 0; i<10; i++)
    {
        scanf("%d", &A[i]);
    }
        
    for(int y = 0; y<10; y++)
    {
        printf("%d ", A[y]);
    }
    
    return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. storing number to array
    By vead in forum C Programming
    Replies: 7
    Last Post: 02-07-2018, 02:01 PM
  2. Storing input to Character Array
    By MagicWand in forum C++ Programming
    Replies: 6
    Last Post: 04-23-2016, 03:58 PM
  3. storing random number into array
    By alien1 in forum C Programming
    Replies: 2
    Last Post: 12-16-2014, 06:36 PM
  4. Storing input in dynamic array
    By samtheearlier in forum C Programming
    Replies: 5
    Last Post: 01-21-2013, 03:33 AM
  5. Storing a number in an array
    By C++ student in forum C++ Programming
    Replies: 4
    Last Post: 09-28-2009, 08:47 AM

Tags for this Thread