I am reposting this thread because the last time I posted it no one understood it and some people with a desire to help me posted solutions that didnt solve my problem. This actually caused the post count fo the thread to reach 5 which caused other people not to reply thinking that the question might have been answered

Kindly dont reply if you dont understand my question
Let me state this problem
I dont want to know the bugs in my problem.
Please dont tell me about them.

I am having a strange problem. Please help me.
I have a code which tries to solve the queen's problem. It is wrong so please dont bother yourself with the code unless you are interested.
The problem is that when I execute this wrong code, it goes into infinite loop.
But prints only one line which is "Successful at level 0 with position 0"
But the interesting thing is that when I add another line which does noting but only print a statement "Entered the while loop", what happens is that the "successful at level ......." line also appears infinite times.

The line is 15th in this code and I have commented that line out.

Can someone please guide why it is so.

Please run the program twice once when it is commented and second time when it is uncommented. I am using gcc (GCC) 4.1.1 20061011 (Red Hat 4.1.1-30)

#include <iostream>
using namespace std;
#define order 8
void printall(int arr[]);
bool validity_check(int,int,int []);
int main()
{
        int nextjump[order];//Keeps track for every order what should be the pos. of next jump.
        for(int i=0;i<order;i++) nextjump[i]=0;
        int level=0;
        int position[order];
        mainloop:
        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))
                        {
                                cout<<"Successful at level "<<level<<" using position "<<i<<endl;
                                position[level]=i;
                                nextjump[level++]=i+1;


                                if(level==order)
                                {

                                        printall(position);
                                        goto mainloop;
                                }

                                nextjump[level]=0;
                                goto mainloop;
                        }
                }

                level-- ;
                goto mainloop;
        }
        return 0;
}

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;
        }

}

void printall(int current[])
{
        for(int i=0;i<order;i++)
        {
                cout<<current[i]<<endl;
        }
}

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.

Comments
Very Very helpful. I am grateful
This article has been dead for over six months. Start a new discussion instead.