Keyboard Input


You can now display text to the screen. It is also a good idea to get data in from the user. Fortunately, C++ makes that just as easy as output. In fact, it is done in a similar manner. Just as output is handled with the cout command, input is handled with the cin command. There are really only two differences. The first difference is that the arrows point into the program >>. (That’s logical, isn’t it?) The second difference is that the arrows must point to some variable. This makes sense if you think about it. If you are going to take input from the keyboard, you have to put it somewhere. An example follows.

Example 2.6

Step 1: Type the following code into your favorite text editor.

#include <iostream> using std::cout;   using std::cin; int main() {   // variable declarations   int number1, number2, product, sum;   // get the users input   cout<<"Please enter an integer (whole number) \n";   cin >> number1;   cout<< "Please enter another integer (whole number)  \n";   cin >> number2;   // compute the answers   product = number1 * number2;   sum = number1 + number2;   // print out the answers   cout << "The sum of " << number1 <<"  and " <<  number2 << " is " << sum <<"\n";   cout << "The product of " << number1 <<"  and " <<  number2 << " is " << product <<"\n";         return 0; }

Step 2: Compile the code by typing in bcc32 example02-06.cpp at the command line.

Step 3: Run the program by typing in example02-06. You should see an image much like the one displayed in Figure 2.6.

click to expand
Figure 2.6: Input/Output and basic operators.

There are a number of items in this program that you should be aware of. The first is the use of the cin command. After declaring integer variables to hold the input, it is also necessary to use the command cin >> number1. That expression literally takes the input from the keyboard and places it into the variable number1. In this code you see the same thing done with the variable number2. Also notice that when you take in input you don’t need to do anything to tell the compiler what kind of variable you want to put the data in. The cin command can see what kind of variable is to the right of the >> sign, and will convert the data to the proper type, if possible. Obviously if you try to put a into an int, you will have a problem.

Watchout!

Make sure that the variable you use for input matches the type of data you asked the user to enter. Do not ask for a decimal value then try and put that into an integer variable!

The next item that you should direct your attention to is the use of compound cout statements. You can output just about as much as you like with the cout command, in a single statement. All you must do is to separate the items you wish to output with << characters. Using this technique, you will be able to combine variables and text to form the output to the screen that displayed the answers. This is an important technique to remember. You will see it used many times in this book.

These simple examples demonstrate the basics of C++ input and output. With just these simple techniques you can handle input from the keyboard as well as output to the monitor screen.

Other Input Options

As you have already seen, the cin operator allows you to input data from the keyboard. However, it is not the only method or necessarily the best for all situations (but it is the simplest). The two other most commonly used methods are get and getline. The get function will retrieve a single character, no matter how much the user types in. The getline function will retrieve a certain number of characters, a number that you specify. This is particularly important when putting the data into an array.

Hint!

Arrays are covered in detail in Chapter 3. For now you should simply think of an array as a string of variables, as you saw with the character array, buffer.

Simply using the cin will allow the user to try and put more characters into the array than it can hold. This is referred to as overflowing. The getline function allows you to specify how many bytes you will get from the user’s input. Each character the user types takes up one byte. So if, in the getline function, you specify four bytes, and the user types in the word computer, you will only retrieve comp, the first four letters. In the following example you will see the getline function used with an array.

Example 2.7

Step 1: Write the following code into your favorite text editor.

#include <iostream> using std::cin; using std::cout; int main() {  char text[10];  cout << "Please enter a word\n";  cin.getline(text,10);  cout << text << endl;  return 0; }

Step 2: Compile and run the program.

Step 3: Type in more than 10 characters. You should see something like what is depicted in Figure 2.7.

click to expand
Figure 2.7: The getline function.

You should notice that only 10 characters are placed in the character array, no matter how many you enter. In fact, you should probably prove this to yourself by deliberately typing in more text. This means that getline is the safe choice if you want to enter a string of characters into an array.




C++ Programming Fundamentals
C++ Programming Fundamentals (Cyberrookies)
ISBN: 1584502371
EAN: 2147483647
Year: 2005
Pages: 197
Authors: Chuck Easttom

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net