You have to understand that in computing, when ever something is destroyed, it is not set to 0 or some magic value. It is simply set to be overwritten when the next process comes.
In your loop, you didn’t initialize SampleVariable
and it just took what ever value was there before. In the first iteration it’s whatever was there before. In the next few iterations, it’s what SampleVariable
was in the past iteration because by chance (not really), your float got assigned to the same location in memory.
Initialize your variable like
float SampleVariable = 0.f;
and the your std::cout
will print 0 everytime.
3
solved C++ – How to clear variables after a while loop? [closed]