To begin, you must validate the return of every input function used -- every time. Otherwise, you invite undefined behavior be using a variable without knowing whether the input succeeded or failed. Always check the return (especially when using scanf())
Moreover, you must handle a matching-failure (user slips and presses 'r' instead of 4) and clear all characters from stdin before you attempt the next input in your loop. If you fail to do so, you will spin off into an infinite loop. Try entering 'r' in your code and see what happens?
In your case, you need to know if you are on your first trip though the loop or not. Just declare another int to use as a flag to indicate if your are in the first iteration or not. You can just use int first_loop = 1; to indicate your first trip through the loop, and set it to 0 during your loop. (don't worry that you set it to 0 every iteration, that is negligible)
Putting it altogether, your main could look like:
int main (void) {
int max = INT_MIN, /* initialize max to minimum possible value */
first_loop = 1; /* flag for 1st iteration, set true */
while (1) { /* loop continually */
int rtn, tmp; /* variable for scanf() return, temp value */
fputs ("enter value, -1 to exit: ", stdout); /* prompt user */
if ((rtn = scanf ("%d", &tmp)) == EOF) { /* read input saving return */
puts ("(user canceled input)"); /* manul EOF, user canceled */
return 0;
}
else if (rtn == 0) { /* return 0, matching failure occurred */
fputs (" error: invalid integer input.\n", stderr);
empty_stdin(); /* empty offending characters from stdin */
continue; /* get next input */
}
if (tmp == -1) { /* if input was -1 */
if (first_loop) { /* if first_loop set, 1st iteration */
puts ("NO INPUT"); /* output NO INPUT and exit */
return 0;
}
break; /* otherwise, break read-loop */
}
if (tmp > max) /* set max as necessary */
max = tmp;
first_loop = 0; /* set first loop flag false */
}
printf ("\nmax: %d\n", max); /* output result */
}
Note: setting of max to the minimum possible value in the range of int, INT_MIN. This is necessary whenever you are testing for max (and the converse for min). What if all the user enters are large negative values? There will be a maximum in the input, but you will never see it with max initialized to 0;
Also note the empty_stdin() function used to remove the characters that remain in stdin after a matching-failure occurs with scanf(). You simply loop until you find the '\n' generated by the user pressing Enter or you reach EOF for stdin. It can be nothing more than:
/* simple function to empty stdin */
void empty_stdin (void)
{
int c = getchar();
while (c != '\n' && c != EOF)
c = getchar();
}
Lastly note, you will need to include limits.h for INT_MIN, so with that, the full program could be:
#include <stdio.h>
#include <limits.h>
/* simple function to empty stdin */
void empty_stdin (void)
{
int c = getchar();
while (c != '\n' && c != EOF)
c = getchar();
}
int main (void) {
int max = INT_MIN, /* initialize max to minimum possible value */
first_loop = 1; /* flag for 1st iteration, set true */
while (1) { /* loop continually */
int rtn, tmp; /* variable for scanf() return, temp value */
fputs ("enter value, -1 to exit: ", stdout); /* prompt user */
if ((rtn = scanf ("%d", &tmp)) == EOF) { /* read input saving return */
puts ("(user canceled input)"); /* manul EOF, user canceled */
return 0;
}
else if (rtn == 0) { /* return 0, matching failure occurred */
fputs (" error: invalid integer input.\n", stderr);
empty_stdin(); /* empty offending characters from stdin */
continue; /* get next input */
}
if (tmp == -1) { /* if input was -1 */
if (first_loop) { /* if first_loop set, 1st iteration */
puts ("NO INPUT"); /* output NO INPUT and exit */
return 0;
}
break; /* otherwise, break read-loop */
}
if (tmp > max) /* set max as necessary */
max = tmp;
first_loop = 0; /* set first loop flag false */
}
printf ("\nmax: %d\n", max); /* output result */
}
Example Use/Output
No Input case:
$ ./bin/no_input
enter value, -1 to exit: -1
NO INPUT
Normal case, with intentional invalid input:
$ ./bin/no_input
enter value, -1 to exit: 1
enter value, -1 to exit: 9
enter value, -1 to exit: 4
enter value, -1 to exit: bananas
error: invalid integer input.
enter value, -1 to exit: 5
enter value, -1 to exit: 11
enter value, -1 to exit: -1
max: 11
Look things over and let me know if you have further questions.
maxto 0, you want ideally negative infinity, or, practically speaking, something like -99999999. That way the max can be less than zero.max_set = 0;then start readinginput.if (input == -1 && !max_set) { /* show NO INPUT */ }otherwisemax_set = 1;and use your general comparison againstmax. Note: you must check the return ofscanf()EVERY time...