943,740 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 1940
  • C RSS
Oct 28th, 2008
0

Infinite loop problem

Expand Post »
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?
Similar Threads
Reputation Points: 10
Solved Threads: 1
Newbie Poster
thanatosys is offline Offline
15 posts
since Oct 2008
Oct 28th, 2008
0

Re: Infinite loop problem

Why do you put buildTrie in an endless while loop???
while(1) will loop forever !!
Reputation Points: 2035
Solved Threads: 644
Senior Poster
ddanbe is offline Offline
3,736 posts
since Oct 2008
Oct 28th, 2008
0

Re: Infinite loop problem

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.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
thanatosys is offline Offline
15 posts
since Oct 2008
Oct 28th, 2008
0

Re: Infinite loop problem

Test for an end of file condition.
Use something like while(!feof(inputfile))
Reputation Points: 2035
Solved Threads: 644
Senior Poster
ddanbe is offline Offline
3,736 posts
since Oct 2008
Oct 28th, 2008
0

Re: Infinite loop problem

Click to Expand / Collapse  Quote originally posted by ddanbe ...
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.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Oct 28th, 2008
0

Re: Infinite loop problem

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!
Reputation Points: 2035
Solved Threads: 644
Senior Poster
ddanbe is offline Offline
3,736 posts
since Oct 2008
Oct 28th, 2008
0

Re: Infinite loop problem

Click to Expand / Collapse  Quote originally posted by thanatosys ...
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.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Oct 31st, 2008
0

Re: Infinite loop problem

i have also problem in loop,do loop and while loop.can some suggest me about this three loops ?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
John11 is offline Offline
4 posts
since Oct 2008
Oct 31st, 2008
0

Re: Infinite loop problem

Click to Expand / Collapse  Quote originally posted by John11 ...
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.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Need help using postest loop in C, Please Help
Next Thread in C Forum Timeline: Newbie question in C PLEASE help!!!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC