Click to See Complete Forum and Search --> : Stupid C Question
RAGEAngel9
10-22-2002, 01:57 PM
I feel stupid for asking this because I really should know it.
Anyway in C, how would I use scanf to get keyboard input if I'm not sure how many inputs I will have. Specifically, I ask the user to input up to 10 integers. Can I just use scanf like this:
scanf("%i %i %i %i %i %i %i %i %i %i", &a, &b, &c, &d, &e, &f, &g, &h, &i, %j);
Or is there a better way?
Thankx
mingshun
10-22-2002, 02:21 PM
Originally posted by RAGEAngel9
I feel stupid for asking this because I really should know it.
Anyway in C, how would I use scanf to get keyboard input if I'm not sure how many inputs I will have. Specifically, I ask the user to input up to 10 integers. Can I just use scanf like this:
scanf("%i %i %i %i %i %i %i %i %i %i", &a, &b, &c, &d, &e, &f, &g, &h, &i, %j);
Or is there a better way?
Thankx
How about something like this?
I did not compile and run this so this is prone to errors and bugs.
#include <stdio.h>
#include <stdlib.h>
main ()
{
int total, *arr, i;
printf ("Enter the total no of inputs: ");
fflush (stdout);
scanf ("%d", &total);
arr = (int *) calloc (total, sizeof (int));
puts ("Enter the rest of the integers");
for (i = 0; i < total; i++) scanf ("%d", arr[i]);
}
Fandelem
10-23-2002, 11:04 AM
If you use the above method, remember to free your memory ! ;o)
k.
l01yuk
10-25-2002, 05:42 AM
Also, if you want to use scanf you should check the return value to make sure the correct data type was input by the user.