Beruflich Dokumente
Kultur Dokumente
Chapter 3: Input/Output
Objectives
In this chapter, you will:
• Learn what a stream is and examine input
and output streams
• Explore how to read data from the standard
input device
• Learn how to use predefined functions in a
program
• Explore how to use the input stream functions
get, ignore, putback, and peek
varChar
– Is a char variable
– Is the argument (parameter) of the function
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 19
cin and the ignore Function
• ignore function
– Discards a portion of the input
• The syntax to use the function ignore is:
• Expression is evaluated
• Value is printed
• Manipulator is used to format the output
– Example: endl
• Example:
– cout << setfill('#');
void main()
{ double P, R, N, MoneyEarned;
2.) Create a program that accepts value for T, I, and N. Compute and output the
value of A given the formula:
T I2
A = 2.5 2
N0.2
#include <iostream>
#include <cmath>
using namespace std;
void main()
{
double T, I, N, A;
cout<<“A = “<<A<<endl;
}
SEQUENTIAL PROGRAMMING IN C++
3.) Create a program that will compute for the value of X given the formula:
(Y3 + 2) – (MRE)1/8
X=
(HIN)4.3
#include <iostream>
#include <cmath>
using namespace std;
void main()
{ double X, Y, M, R, E, H, I ,N;
cout<<“Input value for Y, M, R, E, V, T, H, I and N : “;
cin>> Y>>M>>R>>E>>H>>I>>N;
cout<<“X = “<<X<<endl;
}
SEQUENTIAL PROGRAMMING IN C++
4.) Create a program that will compute for the value of C given the formula:
sin(E * L) + |- (F - O)|
C=
cos(N + E)
#include <iostream>
#include <cmath>
using namespace std;
main()
{ double C, E, L, F, O, N;
cout<<“Input value for E, L, F, O, and N : “;
cin>>E>>L>>F>>O>>N;
cout<<“C = “<<C<<endl;
}
Summary
• Stream: infinite sequence of characters from
a source to a destination
• Input stream: from a source to a computer
• Output stream: from a computer to a
destination
• cin: common input
• cout: common output
• To use cin and cout, include iostream
header