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)
Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953