8 Replies - 626 Views - Last Post: 09 October 2009 - 04:34 AM Rate Topic: -----

#1 bluebear1608  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 82
  • Joined: 03-June 08

stuck in infinite loop

Posted 08 October 2009 - 08:07 PM

Im writing a program that allows the user to search a phrase, but for some reason, it seems to be stuck in an infinite loop.
i need to loop through each song and compare it with the other song, if its the same, then i need to check if the word positions are one appart.

Theres probably some logic errors in there, but i just cant work out what it is. please help me out
int wordQuery(Position result,Position result2)
{
  struct Song *song,*song2;
	struct WordPosition *position, *position2;
  if (result != NULL && result2 !=NULL) 
		{
		  song = result->firstSong;
		  song2 = result2->firstSong;
		  
		  fprintf(stderr,"Song\n");
		  fprintf(stderr,"----\n");
		  while (song != NULL) 
		  {
			
			while (song2 != NULL) 
			{
			  if(song2->Number == song->Number)
			  {
				position = song->firstPosition;
				while(position != NULL)
				{
				  position2 = song2->firstPosition;
				  while(position2!=NULL)
				  {
					if((position->Number+1)==position2->Number);
					{
					  printf("Song Number: %d Position: %d\n",song->Number,position->Number);
					  break;
					}
					
					position2=position2->nextPosition;
				  }
				  position=position->nextPosition;
				}
			  }
			  else
				song2=song2->nextSong;
			}
			song = song->nextSong;
			song2 = result2->firstSong;
		  }
		  fprintf(stderr,"\n");
		}
		return EXIT_SUCCESS;
}



Is This A Good Question/Topic? 0
  • +

Replies To: stuck in infinite loop

#2 no2pencil  Icon User is online

  • Toubabo Koomi
  • member icon

Reputation: 5247
  • View blog
  • Posts: 27,070
  • Joined: 10-May 07

Re: stuck in infinite loop

Posted 08 October 2009 - 08:09 PM

Wow. That is a LOT of while loops to be nesting.

The way that I would handle this, is to put a printf statement in each while loop, & output to a text file for logging. This way you can at least see where it's getting hung up. Also print out any variables, so you can visually verify what you think is going on, compared to what is actually going on.
Was This Post Helpful? 0
  • +
  • -

#3 bluebear1608  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 82
  • Joined: 03-June 08

Re: stuck in infinite loop

Posted 08 October 2009 - 08:18 PM

I have tried putting printf statements in, at the momment, all im getting is this:

Song Number: 9 Position: 150
Song Number: 9 Position: 152
Song Number: 9 Position: 154
Song Number: 9 Position: 213
Song Number: 9 Position: 215
Song Number: 9 Position: 217

over and over and over and over again. its the right answer btw, but showing once is enough i think. =]
Was This Post Helpful? 0
  • +
  • -

#4 no2pencil  Icon User is online

  • Toubabo Koomi
  • member icon

Reputation: 5247
  • View blog
  • Posts: 27,070
  • Joined: 10-May 07

Re: stuck in infinite loop

Posted 08 October 2009 - 08:24 PM

If it's correct, then I don't understand how it's stuck in a loop.
Was This Post Helpful? 0
  • +
  • -

#5 bluebear1608  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 82
  • Joined: 03-June 08

Re: stuck in infinite loop

Posted 08 October 2009 - 08:32 PM

Maybe im missing a break here or there, just dont know where.
Its definitely the correct answer though
Was This Post Helpful? 0
  • +
  • -

#6 mclang  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 32
  • Joined: 11-January 08

Re: stuck in infinite loop

Posted 08 October 2009 - 11:40 PM

To me it seems that the outermost while does not terminate ever.

Reason has to be that "song" is always something else than NULL. So the line:
song = song->nextSong;

is not working as it is supposed to... Are you sure the linked list's last "nextSong" does NOT point back to the first song, in other words is song linked list a circular one?
Was This Post Helpful? 0
  • +
  • -

#7 bluebear1608  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 82
  • Joined: 03-June 08

Re: stuck in infinite loop

Posted 09 October 2009 - 02:52 AM

I dont think thats the case because i have other functions that uses it and they're working fine. Im really scratching my head over this one
Was This Post Helpful? 0
  • +
  • -

#8 mclang  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 32
  • Joined: 11-January 08

Re: stuck in infinite loop

Posted 09 October 2009 - 03:50 AM

Then add more printing so that you can see how the while loops are progressing. For example before "return EXIT_SUCCESS;" is a good place to see if function terminates properly.

Same thing with "fprintf(stderr,"\n");" - does it execute properly? If not, it has to be some problem with "while (song != NULL)" -loop... Is it possible that you call "wordQuery(...)" -function over and over again?

edit: spelling mistakes

This post has been edited by mclang: 09 October 2009 - 04:04 AM

Was This Post Helpful? 0
  • +
  • -

#9 bluebear1608  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 82
  • Joined: 03-June 08

Re: stuck in infinite loop

Posted 09 October 2009 - 04:34 AM

ok, fixed it, i had an else statement in there when it should be there. no more infinitely loop problem, but now its causing a segmentation fault when i call the function a second time (first time it runs fine). i thought it was a problem with the buffer and used a funtion to clear buffer, but it still does it. any ideas?

This post has been edited by bluebear1608: 09 October 2009 - 04:35 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1