Solved

Reading In A Variable Amount Of Interger Input

Posted on 1998-09-15
3
221 Views
Last Modified: 2010-04-15
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
3 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 1252747
What was unexpected about what it did when the user entered all tree integers on the same line?
0
 
LVL 1

Accepted Solution

by:
mlaiosa earned 20 total points
ID: 1252748
When you use the standard input rutines, some crazy buffering will be used.  When you run scanf, it will begin processing the things you asked for (the string whcih is the first parameter).  If the buffer becomes empty before it is done, it will have the user enter chartcers (into the buffer) untill return is pressed, then continues processing the buffer.

If all the ints are entered on the same line, only the first one will be processed by the first scanf (which is what gets the keyboard input), the rest will remain in the buffer.  Next time you call scanf, no input will be requested becaouse the required data is already in the buffer.

Try it out:


#include <stdio.h>

void main ()
 {
  int n;
  printf("\nHow many integers? ");
  scanf("%d",&n);

  int* a=new int[n];


  for ( int i = 0 ; i < n; i++ )
   {
    scanf("%d",a+i);
   }

  printf("Here they are:\n");

  for ( i=0; i<n; i++ )
   {
    printf("%2d: %d\n",i+1,a[i]);
   }
 }

0
 

Author Comment

by:larockd
ID: 1252749
Thank you for taking your time to answer this.  Everything worked perfect. I also appreciate the explanation on the buffering.,.
0

Featured Post

Technology Partners: 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!

Question has a verified solution.

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

Suggested Solutions

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…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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 switch statements in the C programming language.

749 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