Infinite loop problem

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2008
Posts: 15
Reputation: thanatosys is an unknown quantity at this point 
Solved Threads: 1
thanatosys thanatosys is offline Offline
Newbie Poster

Infinite loop problem

 
0
  #1
Oct 28th, 2008
First here is my header file
  1. /* @source Simplespell Program
  2.  *
  3.  * @author: Chris King(cpking@ncsu.edu)
  4.  * @@
  5.  *
  6.  * node.h
  7.  * CSC 230 Fall 2008
  8.  *
  9.  * The @ signs along with the word denote specific information about
  10.  * the program. Using the standards described in:
  11.  * http://emboss.sourceforge.net/developers/c-standards_new.html
  12.  */
  13.  
  14. typedef struct node{ //just creating a node
  15. struct node *array[27]; // a node is merely an array of pointers... TO MORE POINTERS!
  16. }Node, *node_ptr; // wrapping this up
  17.  
  18. node_ptr createRoot(void); // method to create a root node.
  19.  
  20. node_ptr addNode(char); //method to add a node to a trie
  21.  
  22. node_ptr createNode(void); // method to generate a new node
  23.  
  24. node_ptr buildTrie(node_ptr, char*); // eventually this will compile the trie
  25.  
  26. int readFile(FILE *);

here is the source code itself
  1. /* @source Simplespell Program
  2.  *
  3.  * @author: Chris King(cpking@ncsu.edu)
  4.  * @@
  5.  *
  6.  * spellback.c
  7.  * CSC 230 Fall 2008
  8.  *
  9.  * The @ signs along with the word denote specific information about
  10.  * the program. Using the standards described in:
  11.  * http://emboss.sourceforge.net/developers/c-standards_new.html
  12.  */
  13.  
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <ctype.h>
  18. #include <stdbool.h>
  19. #include "node.h"
  20.  
  21. #define MAXSIZE 2048 // max size of a char array here (yes i know it could have been 101)
  22. #define MAXLENGTH 100
  23.  
  24. struct node *rootNode; // our top node in chief.
  25. FILE *inputfile; // any input file here
  26.  
  27. int main(int argc, char *argv[]){
  28. int ck;
  29. ck = argc;
  30. createRoot();
  31. //printf("%d\n", ck);
  32.  
  33. FILE *fp;
  34.  
  35. if(ck == 1){ // checking for no parameters here
  36. printf("Opening default file\n");
  37. readFile(fp = fopen(".WORDS", "r"));
  38.  
  39. fclose(fp);
  40. printf("Closed default file\n");
  41.  
  42. }
  43.  
  44. if(ck == 2 || ck == 4){ // checks for bs parameters here
  45. printf("Invalid Input\n");
  46. return -1;
  47. }
  48.  
  49. if(ck == 3){ // checks for 1 input file here
  50. printf("Opening default file\n");
  51. fp = fopen(".WORDS", "r");
  52. fclose(fp);
  53. //point fp to next file
  54. printf("Closed default file\n");
  55. fp = fopen(argv[2], "r");
  56. printf("Opening selected file\n");
  57. fclose(fp);
  58. printf("Closed selected file\n");
  59. }
  60.  
  61. if(ck >= 5){ // checks for more than 5 (this is not finished yet)
  62. printf("Opening default file\n");
  63. fp = fopen(".WORDS", "r");
  64. fclose(fp);
  65. printf("Sorry the rest has not been written yet.\n");
  66. }
  67.  
  68. return 0;
  69. }
  70.  
  71. node_ptr createRoot(void){ // this simply uses calloc to allocate the ram for my root trie node
  72. rootNode = calloc(1,sizeof(Node));
  73. return rootNode;
  74. }
  75.  
  76.  
  77. node_ptr createNode(void){ // here we find new nodes being created for a trie
  78. struct node *newNode;
  79. newNode = calloc(1,sizeof(Node));
  80. return newNode;
  81. }
  82.  
  83. int readFile(FILE *inputfile){
  84. char line [MAXLENGTH];
  85. struct node *nodeStart;
  86. nodeStart = rootNode;
  87. printf("I AM BEFORE THE WHILE LOOP.\n");
  88. while(1){
  89. printf("HELP I'M IN A WHILE LOOP.\n");
  90. buildTrie(nodeStart, fgets(line, MAXLENGTH, inputfile));
  91. }
  92. return 0;
  93. }
  94.  
  95. node_ptr buildTrie(node_ptr nodeIn, char *text){
  96. while(text[0] == '\0'){
  97. break;
  98. }
  99.  
  100. //rest of code here
  101. printf("we have a line.\n");
  102. return rootNode;
  103.  
  104. }

