wpe41.gif (23084 bytes)CIS3355: Business Data Structures
Fall, 2008
 

Why must we convert data entered from the keyboard?

 

It’s no surprise that when we type something on a keyboard, it must be converted in order for the machine to understand it.  When we type on the keyboard, we are entering characters. If those characters are meant to be integers, we have to convert them. When entering numbers from the keyboard we are actually entering numeric values which are stored as char.

 

How do we do it?

 

First off, since there is no input/output built-in, C++ uses a function called a stream to perform input operations on the keyboard, which is usually the standard input device. A stream is an object where a program can either insert or extract characters to/from it. These characters may be 1 byte or 2 bites.

Text Box: #include <iostream>

 

 

The # sign included in this line of code tells the preprocessor to include the iostream standard file. The file includes the declaration of basic standard input/output library. It is always found at the beginning of a program. By including the iostream library, certain objects are created in memory to handle the input (keyboard) and output (monitor) operations. The standard input stream is known as cin while the output stream is cout.

 

The standard input in C++ is done by using the operator of extraction (>>) on the cin stream.  Cin is one of four types of streams that are predefined.  The other 3 are cout, cerr (std. destination for error messages), and clog (same as cerr, but buffered).  The operator must be followed by the variable that will store the data that is going to be extracted from the stream.

Text Box: int zipcode;
cin >> zipcode; zipcode;
 
 

 

 

The first line of code declares the integer as zipcode. The next line tells the program that the input will come from the keyboard. In order for the input to be processed, the return key needs to be pressed. Always remember the type of variable declared; if you declared an integer, you get an integer, and so on. Basically you will have to trust the user that they enter the correct data type.

 

In order to store numbers in data types other than char, we must convert them. With this conversion, arithmetic operations are now possible. Although some of the functions provided by C++ do this for us, it is a good idea to know how it is done.  Think about the number ‘123’. If we were to look into RAM, we would find  

 

 

‘123’ is stored on three contiguous bytes in binary form. What we want to do is have ‘123’ converted to the integer 123 which is stored on two-bytes – 1 bit for the sign and the rest for the value.

 

First thing we would have to do is identify if the characters entered on the keyboard are just that or numbers. Identifying whether they are or are not is pretty easy to figure out. If the character value is between 48 and 57, they are meant to be numeric values.  Also important to consider is the position of the value. For example, assume that the string consisted only of the character ‘7’. The corresponding numeric value would also be 7. If the string consisted of the characters ‘56’, however, the corresponding value would be equal to (5 * 10) + 6, or 56.

 

The following simple program converts;

Text Box: #include <stdio.h>
void main()
{ char nstring[] = “123”; 
int num = 0, 
offset = 0; 
while ((nstring[offset] >= '0') && (nstring[offset] <= '9')) 
  num = num * 10 + (nstring[offset++] - '0');

 

 

 

 

 


 

The stdlib.h library is another standard file that provides three useful functions for the purpose of converting strings to integers (along with other data types) All of these functions admit one parameter and return a value of the requested type.

  • atoi: converts string to int type.
  • atol: converts string to long type.
  • atof: converts string to float type.

The 3rd line explains the string which is going to be converted. Next line declares that the variable num will store the integer = nstring followed by offset of our array. The next line considers whether each character it is in the range we are specifying. Last line goes thru a loop like this

 

 

This conversion has to occur every time it is necessary, but you don’t always have to write the code.

 

Some good references: (They helped make this tutorial possible.)

 

1. Cplusplus.com: Basic Input/Output

2. C++ Input/Output

3. Basic Input-Output Statements

4. Basic Input/Output

5. String of Characters

6. cprogramming.com: FAQ

7. ZeusCMD: Tutorial - Strings

 

At this point in time, you should be able to answer the following questions:

 

1. What is the usual standard input device?

            a. Monitor

            b. Keyboard

            c. Mouse

            d. Modem

[b.Keyboard]

 

2. Explain what is going in this line of code:

           char city;

           cin >> city;

[Characters are being declared as city, input will come from the keyboard]

 

3. What does including the iostream library accomplish? Where does one see it declared?

[It creates certain objects in memory to handle input and output. At the beginning.]

 

4. What is the first step in converting a string into an integer?

 

 [Identifying which characters are numbers]

 

5. .atoi, atof and atol are included in what standard library file?

            a. iostream

            b. stdlib.h

            c. #include

            d. cout

[b.stdlib.h]