I have put two print statements into the program below and bold-faced them. You should put these print statements into your sum.cpp program as well:
#include <iostream>
using namespace std;
int main() {
int sum;
int value;
cout << "sum = " << sum << endl;
while (!cin.eof()) {
cin >> value;
sum += value;
cout << "value = " << value << " sum = " << sum << endl;
}
cout << "sum = " << sum << endl;
}
The first print statement, which prints sum, prints the values of important variables after initial housekeeping is performed. What should its value be?
The second print statement is at the end of the for loop and prints out the current values of the variables value and sum. These two variables are important variables in your computation and you need to know their values in order to effectively debug the program.
Compile your modified sum.cpp and run it.
Does the first print statement print the value you expect for sum? For some of you it's going to print an extremely large number, like 2825767. Even if the print statement printed 0, like you would expect, let's say that it instead prints a very large number for sum. That should be an enormous tip-off as to where the first error lies. It must lie before this print statement, and hence must lie somewhere amongst the variable declarations. Can you figure out what went wrong? Take a moment to think about it. If you can't figure it out, the TA will tell you. Make the correction and re-compile your program.
Now that the first bug is corrected, your program should be printing 150 as the sum. Obviously that is incorrect. If you correctly implemented your print statements, your program should not be generating output that looks like:
sum = 0 value = 20 sum = 20 value = 30 sum = 50 value = 50 sum = 100 value = 50 sum = 150 sum = 150What looks wrong with this output? Think about it for a moment. If you don't see it, then the TA will tell you.
Once you have identified the problem, you need to figure out why the problem is occurring. Print statements are not going to help you anymore. You need to remember that we told you during lecture that cin.eof() is re-active, not pro-active. That means that even after the input appears to be exhausted once 50 has been read, cin.eof() does not know that the input is exhausted. It will return true and your loop will get entered one last time. This time the cin statement fails and when it fails, it leaves value unchanged. That means it still has the value 50 and sum gets incremented from 100 to 150.
Note that debugging statements will only get you so far in debugging the program. Eventually you are going to need to use your knowledge of C++ to identify the source of the problem and resolve it. Do you remember the right way we told you to write such a loop in class? Think about it. If you are having trouble, go the the Input notes and check out the section entitled "Four common cin errors: #2 -- cin.eof() is not proactive!".
Fix the program, re-compile it, and now the output should be correct. The program sum1.cpp shows the corrected program.