You can use cout for more than just printing out silly phrases, you know:
\\ This program does a bit of arithmetic!
#include <iostream.h>
void main ()
{
cout << 3 + 4;
}
I think you can probably guess what is going to appear on the screen. That's right:
7
The thing passed to cout is called an expression. An expression is something that produces a number or a string. Numeric expressions produce numbers (which can be positive or negative, whole numbers or decimals or even 0), whereas string expressions produce strings. You met string expressions in the last section with things like this:
"Hello" << endl << "Goodbye"
cout can cope with all expressions - numeric or string, cout isn't fussy. It just prints out whatever it is given. While were are on the subject of expressions, what would this display?
\\ This program doesn't do a bit of arithmetic!
#include <iostream.h>
void main ()
{
cout << "3 + 4";
}
Superficially almost identical to the previous program, except this time the result is quite different:
3 + 4
Because the 3 + 4 is included in double quotation marks, cout treats it as though it were a string. Strings can include characters such as "3" and "4", but they are not treated as numbers, merely as "letters" that happen to be shaped as numbers. Any expression that doesn't have double quotation marks round it is treated as a numeric expression and C++ will evaluate it (work it out) to produce an answer. That's what it displays on the screen.
In virtually all computer languages, multiplication is written not with a letter x but with a star, *. Similarly, division is written using the 'forward slash' symbol, /. Here are some numeric expressions involving multiplication and division:
3.1 * 4.9 -200 / 5 3 + 6 * 2.005 + 10 / 7.331
If you want to find out what those expressions produce, try a program like this:
\\ This program does a bit of arithmetic!
#include <iostream.h>
void main ()
{
cout << 3 + 6 * 2.005 + 10 / 7.331;
}
As I mentioned in an earlier section, C++ is very tolerant of different layouts, so you can put as many spaces or line breaks in the expressions as you like (within reason, of course - don't try to split a number such as 2.005 by putting spaces or line breaks in the middle of it).
You have to be very careful with the / operator as it can produce unexpected results. / can either carry out integer division (i.e. producing a whole number answer) if it divides one whole number by another, or floating point division if it divides two decimal values. Here's an example:
#include <iostream.h>
void main ()
{ float answer;
answer = 10 / 3;
cout << "The result of 10 divided by 3 is "
<< answer << endl;
}
Common sense indicates that this program should produce 3.33333, which is what a calculator would give you. However, because both 10 and 3 are whole numbers, the answer to the division is 3 (with the decimal part removed). It doesn't make any difference that answer is declared to be a floating point variable, the division still produces a whole number answer.
The way to get round the problem is to make sure that at least one of the figures that the division is to operate on is floating point. That way, the division is forced to produce a floating point answer. Possible solutions include these:
answer = 10.0 / 3; answer = 10 / 3.0; answer = 10.0 / 3.0; answer = (10 + 0.0) / 3.0;
Whoops, I used a variable there where I shouldn't have done - I haven't taught you about those yet. Naughty me! Almost let the cat out of the bag. You will learn about those in the next section.
Er, that's something to do with telephone switchboards, right? No! It's to do with the order in which C++ carries out mathematical tasks. There is a pecking order which goes like this:
Let me explain ...
\\ This program does a bit of arithmetic!
#include <iostream.h>
void main ()
{
cout << 2 + 3 * 5;
}
This program should add 2 to 3 (producing 5) and then multiply the result by 5 giving 25, right? Wrong! Multiplication is more important than addition, even though the addition comes before the multiplication in the expression, and so C++ multiplies 3 by 5 first and then adds 2:
17
Brackets mean "Do me first". If you wanted the addition to be done first, then you should put the 2 + 3 part in brackets, like this:
\\ This program does a bit of arithmetic!
#include <iostream.h>
void main ()
{
cout << (2 + 3) * 5;
}
This really does produce 25. You can put pairs of brackets inside other pairs of brackets:
\\ This program does a bit of arithmetic!
#include <iostream.h>
void main ()
{
cout << ((12 + 8) / 5 + 7) * 3;
}
This first adds 12 to 8 (making 20), then divides it by 5 (making 4), then adds 7 (making 11) and finally multiplying by 3, giving
33
Actually, the << operator is also included in the operator precedence league table. It is below addition and subtraction in the pecking order. That is why C++ works out all the arithmetic before passing the result to cout. It also means that you can mix arithmetic and strings in programs such as the following:
\\ This program does a bit of arithmetic!
#include <iostream.h>
void main ()
{
cout << "The product of 2.1 and -3.45 is "
<< 2.1 * -3.45;
}
This produces the following result on the screen:
The product of 2.1 and -3.45 is -7.245