you are encountering the strange behaviour because of this function (ideally it should not have compiled at all):
bool validity_check(int level,int n, int current[])
{
for(int i=0;i<level;i++)
{
if(current[i]==n) return false;
}
//Level 0 has always full validity.
if((level>0)&&(level<order-1))
{
if((current[level-1]==n)|(current[level+1]==n)) return false;
}
if(level==order-1)
{
if(current[level-1]==n) return false;
}
// warning: control reaches end of non-void function
//return true ; // add this and the strange behaviour will dissappear
}
at the place where the function is called,
while(true)
{
//cout<<"Entered the while loop"<<endl; //This is the line which can change the whole output when uncommented
for(int i=nextjump[level];i<order;i++)
{
if(validity_check(level,i,position))
// the contents of the eax register at this
// point determines wheateher the result
// is true or false
// since the function does not return a value
// what eax will be on return is influenced
// the state of the registers before the call
// apparently, the cout << ...
// causes eax to be non-zero on
// exit from the function.
// cout << ... returns a reference to cout (in eax)
// non-zero; the if condition evaluates to true
// without the cout << ... eax is zero
// and the if condition evaluates to false.
{
// ...
moral of the story: compile with standard conformance and all warnings enabled. and pay attention to the warnings.
use something like: > g++ -Wall -std=c++98 -pedantic -Werror myprogram.cc
(g++ is really bad if you just settle for the defaults.)
note: this is just advice. anyone is free to ignore it if they feel like doing so.