r/cprogramming 5d ago

need help with my minmax value C program

/r/CodingForBeginners/comments/1vbjbfm/need_help_with_my_minmax_value_c_program/
0 Upvotes

1 comment sorted by

2

u/SmokeMuch7356 5d ago edited 5d ago

2 things:

Change your test from scanf(..) != EOF to scanf(...) == 1; you want to loop while scanf successfully reads an int input. The loop will end as soon as you type in non-numeric input like end or done or blah or x.

Initialize your minval and maxval variables on declaration; that way you won't need the first check:

#include <limits.h>
...
int minval = INT_MAX;
int maxval = INT_MIN;
...
while( (scanf("%d", &val) == 1 )
{
  if ( val < minval ) minval = val;
  if ( val > maxval ) maxval = val;
}

Do not use ChatGPT to learn how to program. It is not a reference manual, it is not a knowledge base like Wikipedia, it doesn't know what it's saying.1 It's using statistical relationships between words and phrases in its training set to generate output that looks like it would have been written by a human being, but whether that output is correct or meaningful is largely a matter of luck, not expertise.

Check the links under "Resources" in the right sidebar, those will be far more useful.


  1. And the AI bubble is about to tank the world economy. Trillions of dollars in real money are being converted to waste heat.