Beruflich Dokumente
Kultur Dokumente
Input/Output
Outline
1.
2.
3.
4.
5.
6.
7.
8.
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
setfill Manipulator
To receive data from the keyboard and send output to the screen,
every C++ must use the header file iostream.
A single input statement can read more than one data at the same
time by using the >> operator.
15.50 48.30
15.50
15.50
48.30
48.30
char
int
double
Input
ch = A
AB
cin >> a;
48
a = 48
65.78 38
z = 65.78, a = 38
57 A 26.9
a = 57, ch = A, z = 26.9
11 34
256
ch = 2, a = 56
Arguments
#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;
}
Statement
Comment
cin.get(ch);
Wright statement
cinget(ch);
cin.getch;
The parameter
parenthesis.
ch
must
be
inside
two
3. 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;
}
The header file iomanip must added to the program using setprecision.
To
disable
the
manipulator
fixed,
use
the
function
unsetf:
cout.unsetf(is::fixed);
Show point may be used with fixed: cout << fixed << showpoint;
setw justifies to the right the output and keeps white spaces for the
unused places.
device
device
ostreamVar.unsetf(ios::left);
ostreamVar.unsetf(ios::right);
Example: cout.unsetf(ios::left);
Example: cout.unsetf(ios::right);
#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;
}
Ali is ignored
7. File Input
Getting input from the keyboard and sending output to the screen
have many limitations:
The output of large amount of data may not fit in one screen.
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.
7. File Input
Use the file stream variables with <<, >>, or other input/output
functions.
7. File Input
#include <fstream>
// Adds additional header files you need
using namespace std;
int main()
{
ifstream inData;
ofstream outData;
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