0

I'm having some issues debugging my program from Problem Set 5: Mispellings.

Well after struggling a couple of hours, here's some background: 1. My check function seems to work just fine, as well as the load and unload. When I execute diff -y student.txt staff.txt at the command line, all the fields from my output are the same as the staff's output (except TIME IN TOTAL). Everything else matches perfectly (WORDS MISSPELLED, WORDS IN DICTIONARY, WORDS IN TEXT).

The issue is when I run check50 on my program, check50 fails entirely. But if I declare the global variable that counts the number of words in my dictionary like int number_words = -1; check50 passes "perfectly", even though declaring it as an <int> instead of as a unsigned int seems to be a bad idea. If you would like you can take a look at my code at: ("...when asking for help, you may show your code to others, but you may not view theirs...").

Thanks for you time. Greetings.

4

1 Answer 1

1

"Ready, Fire, Aim!" You indeed do have an off-by-one error because of the way you handle the end of file condition while processing the dictionary.

Look at your while loop construction. The while loop test checks for EOF detected. If not, start the loop. The loop will then read the next line, process the word that was read, increment the word count, and go back to the while loop test.

The problem is that the EOF condition isn't detected when the last word is read. It is detected on the very next pass through the loop. The flow is something like this. The last word is read and processed, and the loop goes back up to the test for EOF. Unfortunately, the EOF has yet to be detected. In fact, it will be the very next byte read. So, the loop begins to execute one more time, the read from the dictionary executes and the EOF is detected, something is processed as a word (either the last word again, or garbage data, not sure which), and the counter is incremented. Now, the code goes back to the while loop test and the EOF is finally detected.

In short, one extra, nonexistent word is being processed because the code reads from the input file and processes the data before actually checking to make sure EOF wasn't detected.

A more effective technique is to incorporate the read into the while statement and check the return value from the read as the test condition.

A long explanation for a common error. If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

1
  • After adding an if statement that detects EOF, my program passes check50 without any hassle! Thank you so much for your good explanation and quick response! Have a great day!
    – user11236
    Commented Apr 7, 2016 at 22:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .