Never ending loop

  • (2 Pages)
  • +
  • 1
  • 2

27 Replies - 1167 Views - Last Post: 16 December 2010 - 06:30 AM Rate Topic: -----

#1 huzaifa.iu  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 62
  • Joined: 13-December 10

Never ending loop

Posted 13 December 2010 - 11:26 AM

Hello guys, I am new to this forum and this is my first post, so please bear with me :)
I am trying to write a code for a kind of BFS, where I have created the nodes using structure as I don't know how to use classes and objects or pointers. What I wanted to ask is, that when I run this code, it gives no error, but it goes into a never ending loop.

Can anyone tell me why is this happening? I know there's something in the loop expression, but to me it seems alright. I want the code to check for one node for the 'goal', if it doesn't find the node, it continues to explore the next nodes. There I have made a checking IF statement that when it does find the node which is == the goal, it gives out the message else continues with the search. Here's the code. Will be looking for a response. Thanks in advance.

#include <iostream.h>
#include <stdlib.h>
#include <conio.h>
#include <stdio.h>

struct node
{
	int left;
	int right;
	int root;
	int ncost;
	
} ;

void main()
{
	clrscr();
	srand(time(NULL)); 

	node n[5];

for (int a=0; a<5; a++)
	{
		n[a].left  = (rand()%10)+1;
		n[a].right = (rand()%10)+1;
		n[a].root  = (rand()%10)+1;
		n[a].ncost = (rand()%10)+1;
	}
	int goal;
	cout<<"What is the value of your goal node <1-10>: "; cin>>goal;

			int c=0;
			int b=0;
			int cost[10]={0};
			int final_cost=0;
			
			
	   while ((n[c].left && n[c].right) != goal)
		{
		cost[b] = final_cost + n[c].ncost;
		final_cost = cost[b];
		cout<<"Exploring next node ... "<<endl;
		
		
				if (((n[c].left) || (n[c].right)) == goal)
				{
					cout<<"Goal node found!"<<endl;
					cout<<"The final cost for the search of the goal is :"<<final_cost<<endl;
				}
					else
					b++;
					c++;
						
		if (c==5)
		cout<<"Sorry, node not found in this search"<<endl;
		break;
		}
					


getch();
}



Is This A Good Question/Topic? 0
  • +

Replies To: Never ending loop

#2 Salem_c  Icon User is online

  • void main'ers are DOOMED
  • member icon

Reputation: 1675
  • View blog
  • Posts: 3,169
  • Joined: 30-May 10

Re: Never ending loop

Posted 13 December 2010 - 11:39 AM

> while ((n[c].left && n[c].right) != goal)
Are you trying to compare both things with goal?

Because it doesn't do that.
Was This Post Helpful? 0
  • +
  • -

#3 huzaifa.iu  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 62
  • Joined: 13-December 10

Re: Never ending loop

Posted 13 December 2010 - 11:44 AM

Hi, thanks for the quick response! :)

Yeah apparently I did try using the || operator as well to check if there is any changes. But in both && and || it kept on going on a infinite loop. But yes, I am trying to compare both or any of the values with the goal. If any one matches, then it should give follow the IF statement after that giving out the message that the node has been found.
Was This Post Helpful? 0
  • +
  • -

#4 Kilorn  Icon User is offline

  • XNArchitect
  • member icon



Reputation: 1356
  • View blog
  • Posts: 3,528
  • Joined: 03-May 10

Re: Never ending loop

Posted 13 December 2010 - 11:48 AM

while (n[c].left != goal && n[c].right != goal)

I don't think you can check two things at once like that without separating it into two different statements. If I'm wrong, someone please let me know, I'm definitely not an expert at c/c++.
Was This Post Helpful? 1
  • +
  • -

#5 jimblumberg  Icon User is offline

  • member icon


Reputation: 4060
  • View blog
  • Posts: 12,534
  • Joined: 25-December 09

Re: Never ending loop

Posted 13 December 2010 - 11:49 AM

Try something like:

