0% found this document useful (0 votes)
10 views

oops 4

Object Oriented Programming Notes

Uploaded by

Abcd Efgh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

oops 4

Object Oriented Programming Notes

Uploaded by

Abcd Efgh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Program-22

Aim:- Write a program to perform the deletion of whitespaces such as horizontal tabs,
vertical tabs, space, line feed, new line and carriage return from a text file and store the
contents of the file without the white spaceson another file.

Program :-

#include <iostream>
#include<fstream>
using namespace std;

int main(){
ifstream prev("according_8_1.txt"); ofstream next("according_8_2.txt"); char ch;
int c = prev.peek(); while(!prev.eof())
{
ch= prev.get();
if(ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r' || ch == '\f' || ch == '\v'||ch=='\0'){} else{
next.put(ch);
}
c = prev.peek();
}
cout << "Program Executed" << endl; return 0;
}
Program-23

Aim:- Write a program to read the class object of student_info such as name, age, sex, height
and weight from the keyboard and to store them on a specified file using read() and write()
functions. Again, the same file is opened for reading and displaying the contents of the file on
the screen.

Program :-

#include <iostream>
#include <fstream>
using namespace std;

class student_info{ public:


string name, age, height, weight; char sex;
void read(); string write();
};

void student_info :: read(){ cout << "Enter Name: "; cin >> name;
cout << "Enter Age: "; cin >> age;
cout << "Enter Gender(F/M): "; cin >> sex;
cout << "Enter Height: "; cin >> height;
cout << "Enter Weight(kilogram): "; cin >> weight;
}

string student_info :: write(){


return "Name: " + name + "\n" + "Age: " + age + "\n" + "Gender: " + sex + "\n" +
"Height: " + height + "\n" + "Weight: " + weight + "\n" + "\n";
}

int main(){

int n;
cout << "Enter how many records are to be stored: "; cin >> n;
student_info student[n]; ofstream output;
output.open("according_9.txt", ios::app);
// output.write()
for (int i = 0; i < n; i++){ student[i].read();
// output.write(student, sizeof(student[i])); output << student[i].write();
}
output.close(); ifstream fin;
cout << "..............DISPLAYING THE CONTENTS OF THE FILE. " << endl;
fin.open("according_9.txt"); string input;
for(int i = 0; i < n; i++){ while(getline(fin, input)){
cout << input << endl;

}
}
fin.close(); return 0;
}
Program-24

Aim:- Write a program to raise an exception if any attempt is made to refer an element
whose index is beyond the array size.

Program :-
#include <iostream>
using namespace std;

void array_value(int i, int n, int *array){ if(i > n){


throw out_of_range("The entered index is out of range of the array!\n");
}
cout << "The value in the array at the index " << i << " is " << array[i] << endl;
}

int main(){ int n;


cout << "Enter the size of the array: "; cin >> n;
int *array = new int[n];
cout << "Enter the elements: \n"; for(int i = 0; i < n; i ++){
cin >> array[i];
}
bool running = true; while(running){
int i;
cout << "Enter the index of element you want to access: "; cin >> i;
try{
array_value(i, n, array);
}
catch(out_of_range &e){
cout << "Exception occured: " << e.what();
}
char choice;

cout << "Do you want to get any other element(y/n): "; cin >> choice;
if(choice == 'n'){ running = false;
}
else if(choice != 'y' && choice != 'n'){ cout << "Wrong choice entered!\n";
}

}
return 0;

}
Program-25

Aim:- Write a program to read a set of lines from the keyboard and to store it on a
specified file.

Program :-
#include <iostream>
#include <fstream>
using namespace std;

int main(){ string input;


ofstream output("beyond_18.txt");
cout << "Enter the lines you want to add in the file:\nEnter 'end' when you want to stop!\n";
do{
getline(cin, input); if(input != "end"){ output << input; output << "\n";
}
}while(input != "end"); return 0;
}
Program-26

Aim:- Write a program to read a text file and display its contents on the screen

Program :-

#include <iostream>
#include <fstream>
using namespace std;

int main(){ string input;


ifstream in("beyond_19.txt"); while(getline(in, input)){
cout << input << endl;
}
In.close(); return 0;
}
Program-27

Aim:- Write a program to copy the contents of a file into another.

Program :-

#include <iostream>
#include <fstream>
using namespace std;

int main(){ string input;

ifstream source("beyond_20_1.txt"); ofstream destination("beyond_20_2.txt");


cout << "Copying files from beyond_20_1.txt to beyond_20_2.txt\n";
while(getline(source, input))
{
cout << input << endl; destination << input; destination << "\n";
}

source.close(); destination.close(); return 0;


}
Program-28

Aim:- Write a program to read two numbers and divide the first number by the second
number and raise anexception if the second number is zero.

Program :-

#include <iostream>
using namespace std;

float divide(float n1, float n2){ if(n2 == 0){


throw runtime_error("Cannot divide by 0!\n");
}
return (n1 / n2);
}
int main(){ float n1, n2;
cout << "Enter two numbers: "; cin >> n1 >> n2;
try{
float result = divide(n1, n2);
cout << n1 << " divided by " << n2 << " is " << result << endl;
}
catch(runtime_error &error){
cout << "Exception occured: " << error.what();
}
return 0;
}

You might also like