Thread: loop issues

  1. #1
    UNIX chick
    Join Date
    Mar 2003
    Posts
    92

    loop issues

    In a function I'm having three loops, that are nested. But something's not working, and it's driving me nuts! Is there a limit for how long a loop can be, or how many loops that can be nested?

    My program is supposed to do the following:
    - loop while user wants to continue
    - generate a random number, which repsesents a random line in a file
    - fgets() a line, checks if this line is the same as the random number (if the line is correct, in other words)
    - split this line, print out half, get input and check input against first half of line
    - loop inner loop until correct or ! is pressed

    The code is very buggy, but this is what I've got so far. The looping is all wrong, but I've corrected it so many times I can't think of anything else. Also, I'm not sure if this is the best way at all to do what I'm trying to do. All input hightly appreciated.

    Code:
    	while(1) {
    		/* a random line to start off with */
    		randnum = returnRand(lines);
    		printf("%d %d\n", randnum, readline); /* for debugging */
    		
    		/* reads one line at the time */
    		while(fgets(buff, FILEBUFFER, file) != NULL) {
    			
    			if(randnum == readline) {
    				
    				if(buff[0] == '#') { /* line is comment, ignore */
                                       readline = 1;
    					break;
    				} else if(buff[0] == '\n') { /* line is empty, ignore */
    					readline = 1;
    					break;
    				} else if(buff[0] == ' ') { /* line is probably empty, we'll ignore it */
    					readline = 1;
    					break;
    				} else {
    					
    					left = strtok(buff, "=");
    					right = strtok(NULL, " ");
    					
    					/* BUGGY */
    					if(readline != lines) {
    						right[strlen(right) -1] = '\0';
    					}
    					/* this loop works as it should */
    					while(1) {
    						printf("%s = ", left);
    						fgets(input, sizeof(input), stdin);
    						input[strlen(input) -1] = '\0';
    						if(strcmp(input,right) == 0) {
    							printf("Correct!\n");
    							break;
    						} else if(strcmp(input,"!") == 0) {
    							exit(0);
    						} else {
    							printf("Not correct!\n");
    							continue;
    						}
    					} /* end inner loop */
    
    				}
    			}
    ++readline;
    		} /* end middle loop */
    	} /* end outer loop  */

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    I'm saying this a lot at the moment, split the code into several functions.

    Code:
    done = 0;
    while( !done && fgets(buff, FILEBUFFER, file) != NULL)  {
      done = process_line( buff );
    }
    Then
    Code:
    int process_line ( char *buff ) {
      int done = 0;
      if ( buff[0] == '#' || buff[0] == '\n' ) {
        done = 1;
      } else {
        // do something
      }
      return done;
    }
    If 'something' is looking too complex, break it down some more
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    UNIX chick
    Join Date
    Mar 2003
    Posts
    92
    Thank you for your reply, Salem. It was a good idea to split up the function, it was definitely getting too large.. I created a function very similar to your which processes the line, and returns a value based on how the operation went. But I'm having difficulties managing to select the correct line in the file.

    I put this in the first function:
    Code:
    done = readline = 0;
    while(!done && fgets(buff, FILEBUFFER, file) != NULL) {
       randnum = returnRand(lines);
       if(readline == randnum) {
          done = processLine(buff);
       }
       ++readline;
    }
    This works sometimes, but it only does one sentence at the time, due to done (if I've understood it correctly). Also, sometimes it chooses the wrong sentence (one that returns 1), and hence doesn't do anything. The way I see it, again if I've understood it correctly, the problem is to generate a random line - over and over again.

    I hope this is making sense, it certainly doesn't inside my head.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > randnum = returnRand(lines);
    You used to call this once, before reading the file, and now you call it for every line in the file.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My loop within loop won't work
    By Ayreon in forum C Programming
    Replies: 3
    Last Post: 03-18-2009, 10:44 AM
  2. loop in a linked linked lists
    By kris.c in forum C Programming
    Replies: 6
    Last Post: 08-26-2006, 12:38 PM
  3. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  4. Newb needs help with loop issues
    By FatalError in forum C Programming
    Replies: 15
    Last Post: 09-04-2004, 02:19 PM
  5. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 03:45 PM