Mark44
Mentor
- 38,048
- 10,543
Or you could use fflush() to flush the input buffer.SlowThinker said:As for the scanf, I don't actually use scanf at all but you may try to do something like
just before you ask for array length. It's supposed to read and throw away any characters left in stdin.C:while (getchar()!=EOF) { }
C:
fflush(stdin);
This is especially useful if you read in a number and then later read in a string of characters or even one character. Flushing the input buffer after reading a number prevents a following read from taking in the newline character.
I'm not sure I understand your question. Are you are using scanf() to input more than one number at a time? It's easiest on the user of your program (which is not necessarily you) to input a single number per call to scanf().doktorwho said:Do you happen to know how to solve my next bug: When i scan for the numbers in the array if i input let's say 9 numbers and I've said I am going to have 8 numbers that last one is transferred as the array length input for the second loop. Could i somehow tell the scanf function to scan only n numbers and ignore the rest?
You could issue a prompt that asks how many numbers are to be entered. Then use that number to control a for loop. Something like this:
Code:
printf("How many numbers do you want to enter?")
scanf("%d", &count);
for (i = 0; I < count; ++i)
{
printf("> ");
scanf("%d", &arr[ i]);
}