I know it is bad form to show the OP full code, but I think this might help our OP to understand the issues better.
First, here is code that uses a while loop to read any number of integers, then outputs the minimum.
(If you run it from command line, you can end the input either via Ctrl+D (aka EOF), or inputting anything other than an integer.)
Code:
#include <stdio.h>
int main(void)
{
int numbers = 0;
int minimum;
int number;
/* While loop: Read a new integer into number,
* until there is no more input (or it is not an integer).
*/
while (scanf("%d", &number) == 1) {
/* Test 'numbers' for zero, then increase it. */
if (!numbers++) {
/* 'numbers' was zero, so this is the first number. */
minimum = number;
} else
if (number < minimum) {
/* 'numbers' was not zero, but number is the new minimum. */
minimum = number;
}
}
if (numbers) {
/* Numbers is nonzero, so there was at least one number input. */
printf("Of the %d numbers input, %d was the minimum.\n", numbers, minimum);
} else {
printf("No numbers in the input.\n");
}
return 0;
}
In the above code,
!numbers++ does two things at once: it tests if
numbers is zero, and then increments it by one. Some think this is too tricky, and frown on using this, but it is quite common. (Note that
!++numbers would first increase numbers by one, then test if it is zero.)
Here's the same code, but refactored using a for loop for exactly 'numbers' integers. Note that the for statement itself has format
for (initialization ; condition ; next_step) statement
where initialization is done unconditionally, only once, before anything else. The loop begins by testing the condition. If it is true, first statement is done, then next_step. Otherwise the loop ends. As always, statement can be just one statement, or it can be a block of statements enclosed in braces. initialization and next_step can contain multiple things separated by a comma in C99 at least.
I used the traditional
i for the loop variable, since 'number' is already used for the integer value read from input.
Code:
#include <stdio.h>
int main(void)
{
int numbers = 10;
int minimum;
int number;
int i;
for (i = 1; i <= numbers; i++) {
/* Read new number into number. If it is not a number, break out. */
if (scanf("%d", &number) != 1) {
fprintf(stderr, "Cannot parse integer %d of %d!\n", i, numbers);
break;
}
/* If this is the first number, it must be the minimum. Otherwise check. */
if (numbers == 1)
minimum = number;
else
if (number < minimum)
minimum = number;
}
/* If i == numbers + 1, the for loop completed successfully. */
if (i == numbers + 1) {
printf("Of the %d numbers read, %d was the minimum.\n", numbers, minimum);
} else
if (i > 1) {
/* We must have broken out of the loop early, but since i is where
* the error occurred, we have read i-1 integers already.
* Since i>1 (minimum i=2), we have read at least one integer.
*/
printf("Of the %d numbers read, %d was the minimum.\n", i - 1, minimum);
} else
printf("No numbers read, so no minimum.\n");
return 0;
}
Here's a third version of the same code, this time using a while loop to read only up to ten integers.
Code:
#include <stdio.h>
int main(void)
{
int numbers = 0;
int minimum;
int number;
/* Only read up to ten numbers. */
while (numbers < 10) {
/* Increase the numbers count. It will then be 1 for the first number. */
numbers++;
/* Read new number into number. If it is not a number, break out. */
if (scanf("%d", &number) != 1) {
fprintf(stderr, "Cannot parse the %d. integer!\n", numbers);
break;
}
/* If this is the first number, it must be the minimum. Otherwise check. */
if (numbers == 1)
minimum = number;
else
if (number < minimum)
minimum = number;
}
if (numbers > 0)
printf("Of the %d numbers read, %d was the minimum.\n", numbers, minimum);
else
printf("No numbers read, so no minimum either.\n");
return 0;
}
I believe you'd learn most if you compile and run the above three programs, then compare, and try to understand the differences between them. (As far as I can tell, the code is pure C99, and compiles without even pedantic warnings.)
If you use Linux, and have a wide display, I recommend using
diff --side-by-side file1 file2 to compare any two files side-by-side; append
| less if the output is longer than the screen.
(A small off-topic note: "unified diffs", e.g. by running
diff -Naprub thing1 thing2 are a very common way to describe differences between two different versions of the same source code. Lines beginning with
- refer to the left thing, and lines beginning with
+ refer to the right thing; other lines are common to both. The command can compare entire directories, not just single files. If you intend to work with open source code, the ability to quickly understand unified diffs is of great value. And of course most patches are unified diffs.)