Please type something, and press 'enter': (user enters 'cobblers') You entered 'cobblers'
There are two new concepts in 'cin1.cpp'. The first is the definition of a 'string' variable:
char Input [100];
This means that 'Input' is a text string (technically an array of characters) with up to 100 characters. The user's input will be stored in this variable.
The actual input from the user is accomplished by this line:
cin >> Input;
meaning: read information from the keyboard and store it in the variable 'Input'. Finally the program executes:
cout << "You entered '" << Input << "'\n";
which displays 'You entered ' followed by the user's input. Note that 'cout' can figure out the 'Input' is a text string, and act accordingly.
cin can handle different types of data
'cin' is smart enough (or, rather, the compiler is smart enough) to figure out how
to handle different types of data. For example, look at program cin2.cpp.
In this program, the user is expected to enter a number, which is then printed. The
crucial lines are
float n; cin >> n;
'n' is indicated to be a real-number variable, then user input is assigned to 'n'. A typical run of the program might be:
Please type a number, and press 'enter': (user enters 3.1415926535) You entered '3.1416'
If the user had entered something other than a number, 'n' would end up storing a nonsense value. There is a way to test whether this has happened; this will be described later.
Gotcha!
In C programming, the problem I am about to describe is probably
the single biggest cause of programs not
working properly. In the program 'cin1.cpp', the following line
char Input [100];
defined a text string with 100 characters. What would happen if the user entered more than 100 characters? It would be nice to think that the program would calmly limit the input to 100 characters, or perhaps display some sort of error message, or even stop. However, what is most likely to happen is that the program will crash dramatically and, if you're working on a PC, you may well end up rebooting it! Why does this terrible thing happen? The answer is that 'cin' does not know how long 'Input' is. This results from the way that C/C++ handles character strings. All that 'cin' knows about 'Input' is the location in the computer's memory of the start of the variable. If the user continues typing beyond the end of the space allocated to 'Input', the data entered will over-write whatever happens to be in memory at that point. The results will be totally unpredicatable.
In fact, if you allocate 100 characters for user input, the user can in fact only enter 99 characters, because C/C++ adds a 'null' character to the end to indicate the end of the string.
In C++ there is a far better way to handle text strings than to use a fixed-length array of characters as in cin1.cpp. All modern C++ compilers provide a variable-length string mechanism which is much easier and safer to use. Unfortunately, at the time of writing there is no standard method that works with all C++ compilers, so in this tutorial I have adopted the old-fashioned way of doing things.
Exercise: write a program that asks the user to enter two numbers, then adds them and prints the result. Don't worry about checking for errors at this stage.
©1994-2003 Kevin Boone, all rights reserved