OOP L1 L001 Exp9
OOP L1 L001 Exp9
A.3 Theory:
In C++ Programming, the console IO operations are performed using the header file iostream.h.
This header file provides two objects cin and cout to perform input and output operations
respectively. There are mainly two types of consol IO operations.
1. Unformatted consol IO -
2. Formatted consol IO
{
char ch;
cout<<"Press any key: ";
ch = cin.get();
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Object Oriented Programming and Design
cout << "You have pressed: ";
cout.put(ch); return 0;
}
⇢ getline(char *buffer,int size) and write(char * buffer, int n)
The getline(char *buffer,int size) is a method of cin object and it is used to input a string with
multiple spaces.
The write(char * buffer, int n) is a method of cout object and it is used to read n character from
buffer variable.
#include <iostream>
int main()
{
char ch[20];
return 0;
}
⇢ cin and cout objects
The cin is the object used to take input value of any variable of any type. It must be used with an
overloaded operator “>>”.
The cout is an object used to print string and variable values. It must be used with an overloaded
operator “<<”.
A.3.2. Formatted Console I/O :
The C++ programming language provides the following built-in functions to display the output in
formatted form. These built-in functions are available in the header file iomanip.h.
⇢ setw(int) and setfill(char)
The setw( int ) is a method used to set width of the output.
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Object Oriented Programming and Design
The setfill(char) is a method used to fill specified character at unused space.
#include <iostream>
#include <iomanip>
int main()
{
int x=10;
cout<<setw(10);
cout<<x<<endl;
cout<<setw(10)<<setfill('*')<<x<<endl;
return 0;
}
Input Stream: If the direction of flow of bytes is from the device(for example, Keyboard) to the main memory then this process is called input.
Output Stream: If the direction of flow of bytes is opposite, i.e. from main memory to device( display screen ) then this process is called output.
Standard output stream (cout): Usually the standard output device is the display screen. The C++ cout statement is the instance of the ostream
class. It is used to produce output on the standard output device which is usually the display screen.
The data needed to be displayed on the screen is inserted in the standard output stream (cout) using the insertion operator( <<).
standard input stream (cin): Usually the input device in a computer is the keyboard. C++ cin statement is the instance of the class istream and
is used to read input from the standard input device which is usually a keyboard.
The extraction operator(>>) is used along with the object cin for reading inputs.
The extraction operator extracts the data from the object cin which is entered using the keyboard.
We need to know:
Task2: Create your own file named as “MyFile_YourRollNo” and write following details in
your file: for 10 people in a table use gdb compiler, open in excel too
Name:
Roll number
College name
Address
Email address
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Object Oriented Programming and Design
PART B
Students must submit the soft copy as per following segments. The soft copy must be uploaded on
the portal at the end of the practical. The filename should be OOP_batch_rollno_experiment no,
Example: OOP_E1_E001_Exp8
Task 1: -
1.
#include <iostream>
using namespace std;
int main()
{
char ch;
cout<<"Press any key: ";
ch = cin.get();
cout << "You have pressed: ";
cout.put(ch); return 0;
}
2.
#include <iostream>
using namespace std;
int main()
{
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Object Oriented Programming and Design
char ch[20];
cout<<"What is your favourite website: ";
cin.getline(ch, 20);
cout <<endl<< "visit: www.";
cout.write(ch, 11);
cout<<".com"<<endl;
return 0;
}
3.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int x=10;
cout<<setw(10);
cout<<x<<endl;
cout<<setw(10)<<setfill('*')<<x<<endl;
return 0;
}
4.
#include <iostream>
using namespace std;
int main()
{
cerr << "An error occurred";
return 0;
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Object Oriented Programming and Design
}
5.
#include <iostream>
using namespace std;
int main()
{
clog << "An error occurred";
return 0;
}
6.
#include<fstream>
#include<iostream>
using namespace std;
int main()
{
char name[50];
ofstream ofile;
ofile.open("abc.txt");
cout<<"Writing to the file"<<endl;
cout<<"Enter your name:"<<endl;
cin.getline(name,50);
if(ofile.fail())
{
cout<<"Output file could not be opened.\n";
exit(1);
}
else
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Object Oriented Programming and Design
ofile<<name<<endl;
ofile.close();
ifstream ifile;
ifile.open("abc.txt");
cout<<"Reading from the file"<<endl;
if(ifile.fail())
{
cout<<"Input file could not be opened.\n";
exit(1);
}
else
ifile>>name;
cout<<name<<endl;
ifile.close();
return 0;
}
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Object Oriented Programming and Design
#include<fstream>
#include<iostream>
int main()
char name[50];
ofstream ofile;
ofile.open("abc.csv");
cin.getline(name,50);
if(ofile.fail())
exit(1);
else
ofile<<name<<endl;
ofile.close();
ifstream ifile;
ifile.open("abc.csv");
if(ifile.fail())
exit(1);
else
ifile>>name;
cout<<name<<endl;
ifile.close();
return 0;
}
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Object Oriented Programming and Design
Task 2: -
#include <fstream>
#include <iostream>
int main()
ofstream ofile;
ofile.open("MyFile_YourRollNo.csv", ios::app);
if (ofile.fail())
int rollno;
cin.get();
cin.getline(name, 50);
cin.getline(college, 100);
cin.getline(address, 100);
cin.getline(email, 100);
ofile << "\"" << name << "\"," << rollno << ",\"" << college << "\",\"" << address << "\",\""
<< email << "\"" << endl;
ofile.close();
ifstream ifile("MyFile_YourRollNo.csv");
if (ifile.fail()) {
int rollno;
ifile.get();
ifile.getline(email, 100);
cout << name << " " << rollno << " " << college << " " << address << " " << email << endl;
ifile.close();
return 0;
}
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Object Oriented Programming and Design
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Object Oriented Programming and Design
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Object Oriented Programming and Design
1. Task 1 was insightful. It really put everything into perspective about Input/Output streams. It is
not something that comes easily to me. I did need a little time to grasp what to do and how
exactly is the code being implemented
2. Task 2 was a good combination of the concepts we studied about. It took time to implement the
code in csv. I used append mode to put the different parameters into different fields.
Explore the world wide web and answer the following questions: