Calculating Reading Score with a C Program

  • Thread starter Thread starter akieft
  • Start date Start date
  • Tags Tags
    Program Reading
Click For Summary

Discussion Overview

The discussion revolves around a C program intended to read text from a file and calculate the number of words, vowels, and sentences to derive a reading score. Participants are addressing issues related to the program's functionality, compiler errors, and code structure.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related
  • Mathematical reasoning

Main Points Raised

  • One participant describes their program's goal and shares their code, indicating that it does not work as intended.
  • Another participant questions the specifics of the error message and asks for clarification on what the program is supposed to do.
  • There is a suggestion that the compiler installation might be the issue, rather than the code itself.
  • Concerns are raised about the use of fgets, which only reads one line, and whether a different function like fscanf might be more appropriate.
  • Participants suggest using a single loop to count vowels, words, and sentences instead of separate loops for each.
  • There are recommendations to include print statements for debugging purposes to track what is being read from the file.
  • One participant proposes using a while loop to read multiple lines from the file, indicating that fgets returns NULL at the end of the file.
  • Another participant points out that the current word counting logic may not correctly account for words at the end of sentences.
  • There is discussion about restructuring the conditional statements for determining the reading score.

Areas of Agreement / Disagreement

Participants express various opinions on the code's structure and functionality, with no consensus reached on the best approach to resolve the issues presented. Multiple competing views on how to handle the reading and counting logic remain evident.

Contextual Notes

There are unresolved issues regarding the handling of file input, the counting logic for words, and the proper installation of the compiler. Specific assumptions about the input file's format and content are not fully addressed.

  • #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 36 ·
2
Replies
36
Views
3K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 6 ·
Replies
6
Views
6K
Replies
1
Views
2K
Replies
5
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
Replies
3
Views
2K
  • · Replies 22 ·
Replies
22
Views
6K