Console Input and Output

 

This course uses console input/output for all examples – it is not a course in GUI development with windows and other graphical user interface elements. Console input/output means keyboard and screen in text mode as opposed to graphics mode. Therefore, the  C++ classes that support console input and output are critical to this course. In particular, the iostream class with objects cin and cout.

 

The std Namespace:

By using unique namespaces the programmer can ensure both they and standard library developers do not assign identifiers that duplicate one another.

Consider your street address – a house number and a street name. How many such streets with that house number are likely to be found in USA? The street address 811 Chestnut St. would likely be found in every city of population 20000 or more. How do you distinguish between your street address and those thousands of others with the same house number and street name? You use a city and state or a Zip code to distinguish between them and eliminate ambiguity. In the C++ language you use namespaces, which can be considered as analogous to Zip codes or Cities and States.

 

All identifiers that are part of the public interface of the Standard C++ library are declared in the std namespace. This is analogous to saying that the C++ library is contained wholly within Zip code 10000 whereas all of your program identifiers that you create unique to your program are in Zip code 00000, or in the “unnamed” or “global” namespace. More on namespaces in Chapter 23.

 

C++ objects cin, cout, and cerr all lie in the std namespace.

 

The Standard Output Stream:

 

The output stream object cout displays output to the video monitor. The following statement,

 

std::cout << amt << ‘ ‘ << ch << ‘ ‘ << val;

concatenates the values of amt, ch and val,  with intervening spaces, and sends the stream to the display.

 

 

Fomatted Output: Manipulators

 

When a manipulator appears in the stream, the subsequent data item is converted to the format shown for that manipulator.  For example,

std::cout << std::oct << amt << ‘ ‘ …..

changes the format of the value of amt into octal rather than the default decimal format.

 

The Standard Error Stream:

The standard error stream defaults to the console. This is useful in instances where cout may have been redirected to a file – the error messages would still come out on the console.

 

 

The Standard Input Stream:

Int amt;

std::cin >> amt;

 

The above statement causes user input to the keyboard to be accumulated into the variable amt.

The really powerful aspect of the input stream object is that it knows the data type of the destination variable and automatically does the conversion from ASCII characters into the appropriate format. For example, the ASCII characters 32005 have individual values of 51, 50, 48, 48, 53. The cin object converts that series of ASCII characters into the decimal number 32005 and stores it in variable amt as the binary value  [01111101][00000101]

[Note: The textbook Listing 2-20 declares amt as a short int, which is only 2 bytes. ]