'cout' can handle numbers as well as text
For example,
cout << "2 + 2 = " << 2 + 2 << "\n";
Displays the text '2 + 2 = ', followed by the result of adding 2+2, then moves printing onto the next line. In other words, cout is smart enough to figure out how to print different types of data.
Gotcha!
There are a few things that the C/C++ programmer has to be aware of when using
arithmetic. Click here to see the program 'arith1.cpp'.
This program calculates the results of various arithmetic operations, and prints them
on the display. Have a look at the program, and compare it with the output it
produces, which is as follows:
2 + 2 = 4 4 / 2 = 2 4 * 2 = 8 5 - 2 = 4 but... 2 / 3 = 0
Hang on a minute: 2 divided by 3 is 0 ? That can't be right! What do you think is going on here? The answer is that 2 / 3 is zero, if we only allow the computer to use whole numbers. The nearest whole number below 2 / 3 is zero. Technically a whole number is usually referred to as an 'integer'. C++ will assume that you want to use integer arithmetic unless you tell it otherwise. Odd as it sounds, there is a good reason for this. All modern computers can easily work with fractional numbers ('real' numbers), but integers are much, much faster. Anyway, one way to tell the computer that you want to use fractional numbers is to write '2.0' rather than just '2'. The program arith2.cpp demonstrates this, and gives the 'right' answer to '2 / 3', which is 0.6666. The crucial difference is replacing the line
cout << "2 / 3 = " << 2 / 3 << "\n";
with
cout << "2 / 3 = " << 2.0 / 3.0 << "\n";
Exercise Write a program, or modify arith2.cpp, to work out 123 times 12. Check that you get the correct answer, which is 1476.
Gotcha (again)!
This one is even more peculiar. What happens if you execute this line of program?
cout << "123 * 456 = " << 123 * 456 << "\n";
The correct answer to 123 x 456, according to my calulator, is 56088. The result that the C++ program will give depends on the compiler! This is a real downer in software engineering terms: a standardized language like C++ should not give results that depend on the compiler used. However, the C++ standards do not define how much memory should be allocated to store an integer number. Borland C++ version 3 uses 16 bits, and gives the wrong answer. Borland C++ version 5 uses 32 bits, and gives the right answer. The result of 123 x 456 is simply too big to fit in a 16 bit number, and you don't get any warning of this. Of course, you can tell the compiler to use a 'bigger' number (i.e., 32 bits) to store the result, but the problem is that you have to tell it. It can't figure it out.
As before, you will get the right answer if you tell the computer to use real-number, rather than integer, arithmetic. That is, if you calculate '123.0 * 456.0', rather than '123 * 456'.
These peculiarities rarely cause problems for experienced programmers, but they are often a nuisance for beginners. As ever, plenty of practise will help.
©1994-2003 Kevin Boone, all rights reserved