@hjr265
Imagine you are trying to test a C++ code in console fill up a vector from the input with the thought of values after that using EOF input like this:
while ( cin >> num ) {
vector.push_back( num ) ;
}
what will happen?
As expected your code will take inputs as long as it crashes as you input some unexpected character, or will keep taking inputs until it exhausts your memory.
The hasNext()
method used by @ph3nixsabit bro works simillarly.
@ph3nixsabit
hasNext()
method returns true
if there are some more characters left when charcters are read from an input file.
if the input reaches the End Of File of EOF shortly.
But it is when one is taking inputs from an input file, the length of which is probably not infinity.
So, when it reaches the End of the input file, it will detect EOF and return the value false
.
Since, in Online Judges, the input is literally taken from the input files they have. Thus, it will exit the loop successfully.
But it is not the same for console because there is actually no limit to how much text ia printed or written in the console because the console erases previous texts automatically if the output text become big enough. Thus, there is no EOF in console and hasNext()
does not work. And so it feels like an infinity loop.
@hjr265
I had taken into consideration that @ph3nixsabit bro might have shared the code for testing purposes only. And I know that he knows how to solve a problem in a Toph. But, his codes are usually more clear and thoughts are reflected in them more clearly. So, I didn’t expected it to be like that.
I hope that you will understand how I feel with the determination of fixing a code and finding it poorly written bro @hjr265 and bro @ph3nixsabit.
And ofcourse, sorry for the sudden odd reply. I will delete it.