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.
But when I add another line in the code at one place, it prints the first line and the newer line both.
So I dont understand why is it so.
Can someone please guide:
The line is 15th in this code and I have commented that line out. 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;
        }
}

The value of level is never getting incremented, so after the program executes level--; the value of level is -1. And -1 is not a valid number to use for indexing into arrays or to be passed to validity_check()

Learn to use your compiler's debugger so that you can easily find bugs such as that one. It took me all of about 5 minutes (include compile time) to find it.

A couple of things,
1) you are using bitwise OR operator in

if((current[level-1]==n)[B]|[/B](current[level+1]==n)) return false;

You probably meant logical OR i.e.

if((current[level-1]==n)[B]||[/B](current[level+1]==n)) return false;

2) and then usage of 'goto', you could throw away the 'mainloop' label and inside the for() loop replace
goto mainloop;
with
break;
and just delete the third goto mainloop;

Thank you very much for trying to solve my problem. Probably I was not clear in mentioning my problem. I only have the problem with strange behaviour of cout. When I comment line 15, the output is different then when I do not comment it.
Please compile the code and try that. Please do it because the problem is confusing. Dont try to debug the code. It has many errors I know that.
Thanks.

I'd suggest to first fix the code and thereafter see if it behaves in some strange way.
You have no break statement to get out of the 'while(true)' loop, so an infinite loop should always occur.

Let me reiterate my problem. My problem is not the infinte loop. I know that it will occur no matter what becasue the code is buggy.

My problem is only that when I keep the line 15 commented, only one line is printed lets call that line A.
Now when I uncomment line 15 which is merely a cout statement and not a control statement, the line that I uncomment is printed but at the same time line A is also printed many mnay times.
Can someone please explain this behaviour
thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.