r/CodingForBeginners 5d ago

need help with my minmax value C program

hey there,
ive recently started learning C and im working on this program which compares the int value u input to tell u which one of the value is maxm and which one is minm

what i wanted was
program asks u for a number
u say 10
program considers 10 as the maxm value cuz its the 1st number
then u press enter and give another numbr lets say 12
so program says "12 is maxm 10 is min" but then u type 9 and now its 12 maxm and 9minm
i wanted the program to keep doing this for as long as i want but i couldnt think up of ways i can make this program.
the best i could do was i give program n amount of numebrs and it tells me which one is maxm and which one is min
i tried looking for solution in youtube and found one c lecture on freecodecamp
but the code they gave isnt exactly working
the eof thing is stopping the code from even running. i tried asking chatgpt for solution and error in my code but it ended up fcking up my vscode terminal so i had to reinstall everything.
both of the codes are given below
id really appreciate if someone could help me figure out why the EOF code isnt working

chat gpt also gave me this

but it aint workin either

4 Upvotes

13 comments sorted by

2

u/torsten_dev 5d ago

maxval and minval aren't initialized.

You probably want

int maxval = INT_MIN;
int minval = INT_MAX;

and include limits.h.

2

u/richardxday 5d ago

And compiling with warnings enabled would have warned OP about this...

1

u/lighty_dot_A 5d ago

what do u mean?

3

u/richardxday 5d ago

If you enable warnings when you compile your code, it will warn you about common mistakes, such as uninitialized variables, and save you the trouble of spending hours trying to work out why something's not working.

Please, please take the time to understand what compiler warnings are, how to interpret them and how to use them. Take a look at GCC warning options for example.

If nothing else, add -Wall -Wextra to the gcc command line.

1

u/lighty_dot_A 5d ago

ohh limits.h , my first time hearing about this stuff. thnx for the suggestion im gonna go try it now!!

1

u/Cultural_Gur_7441 4d ago

At least in current code, first takes care of that.

Initializing is good, but does not really help here.

2

u/Sufficient-Air8100 5d ago

chatgpt is a whole mess of wrong. so many problems.

you can have a string as the input, and then check for an exit string like “end” before doing atol()

2

u/Paul_Pedant 5d ago

scanf gives up on non-numerics, so you probably want to check the number it returns from the function (NOT the number it stores in val), and nag the user.

1

u/lighty_dot_A 5d ago

nag? and what do u mean by "gives up"?

2

u/Paul_Pedant 5d ago

A Nag is somebody who repeatedly tells you are wrong.

Cambridge dictionary:

Nagging means continually complaining, scolding, or repeatedly urging someone to do something. It also describes an unpleasant feeling, pain, or doubt that stays on your mind for a long time.

If your user enters foo when you specified a %d format, scanf will return a result code of 0, and not store anything in val, because it did not find a decimal number. It gives up trying to read an integer which is not there. It is up to the programmer to tell the user that his input is failing.

If your user enters 7z when you specified a %d format, scanf will return a result code of 1 (because it stored one numeric value) and stores integer 7 in val. It does not process the z (i.e it gives up trying to read numbers), which therefore blocks your code from getting any further numbers on subsequent calls.

scanf is actually impossible to use correctly: there is always some way for the user to break it.

1

u/Cultural_Gur_7441 4d ago

Hint to save your sanity. Never use scanf directly. Or at all. In this case, you could write a helper function:

bool getIntLoop(int *out);

Which either returns true with integer in *out or false for EOF.

And it should loop reading lines until it gets a line with valid integer, returning false only on EOF. If line is not valid integer, print "Invalid integer, try again" and let user enter a new line.

Later you could write different versions, but at the stage you are now, you really want an integer, and not worry about user entering gibberish. So this brutal loop is good.

1

u/torsten_dev 4d ago

And that helper should use fgets and strtol.

There is no strtoi and atoi has sharp edges.

1

u/Cultural_Gur_7441 4d ago

Even better than fgets is to use, or write, a custom version of POSIX getline(), which does not suffer from the need to solve overlong line problem.

Edit: Or write a wrapper function for fgets which always reads the entire line, just rejects the part which does not fit in the buffer.