Sie sind auf Seite 1von 26

Chapter 3

Input/Output

Outline
1.
2.

3.
4.

5.

6.
7.
8.

I/O Streams and Standard I/O Devices


Using Predefined Functions in Program

cin and the get Functions

The Dot Notation Between I/O Stream Variables and I/O Functions: A Precaution

Input Failure
Output and Formatting Output

setprecision Manipulator

fixed Manipulator

showpoint Manipulator

setw

Additional Output and Formatting Tools

setfill Manipulator

left and right Manipulators

Input/Output and the string Type


File Input
Exercises

1. I/O Streams and Standard I/O Devices

In C++, I/O is a sequence of bytes, called a stream, from the source


to the destination.

The bytes are usually characters. (They could be also graphic


image, or digital speech).

There are two types of streams:

Input stream: A sequence of characters from an input device to the computer.

Output stream: A sequence of characters from the computer to an input device.

The standard input/output devices are respectively the keyboard and


the screen.

To receive data from the keyboard and send output to the screen,
every C++ must use the header file iostream.

1. I/O Streams and Standard I/O Devices

The syntax of cin: cin >> variable >> variable ;

A single input statement can read more than one data at the same
time by using the >> operator.

Example: cin >> payRate >> hoursWorked;

Whether the input is:

15.50 48.30

// values in one line separated by space

15.50

// values in one line separated by many spaces

15.50

48.30

48.30

// values in separated ines

The preceding input statement would store 15.50 in payRate and


48.30 in hoursWorked.

1. I/O Streams and Standard I/O Devices


cin >> a;
Data Type of a

Valid Input for a

char

One printable character except the blank.

int

An integer, possibly preceded by a + or - sign


A decimal number, possibly preceded by a + or sign. If

double

the actual data input is an integer, the input is converted


to a decimal number with the zero decimal part.

1. I/O Streams and Standard I/O Devices


int a, b;
double z;
char ch, ch1, ch2;
Statement

Input

Value Stored in Memory

cin >> ch;

ch = A

cin >> ch;

AB

ch = A, B is held for later input

cin >> a;

48

a = 48

cin >> z >> a;

65.78 38

z = 65.78, a = 38

cin >> a >> ch >> z;

57 A 26.9

a = 57, ch = A, z = 26.9

cin >> a >> b >> z;

11 34

a = 11, b = 34, computer waits for


the later input

cin >> ch >> a;

256

ch = 2, a = 56

2. Using Predefined Functions in a Program

A function (like main) is a subprogram to accomplish some job.

A function can be user-defined or predefined (already written).

Predefined functions are organized as collection of libraries: header


files.

To use a predefined function, we need to know the header file in


which it is defined.

Example: pow is a predefined function to calculate xy.


int s = pow(2, 3);

Call or invoke of pow

Arguments

2. Using Predefined Functions in a Program


#include<iostream>
#include<cmath>
#include<string>
using namespace std;
int main()
{
double u, v;
string str;
cout <<"2 to the power of 6 = " << pow(2.0, 6.0) << endl;
u = 12.5;
v = 3.0;
cout << u << " to the power of " << v << " = " << pow(u, v) << endl;
cout << "Square root of 24 = " << sqrt(24.0) << endl;
u = pow(8.0, 2.5);
cout << "u = " << u << endl;
str = "Programming with C++";
cout << "length of str = " << str.length() << endl;
}

2. Using Predefined Functions in a Program: cin function

#include<iostream>
using namespace std;
int main()
{
char ch1, ch2;
int num;
cout << "Enter two characters and an int: " ;
cin.get(ch1); // read one character (even a space) and store it in ch1
cin.get(ch2);
cin >> num; // the >> operator ignores the white spaces
cout << "ch1 = " << ch1 << endl;
cout << "ch2 = " << ch2 << endl;
cout << "num = " << num << endl;
return 0;
}

2. Using Predefined Functions in a Program: cin and get

Statement

Comment

cin.get(ch);

Wright statement

cinget(ch);

The dot is missing between cin and get.

cin.getch;

The parameter
parenthesis.

ch

must

be

inside

two

3. Input Failure

An attempt to read invalid data would result in an input failure.

#include<iostream>
using namespace std;
int main()
{
int a, b;
cout << "Enter two integers: " ;
cin >> a >> b;
cout << endl;
cout << "a = " << a << endl << endl;
cout << "b = " << b << endl << endl;
return 0;
}

4. Output and Formatting Output: setprecision

cout << expression or manipulator << expression or manipulator <<.. .;

Expression is evaluated then


its value is printed

Manipulator is used to format


the output

setprecision Manipulator: is used to control the output of floating-point


numbers. Syntax: setprecision(n) where n is the number of decimal places.

Example: cout << setprecision(2); formats the output of decimal numbers to


two decimal places.

The header file iomanip must added to the program using setprecision.

4. Output and Formatting Output: fixed and showpoint

fixed Manipulator: is used to control the output of floating-point numbers


