Solved

Reading In A Variable Amount Of Interger Input

Posted on 1998-09-15
2
211 Views
Last Modified: 2011-04-14
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
Comment
Question by:larockd
2 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 1252745
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
 

Accepted Solution

by:
rayray120497 earned 20 total points
ID: 1252746
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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
reading tzdatabase for timezone definitions 5 145
C: GetDiskFreeSpaceEx() for default drive 14 114
How to learn Linux? 10 67
Loading flat file data in tables 2 42
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question