while ((n[c].left != goal) && (n[c].right != goal))


Jim

This post has been edited by jimblumberg: 13 December 2010 - 11:49 AM

Was This Post Helpful? 1
  • +
  • -

#6 Kilorn  Icon User is offline

  • XNArchitect
  • member icon



Reputation: 1356
  • View blog
  • Posts: 3,528
  • Joined: 03-May 10

Re: Never ending loop

Posted 13 December 2010 - 11:50 AM

You'll also have to change if (((n[c].left) || (n[c].right)) == goal) as well, I believe.
Was This Post Helpful? 0
  • +
  • -

#7 huzaifa.iu  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 62
  • Joined: 13-December 10

Re: Never ending loop

Posted 13 December 2010 - 11:56 AM

Nope, it is still going into infinite loop at "Exploring next node ... " :(
Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg  Icon User is offline

  • member icon


Reputation: 4060
  • View blog
  • Posts: 12,534
  • Joined: 25-December 09

Re: Never ending loop

Posted 13 December 2010 - 12:01 PM

Did you change this line as well?

               if (((n[c].left) || (n[c].right)) == goal)


Jim
Was This Post Helpful? 0
  • +
  • -

#9 huzaifa.iu  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 62
  • Joined: 13-December 10

Re: Never ending loop

Posted 13 December 2010 - 12:06 PM

View Postjimblumberg, on 13 December 2010 - 11:01 AM, said:

Did you change this line as well?

               if (((n[c].left) || (n[c].right)) == goal)


Jim


Yes I did change that as well, and even tried using && and || on both while and if statements incase it was a logical error, but it still went into infinity at "Exploring next node..."
Was This Post Helpful? 0
  • +
  • -

#10 jimblumberg  Icon User is offline

  • member icon


Reputation: 4060
  • View blog
  • Posts: 12,534
  • Joined: 25-December 09

Re: Never ending loop

Posted 13 December 2010 - 12:11 PM

Please post the modified code.


Jim
Was This Post Helpful? 0
  • +
  • -

#11 huzaifa.iu  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 62
  • Joined: 13-December 10

Re: Never ending loop

Posted 13 December 2010 - 12:14 PM

#include <iostream.h>
#include <stdlib.h>
#include <conio.h>
#include <stdio.h>

struct node
{
	int left;
	int right;
	int root;
	int ncost;
	
} ;

void main()
{
	clrscr();
	srand(time(NULL)); 
	node n[5];

for (int a=0; a<5; a++)
	{
		n[a].left  = (rand()%10)+1;
		n[a].right = (rand()%10)+1;
		n[a].root  = (rand()%10)+1;
		n[a].ncost = (rand()%10)+1;
	}
	int goal;
	cout<<"What is the value of your goal node <1-10>: "; cin>>goal;

			int c=0;
			int b=0;
			int cost[10]={0};
			int final_cost=0;
			
			
	   while (((n[c].left) != goal) || ((n[c].right != goal)))
		{
		cost[b] = final_cost + n[c].ncost;
		final_cost = cost[b];
		cout<<"Exploring next node ... "<<endl;
		
		
				if (((n[c].left) == goal) || ((n[c].right) == goal))
				{
					cout<<"Goal node found!"<<endl;
					cout<<"The final cost for the search of the goal is :"<<final_cost<<endl;
				}
					else
					b++;
					c++;
						
		if (c==5)
		cout<<"Sorry, node not found in this search"<<endl;
		break;
		}
					


getch();
}


Was This Post Helpful? 0
  • +
  • -

#12 shad0wk1  Icon User is offline

  • D.I.C Head

Reputation: 31
  • View blog
  • Posts: 139
  • Joined: 01-October 10

Re: Never ending loop

Posted 13 December 2010 - 12:26 PM

I don't like the clrscr() (clearly not needed at first line of main()) and void main().

if (c==5)
        cout<<"Sorry, node not found in this search"<<endl;
        break;



Maybe because there is no bracket in this if.

This post has been edited by shad0wk1: 13 December 2010 - 12:28 PM

Was This Post Helpful? 1
  • +
  • -

#13 jimblumberg  Icon User is offline

  • member icon


Reputation: 4060
  • View blog
  • Posts: 12,534
  • Joined: 25-December 09

Re: Never ending loop

Posted 13 December 2010 - 12:50 PM

In your code below please explain the purpose of the else.

				if (((n[c].left) == goal) || ((n[c].right) == goal))
				{
					cout<<"Goal node found!"<<endl;
					cout<<"The final cost for the search of the goal is :"<<final_cost<<endl;
				}
					else
					b++;
					c++;
						
		if (c==5)
		cout<<"Sorry, node not found in this search"<<endl;
		break;



#1 Is the line b++ part of this else?
#2 Is the line c++ part of this else?

Also the last is the last if just for debugging?

#3 Also for the last if is the break; part of that if statement.

Jim
Was This Post Helpful? 0
  • +
  • -

#14 huzaifa.iu  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 62
  • Joined: 13-December 10

Re: Never ending loop

Posted 13 December 2010 - 12:51 PM

View Postshad0wk1, on 13 December 2010 - 11:26 AM, said:

I don't like the clrscr() (clearly not needed at first line of main()) and void main().

if (c==5)
        cout<<"Sorry, node not found in this search"<<endl;
        break;



Maybe because there is no bracket in this if.


Thanks :) That solved the never ending loop issue! :D You guys are awesome! Thanks all. Peace.
Was This Post Helpful? 0
  • +
  • -

