#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    1
    Rep Power
    0

    Input few numbers in an integer array


    Hi All,

    I want to input few numbers in an array but don't know pre-hand how many elements would be inputted.

    So the loop should basically run till the user is still inputting.

    Example :- There are 2 arrays A & B.

    Input -
    A - 1,2,3,4,5
    B - 2,2,6,7,9

    So far, I have tried lots of things as follwing but all in vain - >

    While(input = scanf("%d",&A[i]))
    {
    ++i;
    if(input == '\n')
    break;
    }

    Similar loop for array B.

    Then I tried using the scanf("%[\n],&A[i]) as well but didnt get what I needed.

    Kindly help.

    Many Thanks

    Regards
    Milan Singh
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    RTFM! ("Read The Manual!")

    While(input = scanf("%d",&A[i]))

    When you read the documentation for scanf, what does it tell you about the return value of scanf? Will input ever be set to 'n' (110)?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    101
    Rep Power
    0
    Code:
    #include<stdio.h>
    
    int main()
    {
    	int numbers[100];
    	int iterate;
    	int i = iterate = 0;
    	while(scanf("%d",&numbers[i]) == 1 && i < 100)
    	{
    		++i;
    	}
    	for(iterate;iterate < i;iterate++)
    	{
    		printf("%d\n",numbers[iterate]);
    	}
    	getchar();
    	getchar();
    	getchar();
    	return 0;
    }
    Keeps asking the end-user to input numbers until it's a character (not including whitespace)

IMN logo majestic logo threadwatch logo seochat tools logo