Calculating Reading Score with a C Program

  • Thread starter Thread starter akieft
  • Start date Start date
  • Tags Tags
    Program Reading
Click For Summary
A user is developing a C program to read text from a file and calculate the number of words, vowels, and sentences to derive a reading score. They are encountering issues with their code, including a "qcc.exe installation error" and problems with counting due to using `fgets`, which only reads one line at a time. Suggestions from other users include using a while loop to read multiple lines and combining the counting of vowels, words, and sentences into a single loop for efficiency. The user is also advised to check for file opening errors and to ensure proper formatting in their code. The discussion highlights common programming challenges and debugging techniques for beginners.
  • #31
akieft said:
no the installation error is fixed. Basically the program runs, the output is ok1 and then quincy pops up an error saying that this program has quit unexpectedly and you have the option to close, debug, or send error to microsoft. If you click debug its just a bunch of numbers and I have no idea what they mean. I have a feeling it has to do with the while loop but I am unsure how to fix it.

When you see that error, it usually means you have committed a "pointer" error. That means you are trying to use a piece of memory which you think contains data, but which actually "does not exist".

The only place I see in your code where you could have committed such an error is with your FILE *input variable. It is possible, if fopen fails, that it could return NULL. Then when you say fgets(...input) later, fgets will try to read from NULL (bad) and crash.

Try adding the line

perror("fopen");

Right after you say fopen (this will cause fopen's exact error message to be printed, if there is one), or changing your printf right there to

printf("ok %x\n", input); /*if fopen succeeded, this will print a nonzero number, if it failed it will print "ok 0"*/

(If you want to be really fancy you could to an if() test to see whether "input" is equal to 0, and if input is equal to 0 print "Beatles.txt not found!" and politely quit. Your teacher would probably prefer this. But this will not solve the problem that your program doesn't work, it will just make it not work in a more polite way...)

Most compilers come with a "debugger", which when you commit a pointer error they will stop the program in its tracks and show you the source code of the line where the error happened. It is essential you learn to use a debugger someday. Does QCC not come with one?
 
Last edited:
Technology news on Phys.org
  • #32
not that we have been taught no...Our prof has never shown us a debugger to my knowledge neither has our TA
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
Replies
1
Views
2K
  • · Replies 6 ·
Replies
6
Views
6K
Replies
5
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
Replies
3
Views
2K
  • · Replies 22 ·
Replies
22
Views
6K
Replies
8
Views
3K