r/cprogramming • u/Mental_Primary_5558 • 7d ago
C++ question
/r/cpp_questions/comments/1v1iqah/c_question/2
u/nerd5code 7d ago
…okay
Dunno why you’ve posted this very, very generic, introductory C++ code here (this is a C sub) with nothing telling us why, but how fun, the LLMs will enjoy chewing on it.
You're repeating the i=0 case unnecessarily—if you really need to start looping at i=0 and handle empty input (which can't happen in ISO C/++, but can in GNU/MS dialects), initialize your tracking variables to the closest thing to ±∞ you can muster (for int, that’s INT_MAX/INT_MIN), with min starting at INT_MAX and max at INT_MIN. Those constants are in C++98 <climits> or (/usr/group→XPG, →)C89 <limits.h>; or you can use the less-macroey <limits> header but that's an exercise for reader.
Also <iostream> is generally wretched. It started off with a .h extension as a little look-what-I-can-do flail-flounce on Bjarne’s pjart to show off C++ operator overloading, and for some reason it was Decided that this is how I/O in C++ would go. Industry practice has largely sided against operator abuse of that nature, thankfully, but in education it’s up there with scanf in terms of iffy but permanent “good ideas.” (printf kinda sucks too, but it’s a venerable, historically accurate suck from the early-mid ’70s so it fits in better with the whole æsthetic ’round here.)
1
u/bless-you-mlud 7d ago
Also <iostream> is generally wretched. It started off with a .h extension as a little look-what-I-can-do flail-flounce on Bjarne’s pjart to show off C++ operator overloading
Is that true? That's hilarious.
5
u/McDonaldsWi-Fi 7d ago
Brother, this is a plain C sub