#15 huzaifa.iu  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 62
  • Joined: 13-December 10

Re: Never ending loop

Posted 13 December 2010 - 12:56 PM

View Postjimblumberg, on 13 December 2010 - 11:50 AM, said:

In your code below please explain the purpose of the else.

				if (((n[c].left) == goal) || ((n[c].right) == goal))
				{
					cout<<"Goal node found!"<<endl;
					cout<<"The final cost for the search of the goal is :"<<final_cost<<endl;
				}
					else
					b++;
					c++;
						
		if (c==5)
		cout<<"Sorry, node not found in this search"<<endl;
		break;



#1 Is the line b++ part of this else?
#2 Is the line c++ part of this else?

Also the last is the last if just for debugging?

#3 Also for the last if is the break; part of that if statement.

Jim


Hi, here's the code, it solved the loop issue but it is not entering the first IF statement though. Sometimes when I run it goes to the 2nd IF statement and displays that the node was not found, and sometimes when I run it shows "Exploring next node..." once, and then it stops. And when I press any key it gets back to the compiler. So maybe it is not entering the IF statement.

#include <iostream.h>
#include <stdlib.h>
#include <conio.h>
#include <stdio.h>

struct node
{
	int left;
	int right;
	int root;
	int ncost;
	
} ;

int main()
{
	srand(time(NULL)); 

	node n[5];

for (int a=0; a<5; a++)
	{
		n[a].left  = (rand()%10)+1;
		n[a].right = (rand()%10)+1;
		n[a].root  = (rand()%10)+1;
		n[a].ncost = (rand()%10)+1;
	}
	int goal;
	cout<<"What is the value of your goal node <1-10>: "; cin>>goal;

			int c=0;
			int b=0;
			int cost[10]={0};
			int final_cost=0;
			
			
	   while (((n[c].left) != goal) && ((n[c].right != goal)))
		{
		cost[b] = final_cost + n[c].ncost;
		final_cost = cost[b];
		cout<<"Exploring next node ... "<<endl;
		
		
				if (((n[c].left) == goal) || ((n[c].right) == goal))
				{
					cout<<"Goal node found!"<<endl;
					cout<<"The final cost for the search of the goal is :"<<final_cost<<endl;
				}
					else 
					{
					b++;
					c++;
					}
		if (c==5)
		{
		cout<<"Sorry, node not found in this search"<<endl;
		break;
		} 
			
		}
		
					


getch();
}


Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2