So far you have been doing arithmetic and printing the results on the screen. The only way in which you have been able to change the numbers is to alter the program and then recompile it. It would be nice to alter the numbers while the program is running.
Numbers are entered into a program using the instruction cin. This works the same way as cout, except that it provides the numbers rather than accepting them:
// This program accepts two numbers and adds them together
#include <iostream.h>
void main ()
{ int first_num, second_num, result;
cout << "Please enter two numbers to add together."
<< endl
<< "Please enter the first number ";
cin >> first_num;
cout << "Please enter the second number ";
cin >> second_num;
result = first_num + second_num;
cout << endl << "The sum of the numbers is "
<< result;
}
This is the only thing that I have shown which approximates to a useful program. Right, we had better go through this bit by bit.
The instruction cin waits until the user types something and presses Enter. Whatever has been typed is passed to either first_num or second_num depending on which statement it is. This had better be a whole number - those variables are both integers, and if it is anything that can't be stored as an int, the program is going to complain and crash!
You will notice that >> passes items from left to right, just as << passes it from right to left. Common sense indicates that you should be able to do input like this:
first_num << cin;
Yes, I thought so too, but I tried it and C++ doesn't like it. Well, what can I say - it's a stupid language!
The program listed above only works properly if you type whole numbers when you are prompted. If you type anything else (such as a decimal or a string) then the program will fail. Exactly what the program will do depends on which version of C++ you are using and exactly what you entered. It might:
These errors are called Run Time Errors as they only happen when you run the program. You can't blame the compiler for not picking them up as it didn't know what you were going to type when it was translating the program. Errors which the compiler does identify are called Compile Time Errors. Here's another example. I have changed the program above so that it divides the first number by the second:
// This program accepts two numbers and adds them together
#include <iostream.h>
void main ()
{ int first_num, second_num;
int result, remainder;
cout << "Please enter two numbers to divide."
<< endl
<< "Please enter the first number ";
cin >> first_num;
cout << "Please enter the second number ";
cin >> second_num;
result = first_num / second_num;
remainder = first_num % second_num;
cout << endl << "The quotient of the numbers is "
<< result << endl << endl
<< "The remainder of the division is "
<< remainder;
}
You'll notice a few improvements. I have put in a calculation to give the remainder as well as the quotient. If you enter numbers such as 19 and 7 when prompted, then all goes well:
Please enter two numbers to divide.
Please enter the first number 19
Please enter the second number 7
The quotient of the numbers is 2
The remainder of the numbers is 5
The problem occurs when you enter 0 as the second number. If you divide any number by 0, any computer or calculator will give an error. In the C++ system that I use (Borland C++ version 4.5), the following error message pops up:

So far, the variables that we have been dealing with have been numbers, but what if you want to store characters and strings as variables? Storing characters is easy. There is a data type called char which can hold single characters:
#include <iostream.h>
void main ()
{ char my_initial;
cout << "What is your middle initial? ";
cin >> my_initial;
cout << "You typed " << my_initial << endl;
}
Things are a little harder if you want to store a string. Standard C++ has no 'string' type, although there may be one in the version of C++ that you are using. Strings are stored as arrays of characters - see a later chapter.
The char data type is intended to store characters. However, it stores these characters as numbers in the range -127 to 128. For instance, the letter A is stored as 65, B as 66, lower case 'a' as 97, space as 32 etc.
This means that char can be used simply to store numbers just as int would be, as long as you restrict yourself to numbers between -127 and 128 inclusive. You can use char variables in calculations willy-nilly, but be prepared for the fact that if you try to display a char variable, you will get the character rather than the number!
#include <iostream.h>
void main ()
{ char a,b;
int x;
// The following line sets 'a' to character M
a = 77;
b = a + 1; // 'b' becomes 78
cout << b << endl; // Displays N, not 78
x = b;
cout << x << endl; // Displays 78, as x isn't char
}
In the program above, the char variable b is displayed as N, even though it got its value through arithmetic. However, x is an int variable, so it is displayed as a number.
So far you have met comments that start with //. The compiler ignores anything that comes after this up to the end of the line. I have put a comment line at the start of most programs, but you can put them on lines with program statements if you want:
#include <iostream.h> // To enable input & output
void main () // This is the start
{ char my_initial; // Declaration of variable
cout << "What is your middle initial? ";
cin >> my_initial; // Get user to type initial
cout << "You typed " << my_initial << endl;
} // This marks the end of the program
There is another way of doing comments, which is to put them between symbols like this /* and */. In this case, you can put program code after the comment on the same line, because the comment ends with */. Here I have adapted the program above in a very silly way, putting comments in the middle of the code:
#include <iostream.h>
void main ()
{ char /* Hello! */ my_initial;
cout << "What is your middle initial? ";
cin >> /* It's me again! */ my_initial;
cout << "You typed " << my_initial << endl
/* Goodbye! */;
}