7. A Sum of Numbers

A Bug

Let us rewrite the program we began this chapter with, which sums up five numbers, but this time by using a slightly different wording and the operator '+='. Please, type this program in your IDE from scratch, as it contains a few surprises.

#include <iostream>
using namespace std;

int main() 
{ 
    double sum=0;
    double inputValue;
    
    for (double i=1;i<=5;i++)
        cout << "Type in a summand: ";
        cin >> inputValue;
        sum += inputValue;

    cout << "The total sum is: " << sum << endl;
}

Should you execute this program, this is what you will get as the result:

Type in a summand: Type in a summand: Type in a summand: Type in a
summand: Type in a summand: 

This does not quite look right, does it? Why are all of these things being printed out? Nonetheless, let us try typing in a number, for example 44:

Type in a summand: Type in a summand: Type in a summand: Type in a
summand: Type in a summand: 44
The total sum is: 44

Well, this is definitely not a program that sums up five numbers. But, where did it all go wrong?

What are we to do now? Should we call the ghostbusters?

No, by all means do not call the ghostbusters; you should start the debugger instead!

Press the F10 key a few times to execute the program step by step. What is going on there, will you tell. It appears that only the first statement of the for loop is getting repeated - that is the reason why all of these things are getting printed out nonsensically. Why is only one statement in the loop being repeated?

It is because we have an error in the program: the braces are missing in this for loop!

Why does only the first statement get repeated? Well, if we have not written any braces then we are instructing the for loop to repeat only a single statement. What this means is that this whole program is a nifty little obfuscation, where the questionable part of the program could have been written more clearly, but still incorrectly like this:

    for (double i=1;i<=5;i++)
        cout << "Type in a summand: ";
    cin >> inputValue;
    sum += inputValue;

C++ is a free-form language and this makes the distribution of spaces before and after statements irrelevant to the meaning of a program. In case you did not know, the empty space before these statements is called indentation and it is normally typed in by pressing the Tab key.

As you have seen, the compiler did not spot this error. Why? Because the program was, in fact, syntactically valid. It just happened that this program does not do what we want it to do. This type of error, where a program "does not do what it should do" is called a logical error.

Logical errors are easiest to find using a debugger. Normally, the hardest part in correcting an error of this type is in actually finding it. Once we realize where the mistake has been made, it is usually not complicated to correct it.

After corrections, which we leave for you to implement, this program's output should look something like this:

Type in a summand: 3
Type in a summand: 2
Type in a summand: -5
Type in a summand: 8
Type in a summand: 11
The total sum is: 19