Simple Input and Output

 < Day Day Up > 



C++ provides character stream input and output in the form of the cin and cout objects. You have already seen the stream insertion operator in use to send variable values and strings to the cout object. My objective in this book is to limit the use of cin and cout to the absolute minimum required to facilitate program interaction. I do this because there exist several good iostream books on the market that go into much greater detail than I wish to do so in this book.

My second reason for limiting my coverage is that should you decide to specialize in a particular operating system you will need to learn how to use the graphical user interface components like windows, text boxes, text fields, etc., supported by that operating system.

cin

The iostream header file declares the cin object for your console stream input use. The cin object takes input from the standard input device, which is usually the keyboard, and directs it to designated variables. Example 5.9 shows the cin object being used to read in several integer values and direct the input to the integer variables a, b, and c:

Listing 5.9: Using cin Object to ReadInteger Values from Keyboard

start example
 1  #include <iostream>  2  3  using namespace std;  //introduces namespace std  4  5  int main(){  6     int a = 0, b = 0, c = 0;  7  8     cout<<"Enter values for a, b, and c: ";  9     cin>>a>>b>>c; 10     cout<<a<<" "<<b<<" "<<c<<endl; 11 12     return 0; 13  }
end example

Notice the way the >>’s point when using the cin object vs. the cout object. The >> is called the stream extraction operator and the << is called the stream insertion operator.

Trapping Bad Input

A problem arises when using the cin object to read input from the keyboard. If you are expecting a certain type, say, an integer, but the user enters a character or string of characters, it causes the extraction operation to fail and cin’s internal fail bit will be set. What the user sees when this happens is a screen scrolling wildly out of control; the program just crashed.

A simple solution, until you learn more robust error trapping techniques, is to test for the success of the cin stream extraction. Example 5.10 offers an example.

Example 5.10: Testing for Valid Input

start example

click to expand

end example

Example 5.10 shows the cin object being tested for success or failure in the expression section of a while loop. If the extraction operation was successful, which in most cases means the correct type was extracted from the input stream and stored in the designated variable, then the while loop is skipped. If the extraction operation failed then the body of the while loop is entered and will loop until the extraction operation is a success.

cout

The cout object sends stream output to the standard output device, usually the screen. You have seen the cout object in action throughout this chapter. As noted above “<<“ is called the stream insertion operator. It is overloaded to properly handle all the native C++ types.

The most common mistake made when using the cout object is forgetting to put quotation marks around strings, but practice makes perfect.

Learning More About cout and cin

My favorite book on C++ iostreams is the C++ IOStreams Handbook by Steve Teale, Addison Wesley, ISBN 0- 201-59641-5.



 < Day Day Up > 



C++ for Artists. The Art, Philosophy, and Science of Object-Oriented Programming
C++ For Artists: The Art, Philosophy, And Science Of Object-Oriented Programming
ISBN: 1932504028
EAN: 2147483647
Year: 2003
Pages: 340
Authors: Rick Miller

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