-
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 */
-
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
-
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. :cool:
-
> randnum = returnRand(lines);
You used to call this once, before reading the file, and now you call it for every line in the file.