the error
  1. $ ./simplespell
  2. Opening default file
  3. I AM BEFORE THE WHILE LOOP.
  4. HELP I'M IN A WHILE LOOP.
  5. we have a line.
  6. HELP I'M IN A WHILE LOOP.
  7. we have a line.
  8. HELP I'M IN A WHILE LOOP.
  9. we have a line.
  10. HELP I'M IN A WHILE LOOP.
  11. we have a line.
  12. HELP I'M IN A WHILE LOOP.
  13. we have a line.
  14. HELP I'M IN A WHILE LOOP.
  15. we have a line.
  16. HELP I'M IN A WHILE LOOP.
  17. CONTINUES FROM HERE FOREVER
  18.  

I believe the problem is this
  1. while(1){
  2. printf("HELP I'M IN A WHILE LOOP.\n");
  3. buildTrie(nodeStart, fgets(line, MAXLENGTH, inputfile));
  4. }
  5. return 0;

I am trying to get lines until the end of the file, and pass the char array via pointer into the buildTrie function. How can I improve this?
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,973
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 286
ddanbe's Avatar
ddanbe ddanbe is online now Online
Posting Virtuoso

Re: Infinite loop problem

 
0
  #2
Oct 28th, 2008
Why do you put buildTrie in an endless while loop???
while(1) will loop forever !!
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 15
Reputation: thanatosys is an unknown quantity at this point 
Solved Threads: 1
thanatosys thanatosys is offline Offline
Newbie Poster

Re: Infinite loop problem

 
0
  #3
Oct 28th, 2008
Well i need to run fgets to get the pointer to the char array, i need to run this until the end of file... how would that while loop look like? Yes I am aware of while true... but I assumed if there was an error it would just break.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,973
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 286
ddanbe's Avatar
ddanbe ddanbe is online now Online
Posting Virtuoso

Re: Infinite loop problem

 
0
  #4
Oct 28th, 2008
Test for an end of file condition.
Use something like while(!feof(inputfile))
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,033
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: Infinite loop problem

 
0
  #5
Oct 28th, 2008
Originally Posted by ddanbe View Post
Test for an end of file condition.
Use something like while(!feof(inputfile))
Bad idea, feof() shouldn't be use as the exit control of a loop. Read on.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,973
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 286
ddanbe's Avatar
ddanbe ddanbe is online now Online
Posting Virtuoso

Re: Infinite loop problem

 
0
  #6
Oct 28th, 2008
OK Aia I know fgets gets NULL when getting something past eof, but I was concentrating on the infinity of the while loop first.
But anyway, thanks for reminding me!
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,033
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: Infinite loop problem

 
0
  #7
Oct 28th, 2008
Originally Posted by thanatosys View Post
Well i need to run fgets to get the pointer to the char array, i need to run this until the end of file... how would that while loop look like? Yes I am aware of while true... but I assumed if there was an error it would just break.
Hopefully, your assumption has been adjusted to go more in accordance with reality. As you have seen while(1) will not stop if nothing tells it to stop.

  1. while( fgets(line, MAXLENGTH, inputfile) ) {
  2. printf("HELP I'M IN A WHILE LOOP.\n");
  3. buildTrie(nodeStart, inputfile);
  4. }
Might work.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 4
Reputation: John11 is an unknown quantity at this point 
Solved Threads: 0
John11's Avatar
John11 John11 is offline Offline
Newbie Poster

Re: Infinite loop problem

 
0
  #8
Oct 31st, 2008
i have also problem in loop,do loop and while loop.can some suggest me about this three loops ?
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,033
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: Infinite loop problem

 
0
  #9
Oct 31st, 2008
Originally Posted by John11 View Post
i have also problem in loop,do loop and while loop.can some suggest me about this three loops ?
You'll receive a better treatment if you create your own thread, post the pertinent portion of the code you are having problems with, and last but not least, you read, get aware, and follow the rules of the forum.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC