Solved

Reading In A Variable Amount Of Interger Input

Posted on 1998-09-15
2
206 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

762 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now