C++ Data Type Usage

This is an update of a post that originally appeared on October 16, 2015.

Originally I provided this post to correct an example in a previous edition of the book. Now I’m providing it to clarify that same example to a greater degree and answer reader input. The Going Overboard section on page 64 of C++ All-In-One for Dummies, 4th Edition talks about the problems that can occur when you try to stuff a number that’s too large into a specific data type. The problem with the example shown:

cout << 12345678 * 100 / 2 * 3 * 3 << endl;

is that while it does display a warning message, the warning really doesn’t get the point across. In order to see the example as originally intended, you need to change the code to read:

long MyLong = 12345678 * 100 / 2 * 3 * 3;
cout << MyLong << endl;

The code will now produce a warning and you can see why in a clearer way, just as described in the book, because the data type isn’t ambiguous any longer. In both cases you see a warning message of:

warning: integer overflow in expression of type 'int' results in '1260587804'

One of the ways to overcome this problem is to ensure that you use the correct sized variable in the first place. The following code doesn’t produce a warning message because of the use of auto (telling the compiler to choose the correct variable type automatically) and ll (telling the compiler to use a long long variable for the calculation).

auto AutoSize = 12345678ll * 100 / 2 * 3 * 3;
cout << AutoSize << endl;

A number of readers were happy that I pointed the problem out, but wanted to see a fix for the problem as well. When you run the example with the additional code, you see outputs of:

1260587804
1260587804
5555555100

Only the third answer is the correct one and it points out the need to pay attention to both warnings and errors as you code. Please let me know if you have any questions or concerns about this example at [email protected].

Review of Math for the Zombie Apocalypse

Making learning fun is something every author struggles with and few authors achieve. Math for the Zombie Apocalypse is one of the few books out there that actually make a mundane topic like mathematics fun. The essential content of this book is the same as the content for any beginning math book you have ever read. There is no way to get around the requirement of having to learn addition, subtraction, multiplication, and division. However, this book accomplishes its task with panache.

The reader is instantly engaged in a favorite topic of children today, avoiding zombies. Of course, it’s one thing to say that you want to avoid zombies, but it’s quite another to actually accomplish the task. Throughout the book, the reader is asked how he or she would prove their mettle against hoards of zombies roaming the land. The answer is to use math to figure out how to stay alive while less skilled acquaintances become zombies themselves.

Of course, the book is meant entirely in fun. The humor is grand and of the sort that children will enjoy immensely. However, the result of reading the book is that a child sees a useful purpose in learning math—even though this purpose is quite fictional in nature. Most math books out there are dry, humorless tomes filled with mind numbing repetition that will lull the most stalwart child to sleep. There is no reason that a child can’t learn new skills in a fun-filled environment. Before the reader realizes it, he or she has learned new and useful skills.

Fortunately, this isn’t the only book the author intends to write. You’ll want to wait to see the new additions to the for the Apocalypse series, but for now, make sure you check out Math for the Zombie Apocalypse, especially if you have a child that is having a hard time learning the basics. This is the sort of book that I wish had been available when I was growing up and one that I hope others see as being a valuable way to get kids interested in an essential topic. The press, teachers, parents, and even a few students complain about the low scores children achieve in basic math today, but this book does something about the problem.