Solved

Reading In A Variable Amount Of Interger Input

Posted on 1998-09-15
2
214 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

622 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