Link to home
Start Free TrialLog in
Avatar of larockd
larockd

asked on

Reading In A Variable Amount Of Interger Input

How can I read in a different amount of integers based on a users input.

Enter Amount of Integers : 3

I Then allocated memory with malloc to hold 3 integers.  Now I want to enter in three integers.  How can I do this with scanf or is therea better way.

I tried using a for loop with the scanf, but it did not work as expected.
      printf("\nPlease Provide %d chainwheel sizes in ascending order: ", iNumberChainWheels );

for ( iLoopVariable = 0 ; iLoopVariable <  iNumberChainWheels; iLoopVariable++ )
{
scanf("%d",&pChainWheelSizes[iLoopVariable]) ;
}

The above code did call scanf three times, but what if the user entered all tree integers on the same line...  Any thoughts.


      
Avatar of ozo
ozo
Flag of United States of America image

What was unexpected about what it did when the user entered all tree integers on the same line?
ASKER CERTIFIED SOLUTION
Avatar of mlaiosa
mlaiosa

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of larockd
larockd

ASKER

Thank you for taking your time to answer this.  Everything worked perfect. I also appreciate the explanation on the buffering.,.