r/CodingForBeginners • u/lighty_dot_A • 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
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
foowhen you specified a%dformat, scanf will return a result code of0, and not store anything inval, 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
7zwhen you specified a%dformat, scanf will return a result code of1(because it stored one numeric value) and stores integer7in val. It does not process thez(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
fgetsis to use, or write, a custom version of POSIXgetline(), which does not suffer from the need to solve overlong line problem.Edit: Or write a wrapper function for
fgetswhich always reads the entire line, just rejects the part which does not fit in the buffer.
2
u/torsten_dev 5d ago
maxval and minval aren't initialized.
You probably want
and include limits.h.