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 */