I thought I was finished with this program but now I can’t find a way to make it quit. The loop continues even when I enter the terminating character. I’ll post he whole thing. The answer may be simple but I'm just not seeing it.

do{
	  do{
			system("cls");
			printf("Enter the name and path of a .TXT file(type 1 to end):\n");
			fflush(stdout); 
			fgets(fname, 128, stdin);
		
		 if((s = strchr(fname,'\n')) != NULL) *s = '\0';/* remove newline char */ 
		 
		fp=fopen(fname,"r");

		if(fp==NULL) 
			{
			printf("Cannot open file.\n");
           	
				system("pause");
			continue;
			}
			else
		{
			         int charctr=0;/*Stores character count*/

				while ((c = fgetc(fp)) != EOF)
				{

	  				++charctr;/*Chartacter counter*/
					
				}
				rewind(fp); /*Takes file back to the begining*/
				nlines=linecount(fp);
				if (nlines>30)
				{
					printf("Invalid!!!Lines cannot be more that 30\n\n");
					system("pause");
			
				continue;
				}
				
				rewind(fp);
				avgchar=averagechar(charctr,nlines);
				if(avgchar>60)
				{
					printf("Invalid!!!Average number of characters per line must be less than 60 \n\n");
					system("pause");
							
				continue;
				}
			
				rewind(fp); 
				max=longest(fp);
				rewind(fp);
				min=shortest(fp);
				rewind(fp);
				w=words(fp);
				if(w>500)
				{
					
					printf("Invalid!!!Words must be less than 500 \n\n");
					system("pause");
			
				continue;
				 }
				break;
		
				rewind(fp);
				sentence=sentences(fp);
				rewind(fp);
				question=percentage(fp,sentence);
				rewind(fp);
				quote=quotation(fp);
				rewind(fp);
	
			fclose(fp);

			 fp=fopen("summaryData.txt","a");
			 fprintf(fp,"\n***Filename:%s***\n", fname);/*Print to file*/
			 fclose(fp);

			printf("Enter 1 to quit, 2 to continue>");
			scanf("%d",&quit2);
			quit = quit2;


      }/*end else*/
		
	}while(1);/*Everything is true*/
   }while(quit != 1);
return 0;
}

Recommended Answers

All 8 Replies

Does your code look this neat to you?

do {
        do {
            system("cls");
            printf("Enter the name and path of a .TXT file(type 1 to end):\n");
            fflush(stdout);
            fgets(fname, 128, stdin);

            if ((s = strchr(fname, '\n')) != NULL)
                *s = '\0';      /* remove newline char */

            fp = fopen(fname, "r");

            if (fp == NULL) {
                printf("Cannot open file.\n");
                system("pause");
                continue;
            } else {
                int charctr = 0;  /*Stores character count */

                while ((c = fgetc(fp)) != EOF) {
                    ++charctr;  /*Chartacter counter */
                }

                rewind(fp);     /*Takes file back to the begining */
                nlines = linecount(fp);
                if (nlines > 30) {
                    printf("Invalid!!!Lines cannot be more that 30\n\n");
                    system("pause");
                    continue;
                }

                rewind(fp);
                avgchar = averagechar(charctr, nlines);
                if (avgchar > 60) {
                    printf("Invalid!!!Average number of characters per line must be less than 60 \n\n");
                    system("pause");
                    continue;
                }

                rewind(fp);
                max = longest(fp);
                rewind(fp);
                min = shortest(fp);
                rewind(fp);
                w = words(fp);
                if (w > 500) {
                    printf("Invalid!!!Words must be less than 500 \n\n");
                    system("pause");
                    continue;
                }
                break;
//!! The rest of this code is unreachable - what's it for?
                rewind(fp);
                sentence = sentences(fp);
                rewind(fp);
                question = percentage(fp, sentence);
                rewind(fp);
                quote = quotation(fp);
                rewind(fp);

                fclose(fp);

                fp = fopen("summaryData.txt", "a");
                fprintf(fp, "\n***Filename:%s***\n", fname);  /*Print to file */
                fclose(fp);

                printf("Enter 1 to quit, 2 to continue>");
                scanf("%d", &quit2);
                quit = quit2;
            }                   /*end else */
        } while (1);            /*Everything is true */
    } while (quit != 1);

If not, then the FIRST thing you must learn is how to format code properly. Because finding your way out of a huge mass of chaotically indented code is impossible.

The second point is that this code is ~100 lines long.
Use some functions to simplify the code so you can separate the decision logic from the actual bits of work.

The third point is that half the code is unreachable (see comment)

The closest thing I can figure out is
1) you need to use consitant indenting so someone can actually follow the code easier
2) quit is never set to 1 followed by break ing out of the inner endless loop.

I already have about eight or so functions the results are supposed to be printed to to a logfile and to screen but I took out the commands to make it simpler when you say unreachable what do you mean?

As for setting quit to 1 does this mean I should break after everthing is found to be true?

As for setting quit to 1 does this mean I should break after everthing is found to be true?

I'd assume you want to break out when someone enters the command to QUIT.

just make goto quit and label quit as exit(0);

commented: 2 years late, didn't read the thread, suggested using 'goto' as the way out - grade:F -6

i didnt also read hole code because its indent is little bit bad but here where u scanf if u wanna exit or not you should have shown a test with if something like this
if(quit==1) {return 0;}
else continue;

commented: stop now, while you're still behind -2
scanf("%d", &quit2);
                quit = quit2;
            }                   /*end else */
        } while (quit != 1);    ///////Change this me thinks/////////
    } while (quit != 1);                 Then you won't have a forever loop

Why is there two loops?

(1) this thread was dead and buried, until "lolguy" dug up the bloated corpse to defile it even further

(2) the original code is an example of what not to do. don't spend any time thinking about it.

commented: Kicking a dead horse two days after it died a second death. -3
commented: Not excessive +15
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.