Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 219
  • Last Modified:

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.


      
0
larockd
Asked:
larockd
1 Solution
 
ozoCommented:
You may want to delete one of your two identical questions Q.10081008 and Q.10081009
It may also help to tell us in what way it did not work as you expected.
0
 
rayray120497Commented:
put a space before %d in the scanf statement, such as:
scanf( " %d", &pChainWheelSizes[iLoopVariable] );
the white space can be blank, tab, newline, carriage return, vertical tab, and formfeed, so not matter how you enter those three number, the program can get them all.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now