A Note About Arithmetic Overflow

In Fig. 4.7, line 54

total = total + grade; // add grade to total

added each grade entered by the user to the total. Even this simple statement has a potential problem—adding the integers could result in a value that’s too large to store in an int variable. This is known as arithmetic overflow and causes undefined behavior, which can lead to unintended results (en.wikipedia.org/wiki/Integer_overflow#Security_ramifications). Figure 2.5’s addition program had the same issue in line 19, which calculated the sum of two int values entered by the user:

sum = number1 + number2; // add the numbers; store result in sum

The maximum and minimum values that can be stored in an int variable are represented by the constants ...

Get C++11 for Programmers, Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.