Rev D Lecture 2 I/O
In C++ a sequence of bytes is called a stream. The stream goes from a source to destination. The stream always uses the computer as the reference point.
A device will input data into the computer . The default device is the keyboard. We use a predefined variable for this input stream, cin (common input). Cin is a variable of data type istream. It is defined when the file iostream is included with the program. Therefore in order to use cin and cout we need to insert the header file, iostream, which defines them. When executing the build function with the statemen t#include<iostream> in the program the linker then inserts this file and only then cin and cout can be used. cin has use of an operator and functions.
a) operator >>
This is an extraction , binary operator and is used with a variable.
e.g. cin >> number; //input keyboard data is to be put in a stream, that is lined up in a queue with the first one to be written into the variable number. To correctly enter data the value type must match the variable type.
Exercise 1.
Statement Input Memory values
int a = 45;
int b = 17;
double z = 18.15;
char ch = ‘j’;
char ch1 = ‘n’;
cin >> ch; Nt a= b= ch= ch1= z=
cin >> ch1; a= b= ch= ch1= z=
cin >> a; 27 a= b= ch= ch1= z=
cin >> z; 9 a= b= ch= ch1= z=
cin >> a >> z; 4 18.4 a= b= ch= ch1= z=
cin >> a >> z; 13 a= b= ch= ch1= z=
9.6 a= b= ch= ch1= z=
cin>>z>>ch1>>a; 15.5A4 a= b= ch= ch1= z=
cin>>z>>ch1>>a; 19.3B a= b= ch= ch1= z=
26 a= b= ch= ch1= z=
cin>>a>>z; 15.65 a= b= ch= ch1= z=
cin>>ch>>ch1; C D a= b= ch= ch1= z=
cin>>a>>b; 48.62 a= b= ch= ch1= z=
cin>>a>>b; 15 11 a= b= ch= ch1= z=
Program
/*This
is lecture 2 Exercise 1 program*/
#include <iostream>
using namespace std;
int main()
{
int
a = 45;
int
b = 17;
double
z = 18.15;
char
ch = 'j';
char
ch1 = 'n';
cout<< "Input"<<endl;
cin >> ch; //input = Nt
cout<<"a=
"<<a<<" "<<"b= "<<b<<" "<<"ch=
"<<ch
<<" "<<"ch1=
"<<ch1<<" "<<"z= "<<z<<" "<<endl;
cin >> ch1;
cout<<"a=
"<<a<<" "<<"b= "<<b<<" "<<"ch=
"<<ch
<<" "<<"ch1=
"<<ch1<<" "<<"z= "<<z<<" "<<endl;
cout<< "Input"<<endl;
cin >> a; //input = 27
cout<<"a=
"<<a<<" "<<"b= "<<b<<" "<<"ch=
"<<ch
<<" "<<"ch1=
"<<ch1<<" "<<"z= "<<z<<" "<<endl;
cout<< "Input"<<endl;
cin >> z; //input = 9
cout<<"a=
"<<a<<" "<<"b= "<<b<<" "<<"ch=
"<<ch
<<" "<<"ch1=
"<<ch1<<" "<<"z= "<<z<<" "<<endl;
cout<< "Input"<<endl;
cin >> a >> z; //input =
4 18.4
cout<<"a=
"<<a<<" "<<"b= "<<b<<" "<<"ch=
"<<ch
<<" "<<"ch1=
"<<ch1<<" "<<"z= "<<z<<" "<<endl;
cout<< "Input"<<endl;
cin >> a >> z; //input =
13
//input = 9
cout<<"a=
"<<a<<" "<<"b= "<<b<<" "<<"ch=
"<<ch
<<" "<<"ch1=
"<<ch1<<" "<<"z= "<<z<<" "<<endl;
cout<< "Input"<<endl;
cin>>z>>ch1>>a;
//input = 15.5A4
cout<<"a=
"<<a<<" "<<"b= "<<b<<" "<<"ch=
"<<ch
<<" "<<"ch1=
"<<ch1<<" "<<"z= "<<z<<" "<<endl;
cout<< "Input"<<endl;
cin>>z>>ch1>>a; //input = 19.3B
//input = 26
cout<<"a=
"<<a<<" "<<"b= "<<b<<" "<<"ch=
"<<ch
<<" "<<"ch1=
"<<ch1<<" "<<"z= "<<z<<" "<<endl;
cout<< "Input"<<endl;
cin>>a>>z; //input =
15.65
cout<<"a=
"<<a<<" "<<"b= "<<b<<" "<<"ch=
"<<ch
<<" "<<"ch1=
"<<ch1<<" "<<"z= "<<z<<" "<<endl;
cout<< "Input"<<endl;
cin>>ch>>ch1; //input =
CD
cout<<"a=
"<<a<<" "<<"b= "<<b<<" "<<"ch=
"<<ch
<<" "<<"ch1=
"<<ch1<<" "<<"z= "<<z<<" "<<endl;
cout<< "Input"<<endl;
cin>>a>>b; //input =
48.62
cout<<"a=
"<<a<<" "<<"b= "<<b<<" "<<"ch=
"<<ch
<<" "<<"ch1=
"<<ch1<<" "<<"z= "<<z<<" "<<endl;
cout<< "Input failure"<<endl;
cin>>a>>b; //input = 15
11
cout<<"a=
"<<a<<" "<<"b= "<<b<<" "<<"ch=
"<<ch
<<" "<<"ch1=
"<<ch1<<" "<<"z= "<<z<<" "<<endl;
cout<<"The
end"<<endl;
return
0;
}
b) functions
1) cin.clear(); // restores the input to working state
2) cin.ignore(int #, ch);
e.g. cin.ignore(50,’a’); //ignores 50 characters or all the characters until the character ‘a’ is found, whichever comes first.
3) cin.get(varChar);
e.g. int a;
char ch, ch1;
cin>>ch>>ch1>>a;
data input: A 42
ch= ch1= a=
can repeat for same data input;
cin>>ch;
cin.get(ch1);
cin>>a’
ch= ch1= a=
4) cin.putback(ch);
lets you put the last character extracted from the input stream by the get function back into the input stream
5) ch=cin.peek();
places the character from the input stream into the variable but doesn’t remove it from the queue
Exercise 2
Data input for the following program: a bcde
/*This is lecture 2 Exercise 2 program*/
#include <iostream>
using namespace
std;
int main()
{
char ch;
int a;
cout<<"Enter a string: ";
cin.get(ch);
cout<<endl;
cout<< "after first cin.get(ch) ch= "<<ch
<<endl;
cin.get(ch);
cout<< "after second cin.get(ch) ch= "<<ch
<<endl;
a = ch;
cout<< "after second cin.get(ch) a= "<<a<<endl;
cin.get(ch);
cout<< "after third cin.get(ch) ch= "<<ch<<'\n';
cin.putback(ch);
cin.get(ch);
cout<< "after putback(ch) and fourth cin.get(ch) ch= "<<ch <<endl;
ch = cin.peek();
cout<< "after peek() ch= "<<ch
<<endl;
cin.get(ch);
cout<< "after fifth cin.get(ch) ch= "<<ch<<endl;
cin.get(ch);
cout<< "after sixth cin.get(ch) ch= "<<ch<<endl;
return 0;
}
getline(cin,string_variable);
Entering strings can sometimes be a problem. As we have seen the normal cin skips spaces when reading in strings.
e.g.
Exercise 3
// Data input for this program is Harry Potter
#include<iostream>
#include<string>
using namespace std;
int main()
{
int a;
string name, throw_Away;
char ch;
cout << "What is your full name ? ";
cin >> name>>throw_Away;
cin.get(ch); // Required to clean out the buffer of the line feed character
cout<<"the cin name is "<<name<< endl;
cout<<"the cin throw_Away is "<<throw_Away<< endl;
a=ch;
cout<<"the cin ch is "<<a<<endl;
string full_Name;
cout << "What is your full name ? ";
getline(cin, full_Name);
cout <<"the getline true name is "<<full_Name;
cout<<endl;
return 0;
}
2. Output stream
The computer will output data into a device. The default output device is the monitor screen. We use a predefined variable for this input stream, cout (common output). Cout is a variable of data type ostream. It is defined when the file iostream is included with the program.
a) operator <<
This is an insertion, binary operator and is used with a variable.
e.g. cout<< number; //the computer outputs data from a variable to be placed in the monitor buffer.
b) functions require <iomanip> header file
1) setprecision(n);
e.g. cout<<setprecision(2); // Sets decimal number output to two decimal places
2) fixed
e.g. cout<<fixed; // Sets all floating point numbers to a fixed decimal format.
3) scientific
e.g. cout<<scientific; //Sets all floating point numbers to scientific notation such as 3.88567e+002
4) showpoint
e.g. cout<<showpoint; // Always shows decimal point for a decimal number.
5) setw(n)
e.g. cout <<setw(8)<<x // The next output value is formatted to take up 8 columns
6) setfill(ch)
e. g. cout<<setfill(ch); // formats the unused defaulted left most columns of the setw instruction with the named character instead of a default space.
7) left
e.g. cout<<left ; // formats the output to be left justified (by default normally right justified)
cout<<”1234567890\n”
cout<<setw(8)<<abcd;
8) right
e.g. cout<<right; // formats the output to go to the default mode of right justified
3. File Input/Output
Entering data to programs via the keyboard utilizing cin is adequate in small quantities. For large quantities it is better to utilize files on secondary storage. Therefore to enter or retrieve information from files require different stream variables. Unlike the cin and cout these variables have to be defined by the user. Once they are defined they can be used with the same oerators and functions as cin and cout.
To define them one must include the file stream header file fstream.
e.g.
Exercise 4
/*Program is to read in test scores and the
student name from a data file and calculate the test average and to write back
the name, scores and test average in a formatted manner to another output file.
Input data file, test.txt contains Jack Smith 78
93 46 98 82*/
// Program to calculate the average test score.
#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>
using namespace
std;
int main()
{
ifstream indata;
ofstream outdata;
string firstName; // line 10
string lastName; //line 11
double test1,test2,test3,test4,test5;
double average;
indata.open("c:\\New Folder\\test.txt");
outdata.open("c:\\New Folder\\testavg.txt");
outdata<<fixed<<showpoint;
outdata<<setprecision(2);
cout<<"processing
data" <<endl;
indata>>firstName>>lastName; //line 19
outdata<<"Student
name: "<<firstName<<"
"<<lastName<<endl; //line 20
indata>>test1>>test2>>test3>>test4>>test5;
outdata<<"Test scores;
" <<setw(6)<<test1 <<setw(6)<<test2
<<setw(6)<<test3 <<setw(6)<<test4
<<setw(6)<<test5
<<endl;
average = (test1 + test2 + test3+ test4 + test5) / 5;
outdata<<"average test
score: " << setw(6)
<<average
<< endl;
indata.close();
outdata.close();
return 0;
}
Suppose test file contained data in the following format
Harry Potter
98 94 100 92 98 97 (You know Harry is a good student J)
You can change the program to use the getline function for the name
Do the following steps:
Edit test.txt to have the data in the above format(use notepad)
In the program for line 10 change firstName to name
Comment out line 11
Change line 19
From: indata>>firstName>>lastName;
To: getline(indata, name)
Change line 20
From: outdata<<"Student name: "<<firstName<<" "<<lastName<<endl
To: outdata<<"Student name: "<<name <<endl
Every time you write to the same file you write over the previous data.
To enter additional info open the existing output file with the following
Instructions:
ofstream outdata;
outdata.open("c:\\test.txt",ios::app);