in a fixed decimal format. Syntax: cout << fixed;

To

disable

the

manipulator

fixed,

use

the

function

unsetf:

cout.unsetf(is::fixed);

showpoint is an output manipulator to show the decimal part as zeros of


a decimal number without decimal part.

Syntax of showpoint: cout << showpoint;

Show point may be used with fixed: cout << fixed << showpoint;

4. Output and Formatting Output: setw

setw Manipulator: is used to output the value of an expression in a


specific number of columns.

The output is right-justified.

Syntax: cout << setw(n); // the out

Example: cout << setw(5) << x << endl;

4. Output and Formatting Output: Example


#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int x = 19;
int a = 345;
double y = 76.384;
cout << fixed << showpoint;
cout << "12345678901234567890" << endl;
cout << setw(5) << x << endl;
cout << setw(5) << a << setw(5) << "Hi" << setw(5) << x << endl;
cout << setprecision(2);
cout << setw(6) << a << setw(6) << y << setw(6) << x << endl;
cout << setw(6) << x << setw(6) << a << setw(6) << y << endl;
cout << setw(5) << a << x << endl;
cout << setw(2) << a << setw(4) << x << endl;
return 0;
}

5. Additional Output and Formatting Tools

setw justifies to the right the output and keeps white spaces for the
unused places.

setfill can be used to fill the unused columns with a character


other than a space.

The syntax of setfill is: ostreamVar << setfill(ch);

5. Additional Output and Formatting Tools: setfill


int x = 15;
int y = 7634;
cout << "12345678901234567890" << endl << endl;
cout << setw(5) << x << setw(7) << y << setw(8) << "Warm" << endl << endl;
cout << setfill('*'); // * can by any other character like #, $, &, -,
cout << setw(5) << x << setw(7) << y << setw(8) << "Warm" << endl << endl;

5. Additional Output and Formatting Tools: Left and right


Manipulators
ostreamVar << left;

ostreamVar << right;

// sets the output to be left-

// sets the output to be right-

justified on the standard output

justified on the standard output

device

device

Example: cout << left;

Example: cout << right;

ostreamVar.unsetf(ios::left);

ostreamVar.unsetf(ios::right);

// disables the left manipulator

// disables the right manipulator

Example: cout.unsetf(ios::left);

Example: cout.unsetf(ios::right);

5. Additional Output and Formatting Tools: Left and right


Manipulators

#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int x = 15;
cout << left;
cout << "12345678901234567890" << endl << endl;
cout << setfill('*') << setw(5) << x << endl << endl;
cout << right;
cout << setw(5) << x << endl << endl;
return 0;
}

6. Input/Output and the string Type


cin >> variable; // reads a string and save it into variable (spaces are
not considered).
Example: string name;
cin >> name; // if the user enters Sarah Ali name = Sarah
and

Ali is ignored

getline (istreamVar, strVar) ; // reads a string containing spaces).


Example: string name;
getline(cin, name); // if the user enters Sarah Ali , name = Sarah Ali

cout << text with/without spaces; // displays the text delimited by

7. File Input

Getting input from the keyboard and sending output to the screen
have many limitations:

Inefficient to type a large amount of data anytime you run a program.

Typing a large amount of data may generate errors.

The output of large amount of data may not fit in one screen.

We can read/write from/into a file.

The header file called fstream is used for file I/O.

A data Type ifstream is defined in fstream (similar istream).


A data Type ofstream is defined in fstream (similar ostream).

7. File Input

The variables cin and cout are already defined and associated with
the standard I/O devices.

In addition, >>, get, and other variables can be used with cin.

While, <<, setfill, setw, and other variables can be used with cout.

cin and cout can not be used in reading or writing in a file.

File stream variables (ifstream and ofstream) must be declared to


input and output from and to a file.

7. File Input

File I/O is a five-step process:

Include the header file fstream in the program.

Declare file stream variables (ifstream for input and ofstream


for output).

Associate the file stream variables with the input/output


sources.

Use the file stream variables with <<, >>, or other input/output
functions.

Close the file.

7. File Input
#include <fstream>
// Adds additional header files you need
using namespace std;
int main()
{
ifstream inData;
ofstream outData;

// Open the files


inData.open("prog.dat"); // open the input file
outData.open("prog.out"); // open the output file
...
// Close the files
inData.close();
outData.close();
return 0;
}

8. Exercises
// Given the input: 46 A 49
// and the C++ code:
#include<iostream>
using namespace std;
int main()
{
int x = 10, y = 18;
char z = 'A';
cin >> x >> y >> z;
cout << x << " " << y << " " << z << endl;
return 0;
} // What is the output?

8. Exercises
// Suppose that age is an int variable and name is a string
//variable. What are the values of age and name after the
//following input stetements execute?
cin >> age;
getline(cin, name);
If the input is:
a.35 Mickey Balto
b.35
Mickey Balto

Das könnte Ihnen auch gefallen