/*****************************************************
81.Simple program for exception handling
*******************************************************/
#include
#include
void main()
{
cout<<"start\n";
try
{
cout<<"inside the try block\n";
throw 150;
cout<<"this will not execute\n";
}
catch(int i)
{
cout<<"\ncaught an exception";
cout<<"\nvalue is:"<
}
cout<<"\nend";
getch();
}
void accept(int num)
{
try
{
if(num==1)
throw num;
}
catch(int i)
{
cout<<"caught an exception"<<"\n";
}
}
void main()
{
cout<<"start\n";
accept(0);
accept(1);
accept(2);
cout<
getch();
}
OUTPUT:
Start
Inside the try block
Caught an exception
Value is :150
end
/***************************************************************
82. Implementing a function that generates an exception
****************************************************************/
#include
#include
void accept( int num)
{
try
{
if(num==1)
throw num;
}
catch(int i)
{
cout<<"caught an exception"<<"\n";
}}
void main()
{
cout<<"start\n";
accept(0);
accept(1);
accept(2);
cout<
getch();
}
OUTPUT:
start
start
Caught an exception
/*******************************************************
83. Program for multiple catch statements
*********************************************************/
#include
#include
void test(int x)
{
try
{
if(x==1)
throw x; //throwing int
else
if(x==0)
throw 'x'; //throwing char
else
if(x==-1)
throw 1.0;//throwing double
cout<<"End of try block"<
}
catch(char c) //first catch stmt
{
cout<<"caught a character\n";
}
catch(int m) //second catch stmt
{
cout<<"caught an intiger\n";
}
catch(double d) //third catch stmt
{
cout<<"caught a double\n";
}
cout<<"end of try and catch system\n";
}
void main()
{
cout<<" MULTIPLE CATCHES\n";
cout<<"x==1\n";
test(1);
cout<<"x==0\n";
test(0);
cout<<"x==-1\n";
test(-1);
cout<<"x==2\n";
test(2);
getch();
}
OUTPUT:
MULTIPLE CATCHES
X==1
Caught an integer
End of try and catch system
X==0
Caught a character
End of try and catch system
X==-1
Caught a double
End of try and catch system
X==2
End of try block
End of try and catch system
/**************************************************************
84. Program for catching all exceptions in a single catch
******************************************************************/
#include
#include
void test(int x)
{
try
{
if(x==0) throw x; //int
if(x==-1)throw 'x';//char
if(x==1)throw 1.0; //float
}
catch(...) //catch all
{
cout<<"caught an exception\n";
}
}
void main()
{
cout<<"testing the catches"<
test(-1);
test(0);
test(1);
getch();
}
OUTPUT:
Testing the catches
Caught an exception
Caught an exception
Caught an exception
/**********************************************
85. Program for rethrowing
***********************************************/
#include
#include
void divide(double x,double y)
{
cout<<"Inside function\n";
try
{
if(y==0.0)
throw y;
else
cout<<"DIVISION ="<
}
catch(double) //catch a double
{
cout<<"caught double inside function \n";
throw; //rethrowing double
}
cout<<"END OF FUNCTION\n\n";
}
void main()
{
cout<<"Inside main\n";
try
{
divide(11.5,5.0);
divide(22.0,0);
}
catch(double)
{
cout<<"caught double inside main"<
}
cout<<"end of main";
getch();
}
OUTPUT:
Inside main
Inside function
DIVISION =2.3
END OF FUNCTION
Inside function
Caught double inside function
Caught double inside main
End of main
/****************************************************
86. Program for operations using char files
****************************************************/
#include
#include
#include
#include
void main()
{
char string[80];
cout<<"enter a string ";
cin>>string;
int len=strlen(string);
fstream file; //i/p and o/p stream
file.open("TEXT", ios::in|ios::out);
for(int i=0;i
file.put(string[i]); //put a char to files
file.seekg(0); //go to start
char ch;
while(file)
{
file.get(ch); //get a char from file
cout< //display it on the screen
}
getch();
}
OUTPUT:
Enter a string
Sriram
S
R
I
R
A
M
/*****************************************************
87. To work with a single file
********************************************************/
//creating files with constructor function
#include
#include
#include
void main()
{
ofstream fout("DATA"); //connect data file to fout
char name[40];
int marks;
cout<<"enter the name\n";
cin>>name; //get name from keyboard and
fout< //write to DATA
cout<<"enter marks \n";
cin>>marks; //get marks from keyboard
fout< //write marks to the DATA
fout.close(); //disconnect DATA file from fout
ifstream fin("DATA"); //connect DATA file to fin
fin>>name;
fin>>marks;
cout<<"\nname="<
cout<<"\nmarks="<
fin.close(); //disconnect DATA from fin
}
OUTPUT:
Enter the name
Sriram
Enter marks
890
Name=Sriram
Marks=890
/***********************************************
88. Program for creating a txt file
************************************************/
#include
#include
#include
void main()
{
char ch;
ofstream myfile("TXT"); //open the file to write
cout<<"enter characters \n";
while(cin) {
cin.get(ch); //get character from keyboard into ch
myfile.put(ch); //store character from ch to file
}
myfile.close(); //close file
ifstream myfile1("TXT"); //open file to read
cout<<"the entered characters are\n";
while(myfile1){
myfile1.get(ch); // get character from file
cout<
}
myfile.close()
while(!fin.eof()) //check end of the file
{
fin.getline(line,N); //read a line
cout<
cout<<"\n";
}
fin.close(); //disconnect country and
fin.open("CAPITAL"); //connect to capital
cout<<"contents of the capital are\n\n";
while(!fin.eof())
{
fin.getline(line,N);
cout<
}
fin.close();
getch();
}
OUTPUT:
Contents of country
Contents of the capital are
/***********************************************************
89. Program to implement scope resolution operator
***********************************************************/
#include
#include
int m=10;
void main()
{
int m=20;
clrscr();
{
int k=m;
int m=30;
cout<<"we are in inner block"<
cout<<"k="<
cout<<"m="<
cout<<"::m="<<::m<
}
cout<<"we are in outer block"<
cout<<"m="<
cout<<"::m="<<::m<
getch();
}
output:
we are in inner block
k=20
m=30
::m=10
we are in outer block
m=20;
::m=10
/**********************************************
90. Program for binary files
***********************************************
#include
#include
#include
#include
class employee
{
int eno;
char ename[10],dept[10];
float basic;
public:
void readdata();
void writedata();
};
void employee::readdata()
{
cout<<"\n enter employee no"<
cin>>eno;
cout<<"\n enter employee name"<
cin>>ename;
cout<<"\n enter employee department"<
cin>>dept;
cout<<"enter employee basic salary"<
cin>>basic;
}
void employee::writedata()
{
cout<
cout<
cout<
cout<
}
void main()
{
employee emp[50];
fstream file;
int n;
cout<<"how many employees do u want to enter"<
cin>>n;
file.open("emp.data",ios::in/ios::out);
cout<<"enter employee details"<
for(int i=0;i
{
emp[i].readdata();
file.write((char*)&emp[i],sizeof(emp[i]));
}
file.seekg(0);
cout<<"eno"<
cout<<"ename"<
cout<<"dept"<
cout<<"basic"<
for(i=0;i
{
file.read((char*)&emp[i],sizeof(emp[i]));
emp[i].writedata();
}
file.close();
getch();\
}
Output :
how many employees do u want to enter
1
enter employee details
enter employee no
101
enter employee name
aaaa
enter employee department
CSc
enter employee basic salary
9000
eno ename dept salary
101 aaaa CSc 9000
0 comments:
Post a Comment