CS 1408 Intro to Computer Science with VB.NET



[pic] |Department of Computer

and

Mathematical Sciences |CS 1410

Intro to Computer Science with

C++ |6 | |

Lab 6: Input and Output

Objectives:

The main objective of this lab is to understand Streams, Format Manipulators, and Text Files.

All computers must have some facilities for inputting information to be processed and for outputting the results of computation. Personal computers, like the one you're using now, usually have a variety of input/output (I/O) devices attached to them. These include keyboards for input, and monitors and/or printers for output. Disk drives and modems can be used for both input and output. Therefore programming languages like C++ include various features that allow the programmer to use these devices. In this lab, we will experiment with C++ programs that read input from the keyboard or disk files, and send output to the monitor or disk files.

Unlike many higher-level programming languages, C++ does not contain any predefined facilities for easily inputting data from a disk file (or the keyboard) or outputting data to a disk file (or the monitor). To perform I/O in C++, the programmer must rely on LIBRARIES of utility functions that have been created by other programmers especially for this purpose. Programming with specialized libraries of utility functions is an important aspect of C++. Numerous libraries of functions are included as part of any C++ programming package. To make use of the special utilities contained in libraries, a source code file must contain certain instructions, called Compiler Directives, that inform the compiler which libraries are needed. These directives are usually placed at the top of the file. You will recall that all the program files we have seen so far begin with the following two lines:

#include

#include

These lines are the directives informing the compiler that the program will be using functions contained in the libraries iostream and iomanip. The files mentioned in these directives (iostream, iomanip) are not the complete source code files for these libraries, but instead contain just enough information for the compiler to properly finish compiling the program. The actual code for these libraries (which has already been compiled) is added to your program during the LINKING phase of program construction. You may have already noticed this phase occuring as you were preparing to run programs from previous labs. Files such as iostream and iomanip are called HEADER FILES. In case you're wondering, the two header files mentioned above (and many others) are located in the directory C:\Program Files\Microsoft Visual Studio\Vc98\Include The libraries iostream and iomanip contain the objects and functions used for I/O in the next program. In particular, the library iostream contains the definitions of two objects, cin and cout, that we will use for input from the keyboard and output to the monitor. These objects are called STREAMS. Basically, "stream" is the term used in C++ programming for a text file. Oddly enough, both the keyboard and the monitor are considered to be streams. The stream cin is connected to the keyboard, and the stream cout is connected to the monitor. You can therefore think of cin as the C++ name for the keyboard, and cout as the C++ name for the monitor. The library iomanip contains the definitions of objects and functions that are used to FORMAT stream output. Carefully formatted output is an important ingredient of "user friendly" programs.

The objects and functions that are used to FORMAT stream output are called format manipulators. Examples of the use of format manipulators are contained in the program below. Here is a brief summary of the purpose of these manipulators:

setw(n) - sets the width of the printing field to n characters for the next output only. Default is 1. Leading or trailing blanks may be inserted if the output does not fill up the entire field.

setprecision(n) - causes all subsequent floating point outputs to be displayed with n decimal places of accuracy. Default is usually 6.

endl - inserts a newline character ('\n') and flushes the stream buffer.

fixed - causes all subsequent floating point outputs to be displayed in standard decimal form.

scientific - causes all subsequent floating point outputs to be displayed in scientific notation form.

showpoint - causes all subsequent floating point outputs to be displayed with a decimal point, even if the value is a whole number.

left - causes all subsequent outputs to left-justified within the printing field.

right - causes all subsequent outputs to right-justified within the printing field. This is the default.

Task 1: cout Output Stream

The purpose of this task is to help understand output stream cout, stream operator ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download