Keltron It Education Centre Spencer Trivandrum University of Kerala
Keltron It Education Centre Spencer Trivandrum University of Kerala
Keltron It Education Centre Spencer Trivandrum University of Kerala
2008-2009
SUJITH KUMAR
Program:
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
void main()
clrscr();
cin>>m>>n;
cin>>p>>q;
if(n!=p)
getch();
exit(0);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
cin>>A[i][j];
for(i=0;i<p;i++)
2
for(j=0;j<q;j++)
cin>>B[i][j];
for(i=0;i<m;i++)
for(j=0;j<q;j++)
sum=0;
for(k=0;k<n;k++)
sum=sum+A[i][k]*B[k][j];
C[i][j]=sum;
cout<<"Resultant Matrix"<<endl;
for(i=0;i<m;i++)
for(j=0;j<q;j++)
cout<<" "<<C[i][j];
cout<<endl;
getch();
Out put 1:
3 4
3
Enter the order of second matrix
4 3
12 13 14 10 25 21 14 13 12 47 10 12
24 20 23 21 22 20 35 36 14 12 14 14
Resultant Matrix
Out put 2:
3 2
4 3
Aim: Write a C++ program to to count characters, words and number of lines in a text.
Program:
#include <iostream.h>
#include <conio.h>
#include <iomanip.h>
#include <string.h>
class textc
protected:
char text[100];
int nw;
int nc;
int nl;
public:
4
textc()
strcpy(text,"");
nw=0;
nc=0;
nl=0;
void gettext()
cin.getline(text,100);
void count()
int i,x;
x=strlen(text);
for(i=0;i<x;i++)
if(text[i]==' ')
nw++;
if(text[i+1]==' '||text[i+1]=='.'||text[i+1]==NULL)
nw--;
if(text[i]=='.'||text[i+1]==NULL)
nl++;
if(text[i+1]!=' ')
nw++;
5
nc=i;
void display()
};
void main()
textc T;
clrscr();
T.gettext();
T.count();
T.display();
getch();
Out put:
Characters -> 47
words -> 9
Lines ->2
Program:
#include <iostream.h>
#include <fstream.h>
#include <string.h>
6
#include <stdlib.h>
#include <conio.h>
class electricity
char Cnam[20];
int Cno;
int Pre_R;
int Cur_R;
float Rate;
public:
void getdata()
cout<<"Con.No : ";
cin>>Cno;
cin>>Cnam;
cin>>Pre_R;
cin>>Cur_R;
void display()
gotoxy(15,15);
gotoxy(45,15);
gotoxy(15,17);
cout<<"pre:reading:"<<Pre_R;
gotoxy(15,19);
7
cout<<"cur:reading:"<<Cur_R;
gotoxy(15,21);
gotoxy(15,23);
cout<<"Amount : "<<Rate;
void calcu()
if(Cunit<101)
Rate=Cunit*.8;
else if(Cunit<201)
Rate=80+(Cunit-100)*1;
else if(Cunit<301)
Rate=180+(Cunit-200)*2;
else if(Cunit<501)
Rate=380+(Cunit-300)*5;
else
Rate=1380+(Cunit-500)*9;
};
void main()
electricity E;
clrscr();
E.getdata();
E.calcu();
clrscr();
gotoxy(25,10);
8
cout<<"Electricity Billig";
gotoxy(24,12);
cout<<"********************";
E.display();
getch();
Out put:
Con.No :S2456
Electricity Billig
********************
pre:reading:10245
cur:reading:10347
Consumed Unit:102
Amount :82
Aim: Write a C++ program to read a list containing item name, code, and cost and produce a three column output.
Program:
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
class List
char It_nam[20];
9
int It_code;
float It_price;
public:
void getitem()
cin>>It_nam;
cin>>It_code;
cout<<"EnterPrice/unit : ";
cin>>It_price;
void display()
cout<<endl<<setw(10)<<It_code<<setw(10)<<It_nam<<setw(10)<<It_price<<endl;
};
void main()
List L[20];
char ch='y';
int i=0,j=0;
clrscr();
while(ch=='y')
L[i].getitem();
++i;
cin>>ch;
10
clrscr();
gotoxy(15,5);
gotoxy(14,6);
cout<<"**************" <<endl;
cout<<setw(10)<<"Code"<<setw(10)<<"Item"<<setw(10)<<"Price"<<endl;
while(j<i)
L[j].display();
++j;
getch();
Out put:
EnterPrice/unit : 18.50
Enter Another....?(y/n) :y
EnterPrice/unit :25.80
Enter Another....?(y/n) :n
Item Details
**************
11
Aim: Write a C++ program to prepare a shopping list using class with arrays as data members.
Program:
#include <iostream.h>
#include <iomanip.h>
#include <stdlib.h>
#include <conio.h>
class shopping
char item[20][10];
int it_no[20],it_qty[20];
float it_rate[20],total[20],sum;
int count;
public:
shopping()
count=0;
sum=0;
void getdata()
cin>>item[count];
cin>>it_rate[count];
cout<<"Enter quantity:";
cin>>it_qty[count];
total[count]=it_rate[count]*it_qty[count];
it_no[count]=count+1;
count++;
void display()
12
{
int y=6;
cout<<"Slno:"<<"\t"<<"Item"<<setw(25)<<"Price"<<setw(5)<<"qty"<<setw(10)<<"Total"<<endl;
cout<<"-----------------------------------------------------"<<endl;
for(int i=0;i<count;i++)
cout<<it_no[i]<<"\t"<<item[i];
gotoxy(23,y);
cout<<setw(14)<<it_rate[i]<<setw(5)<<it_qty[i]<<setw(10)<<total[i]<<endl;
sum=sum+total[i];
y++;
void disp()
};
void main()
shopping S;
char ch='Y';
int x;
do
clrscr();
S.getdata();
S.display();
13
cin>>ch;
}while(ch=='y'||ch=='Y');
S.disp();
getch();
Out put:
Enter quantity:3
Do u want to continue?(Y/N) y
Enter quantity:1
Do u want to continue?(Y/N) n
Aim: Write a C++ program to find the interest of a FD using dynamic initialization of constructors.
Program:
#include <iostream.h>
#include <conio.h>
#include <iomanip.h>
class FD
{
14
float principle;
float rate;
float days;
float intr;
public:
FD()
principle=0.0;
rate=0.0;
days=0;
intr=0.0;
principle=pr;
rate=r;
days=d;
intr=principle*(days/365)*rate/100;
void display()
cout<<endl<<"Principle : "<<principle<<endl;
cout<<"Rate : "<<rate<<"%"<<endl;
cout<<"Interest : "<<setprecision(3)<<intr<<endl;
};
void main()
FD F1;
float pr,rt;
15
char ch='y';
int dt;
clrscr();
do
cout<<setw(25)<<"FD Details"<<endl;
cout<<"Principle : ";
cin>>pr;
cin>>rt;
cout<<"Period[days] : ";
cin>>dt;
F1=FD(pr,rt,dt);
F1.display();
cin>>ch;
}while (ch=='Y'||ch=='y');
Out put:
FD Details
Principle : 25000
Period[days] : 450
Principle : 25000
Rate : 4.5%
Interest :1386.986
FD Details
Principle :50000
Rate [%]: 3
16
Period[days] : 365
Principle : 50000
Rate : 3%
Interest :1500
Aim: Write a C++ program to create a rank list of students using two different classes.
Program:
#include <iostream.h>
#include <conio.h>
#include <iomanip.h>
#include <string.h>
class student
protected:
char nam[20];
int M1,M2,M3,tot;
public:
student()
strcpy(nam,"");
M1=0;
M2=0;
M3=0;
tot=0;
void getstud()
cout<<setw(25)<<"Mark Details"<<endl;
cin>>nam;
17
cout<<"Mark1 : ";
cin>>M1;
cout<<"Mark2 : ";
cin>>M2;
cout<<"Mark3 : ";
cin>>M3;
tot=M1+M2+M3;
};
int rnk;
public:
void display()
cout<<setw(10)<<++rnk;
cout<<setw(10)<<nam;
cout<<setw(10)<<tot<<endl;
};
rank T;
if(x.tot<y.tot)
T=x;
x=y;
y=T;
18
}
void main()
rank R[10],T;
char check='y';
int x=0;
clrscr();
for(int i=0;check=='y';i++)
R[i].getstud();
cin>>check;
cout<<"Rank list"<<endl;
for(int j=0;j<i;j++)
for(int k=0;k<i-j-1;k++)
great(R[k],R[k+1]);
clrscr();
cout<<setw(25)<<"Rank List"<<endl;
cout<<setw(10)<<"Rank"<<setw(10)<<"Name"<<setw(10)<<"Total"<<endl;
for(j=0;j<i;j++)
R[j].display();
19
getch();
Out put:
Mark Details
Mark1 :48
Mark2 : 49
Mark3 :47
Mark Details
Mark1 :46
Mark2 : 45
Mark3 :45
Mark Details
Mark1 :42
Mark2 : 43
Mark3 :46
1 ramesh 144
2 suresh 136
3 victor 131
Aim: Write a C++ program for different string manipulations - concatenation, copy, compare
20
Program:
#include <iostream.h>
#include <conio.h>
#include <string.h>
#include <iomanip.h>
void main()
char str1[20],str2[20];
clrscr();
cin>>str1;
strcpy(str2,str1);
cout<<endl<<"String 1 is :"<<str1<<endl;
cin>>str2;
int x=strcmp(str1,str2);
if (x<0 || x>0)
else
strcat(str1,str2);
getch();
Out put:
21
Enter String 1: keltron
String 1 is : keltron
String 2 is : keltron
Aim: Write a C++ program to calculate the salary of different grade of employers in a company using friend
function.
Program:
/*DA 45% LIC 5%grade1:hra->10%, pf->15%grade2:hra->8%, pf->13%grade3:hra->7%, pf->11%
grade4:hra->6%, pf->10%*/
#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
#include <iomanip.h>
#include <conio.h>
#define DA 0.45
class employee
{
char enam[20];
float ebasic;
int grade;
public:
void getdata()
{
cout<<setw(30)<<" Employee Details."<<endl;
cout<<"Enter emp Name : ";
cin>>enam;
cout<<"Enter grade[1-4] : ";
cin>>grade;
cout<<"Enter basic : ";
cin>>ebasic;
}
friend void calcu(employee E);
};
void calcu(employee emp)
{
float da,pf,lic,hra,gsal,nsal;
da=DA * emp.ebasic;
lic=.05*emp.ebasic;
if(emp.grade==1)
{
pf=.15*emp.ebasic;
hra=.1*emp.ebasic;
22
}
if(emp.grade==2)
{
pf=.13*emp.ebasic;
hra=.08*emp.ebasic;
}
if(emp.grade==3)
{
pf=.11*emp.ebasic;
hra=.07*emp.ebasic;
}
if(emp.grade==4)
{
pf=.10*emp.ebasic;
hra=.06*emp.ebasic;
}
gsal=emp.ebasic+da+hra;
nsal=gsal-pf-lic;
cout<<"Employee Name:"<<setw(5)<<emp.enam<<endl;
cout<<"Basic "<<setw(5)<<emp.ebasic<<endl;
cout<<"DA "<<setw(5)<<da<<endl;
cout<<"HRA "<<setw(5)<<hra<<endl;
cout<<"PF "<<setw(5)<<pf<<endl;
cout<<"LIC "<<setw(5)<<lic<<endl;
cout<<"Gross Salary "<<setw(5)<<gsal<<endl;
cout<<"Net Salary "<<setw(5)<<nsal<<endl;
}
void main()
{
employee E;
char x;
do
{
clrscr();
E.getdata();
calcu(E);
cout<<"Do u calculate another(y/n) -> ";
cin>>x;
}while(x=='y');
}
Out put:
Employee Details.
Enter grade[1-4] : 2
Employee Name:gowri
Basic 5000
23
DA 2250
HRA 400
PF 650
LIC 250
Aim: Write a C++ program to print a student report using multiple inheritances.
Program:
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#include<iomanip.h>
class marks
protected:
int M1,M2,M3;
public:
marks()
M1=M2=M3=0;
void getmark();
};
class sports
protected:
int grace;
public:
24
sports()
grace=0;
void getgrace();
};
int Rollno;
char name[20];
public:
student()
Rollno=0;
strcpy(name,"");
void getstudent();
void display();
};
void marks::getmark()
cout<<"Enter Marks"<<endl;
cin>>M1>>M2>>M3;
void sports::getgrace()
cin>>grace;
void student::getstudent()
25
{
cin>>Rollno;
cin>>name;
void student::display()
int s=M1+M2+M3;
int Tot=s+grace;
cout<<endl<<Rollno<<"\t\t"<<name<<"\t\t\t"<<Tot<<endl;
void main()
student s[10];
int j,i=0;
char ch='y';
while(ch=='y')
clrscr();
s[i].getstudent();
s[i].getmark();
s[i].getgrace();
i++;
cin>>ch;
cout<<endl<<"Students Records"<<endl;
cout<<endl<<"Roll No:\tName\t\t\tTotalmarks"<<endl;
for(j=0;j<i;j++)
26
{
s[j].display();
getch();
Out put:
Students Details
Enter Marks
45 46 43
Students Details
Enter Marks
42 43 41
Students Records
#include <iostream.h>
#include <conio.h>
27
#include <string.h>
void main()
clrscr();
int n;
void transfer(int,char,char,char);
cin>>n;
transfer(n,'A','C','B');
getch();
if(n>0)
transfer(n-1,from,tmp,to);
cout<<"Move disk"<<n<<"From"<<from<<"to"<<to<<endl;
transfer(n-1,tmp,to,from);
Out put:
28
Move 1 disk FromBtoC
Move1disk FromAtoB
Aim: Write a C++ program to perform arithmetic operations on complex numbers using operator overloading.
Program:
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
class complex
float real;
float img;
public:
complex()
real=img=0;
void getdata()
cin>>real;
29
cout<<"Imaginary Part : ";
cin>>img;
void display()
if(img>=0)
cout<<real<<"+"<<"i"<<img<<endl;
else
cout<<real<<"-"<<"i"<<(-1*img)<<endl;
};
complex complex::operator+(complex c)
complex tmp;
tmp.real=real+c.real;
tmp.img=img+c.img;
return(tmp);
complex complex::operator-(complex c)
complex tmp;
tmp.real=real-c.real;
tmp.img=img-c.img;
return(tmp);
complex complex::operator*(complex c)
complex tmp;
tmp.real=(real*c.real)-(img*c.img);
tmp.img=(img*c.real)+(real*c.img);
return(tmp);
30
}
complex complex::operator/(complex c)
complex tmp;
float ft;
ft=c.real*c.real+c.img*c.img;
tmp.real=(real*c.real+img*c.img)/ft;
tmp.img=(img*c.real-real*c.img)/ft;
return(tmp);
void main()
complex c1,c2,c3;
clrscr();
c1.getdata();
c2.getdata();
cout<<"Addition : ";
c3=c1+c2;
c3.display();
cout<<"Subtraction : ";
c3=c1-c2;
c3.display();
cout<<"Multiplication : ";
c3=c1*c2;
c3.display();
cout<<"Division : ";
c3=c1/c2;
31
c3.display();
getch();
Out put:
Real Part : 16
Imaginary Part : 8
Real Part : 12
Imaginary Part : 5
Results
Addition : 28 + i13
Subtraction : 4 + i3
Aim: Write a C++ program to concatenate two strings using operator overloading.
Program:
#include <iostream.h>
#include <conio.h>
#include <string.h>
class string
char str[20];
public:
string()
strcpy(str,"");
string(char *ch)
32
strcpy(str,ch);
void display()
cout<<str<<endl;
};
string tmp;
strcpy(tmp.str,str);
strcat(tmp.str,ch.str);
return(tmp);
void main()
clrscr();
string ch3;
string ch1="GOOD";
ch3=ch1+ch2;
ch3.display();
getch();
Out put:
33
Program 14:VIRTUAL FUNCTION
Aim: Write a C++ program to display the details of books and CDs in a shop using virtual functions.
Program:
#include <iostream.h>
#include <conio.h>
#include <string.h>
#include <iomanip.h>
class media
protected:
char title[20];
public:
media()
strcpy(title,"");
void getmedia()
cout<<"Title : ";
cin>>title;
{}
};
char author[20];
float price;
public:
34
book()
strcpy(author,"");
price=0;
void getbook()
getmedia();
cout<<"Author : ";
cin>>author;
cout<<"Price : ";
cin>>price;
void display()
cout<<"Title : "<<title<<endl;
cout<<"Author : "<<author<<endl;
cout<<"Price : "<<price<<endl<<endl;
};
char director[20];
float price;
public:
CD()
strcpy(director,"");
price=0;
35
}
void getcd()
getmedia();
cout<<"Director : ";
cin>>director;
cout<<"Price : ";
cin>>price;
void display()
cout<<"Name : "<<title<<endl;
cout<<"Director : "<<director<<endl;
cout<<"Price : "<<price<<endl<<endl;
};
void main()
book B;
CD C;
clrscr();
B.getbook();
cout<<"Enter CD Details"<<endl;
C.getcd();
M1 = &B;
M2 = &C;
cout<<setw(20)<<"MEDIA DETAILS"<<endl;
cout<<setw(15)<<"Book"<<endl;
36
M1->display();
cout<<setw(15)<<"CD Details"<<endl;
M2->display();
getch();
Out put:
Title :othello
Author : shakespere
Price :250
Enter CD Details
Title :IIhariharnagar
Director :lal
Price :100
MEDIA DETAILS
Book
Title :othello
Author : shakespere
Price :250
CD
Name : IIhariharnagar
Author : lal
Price:100
Aim: Write a C++ program to perform push and pop operations on a stack.
Program:
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#define MAX 5
37
int stack[20];
class stck
int top;
public:
stck()
top=-1;
char push(int);
int pop();
int empty();
int overflow();
};
char ch;
if(overflow())
return 0;
stack[++top]=st;
cin>>ch;
return(ch);
int stck::pop()
if(empty())
38
{
cout<<"Stack Empty"<<endl;
return 0;
cout<<stack[top--];
return 0;
int stck::overflow()
if(top==MAX-1)
return 1;
return 0;
int stck::empty()
if(top==-1)
return 1;
return 0;
void main()
stck s;
int n,x;
char ch='y';
clrscr();
while(1)
cout<<endl<<"\t"<<"MENU"<<endl;
cout<<"1. PUSH"<<endl;
cout<<"2. POP"<<endl;
39
cout<<"3. Exit"<<endl;
cin>>x;
switch(x)
case 1:
do
cin>>n;
ch=s.push(n);
}while(ch=='y');
break;
case 2:
s.pop();
break;
case 3:
exit(0);
break;
default:
cout<<"Invalied";
break;
Out put:
MENU
1. PUSH
2. POP
3. Exit
40
Enter ur choice : 1
MENU
1. PUSH
2. POP
3. Exit
Enter ur choice : 2
MENU
1. PUSH
2. POP
3. Exit
Enter ur choice : 2
MENU
1. PUSH
2. POP
3. Exit
Enter ur choice :2
Stack Empty
MENU
1. PUSH
2. POP
3. Exit
Enter ur choice : 3
Aim: Write a C++ program to create a linked list and add and delete from it.
Program:
41
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
struct node
int data;
node * next;
}*p,*q,*head,*temp;
void main()
void create();
void insert();
void display();
void del();
clrscr();
while(1)
int choice;
cin>>choice;
switch(choice)
case 1:
create();
break;
case 2:
insert();
break;
case 3:
42
del();
break;
case 4:
exit(0);
break;
void display()
p=head;
while(p!=NULL)
cout<<p->data<<"-";
p=p->next;
cout<<endl;
void create()
node *p;
char ch;
do
temp=new node;
temp->next=NULL;
cin>>temp->data;
if(head==NULL)
43
head=temp;
else
p->next=temp;
p=temp;
cin>>ch;
}while(ch=='y');
display();
void insert()
char ch='n';
do
cin>>choice;
temp=new node;
temp->next=NULL;
cin>>temp->data;
switch(choice)
case 1:
temp->next=head;
head=temp;
break;
44
case 2:
p=head;
while(p->next!=NULL)
p=p->next;
p->next=temp;
break;
case 3:
cout<<"Current list"<<endl;
display();
cin>>data1;
p=head;
while(p!=NULL)
if(p->data==data1)
q->next=temp;
temp->next=p;
break;
else
q=p;
p=p->next;
break;
case 4:
cout<<"Current list"<<endl;;
45
display();
cin>>data1;
p=head;
while(p!=NULL)
if(p->data==data1)
temp->next=p->next;
p->next=temp;
break;
else
p=p->next;
break;
cout<<"Changed Data"<<endl;
display();
cin>>ch;
}while(ch=='y');
void del()
char ch='n';
46
cout<<" Delete from Where : "<<endl;
do
cin>>choice;
switch(choice)
case 1:
head=head->next;
break;
case 2:
p=head;
while(p->next!=NULL)
q=p;
p=p->next;
q->next =NULL;
delete p;
break;
case 3:
cout<<"Current list";
display();
cin>>data1;
p=head;
while(p!=NULL)
if(p->data==data1)
47
{
q->next=p->next;
delete p;
break;
else
q=p;
p=p->next;
break;
cout<<"Changed Data"<<endl;
display();
cin>>ch;
}while(ch=='y');
Out put:
MENU
1. Create
2. Insert
3. Delete
4. Exit
Do u insert another....?(y/n)y
48
Do u insert another....?(y/n)n
Data in List
10 – 12 –
MENU
1. Create
2. Insert
3. Delete
4. Exit
Where to insert
1. First Node
2. Last Node
3. Insert Before
4. Insert After
Current list
Data in List
10 – 12 –
Changed Data
Data in List
10 – 9 -12 –
Do u insert another....?(y/n) n
MENU
1. Create
2. Insert
3. Delete
4. Exit
49
1. First Node
2. Last Node
3. Particular Node
Changed Data
Data in List
9 -12 –
Do u Delete another....?(y/n) n
MENU
1. Create
2. Insert
3. Delete
4. Exit
#include <conio.h>
#include <iomanip.h>
#include <string.h>
void main()
int n,i,*A;
clrscr();
cin>>n;
A=new int[n];
for(i=0;i<n;i++)
cin>>A[i];
50
bsort(n,A);
for(i=0;i<n;i++)
cout<<setw(10)<<A[i]<<endl;
getch();
for(int j=0;j<n-1;j++)
for(int k=0;k<n-j-1;k++)
if(A[k]>A[k+1])
int tp=A[k];
A[k]=A[k+1];
A[k+1]=tp;
Out put:
Enter Elements : 12 20 8 56
4 23 11 10
6 7
51
8
10
11
12
20
23
56
Aim: Write a C++ program to search the record in the file using name or phrases.
Program:
#include <iostream.h>
#include <conio.h>
#include <fstream.h>
#include <string.h>
class student
char nam[20];
int age;
public:
student()
strcpy(nam,"");
age=0;
//void getstud();
return(strcmp(ch,nam));
void display()
52
cout<<"Student Name : "<<nam<<endl;
cout<<"Age : "<<age<<endl;
void getstud()
cin>>nam;
cout<<"Age : ";
cin>>age;
};
void main()
student S;
char ch[20],check='y';
int x=0;
fstream f;
f.open("student",ios::in|ios::out);
clrscr();
cout<<"Enter Data"<<endl;
while(check=='y')
S.getstud();
f.write((char*)&S, sizeof(S));
cin>>check;
f.seekg(0);
cin>>ch;
53
cout<<"Result"<<endl;
while(f.eof()==0)
f.read((char*)&S, sizeof(S));
x=S.search(ch);
if(x==0)
S.display();
break;
if(x!=0)
cout<<"Not Found"<<endl;
f.close();
getch();
Out put:
Enter Data
Age : 24
Enter Data
Age : 23
Enter Data
Age : 26
54
Result
Age : 23
Aim: Write a C++ program to add, update, and delete from a file.
Program:
#include <iostream.h>
#include <conio.h>
#include <fstream.h>
#include <string.h>
#include <stdlib.h>
#include <iomanip.h>
#include <stdio.h>
class student
public:
char nam[20],place[20];
int age;
public:
void display()
cout<<setw(13)<<nam<<setw(10)<<age<<setw(10)<<place<<endl;
void getstud()
cin>>nam;
cout<<"Age : ";
cin>>age;
cout<<"Place :";
cin>>place;
55
}
};
void main()
student S;
char ch[20],c;
int i,check,n,flg=0;
fstream f,f1;
int sz=sizeof(S);
clrscr();
while(1)
cout<<endl<<"Menu"<<endl;
cin>>check;
switch(check)
case 1:
f.open("d:\student1.txt",ios::in|ios::out);
cout<<"Enter Data"<<endl;
f.seekp(0,ios::end);
S.getstud();
cin.get(c);
f.write((char*)&S, sizeof(S));
f.close();
break;
case 2:
int loc;
flg=0;
f1.open("e:\student1.txt",ios::in|ios::out|ios::ate);
56
f1.seekg(0,ios::beg);
f1.seekp(0,ios::beg);
cin>>ch;
while(f1.read((char*)&S, sizeof(S)))
if(strcmp(S.nam,ch)==0)
S.getstud();
cin.get(c);
n=f1.tellg();
loc=n/sz;
f1.seekp((loc-1)*sz);
f1.write((char*)&S, sizeof(S));
f1.close();
flg=1;
break;
if(flg==0)
cout<<"Can't Found"<<endl;
f1.close();
break;
case 3:
f1.open("d:\student1.txt",ios::in|ios::out);
f.open("d:\student.txt",ios::in|ios::out);
f1.seekg(0,ios::beg);
f.seekg(0,ios::beg);
57
flg=0;
cin>>ch;
while(f1.read((char*)&S, sizeof(S)))
if(strcmp(S.nam,ch)!=0)
f.write((char*)&S, sizeof(S));
if(strcmp(S.nam,ch)==0)
flg=1;
cin.get(c);
if(flg==1)
cout<<"Removed Successfully"<<endl;
else
cout<<"Not found...!!!"<<endl;
f.close();
f1.close();
remove("d:\student1.txt");
if(rename("d:\student.txt","d:\student1.txt")==0)
remove("d:\student.txt");
break;
case 4:
f.open("d:\student1.txt",ios::in);
i=0;
f.seekg(0);
cin.get(c);
cout<<setw(24)<<"Student Details"<<endl<<setw(15)<<"Student
Name"<<setw(10)<<"Age"<<setw(10)<<"Place"<<endl;
while(f.read((char*)&S, sizeof(S)))
S.display();
58
i++;
if(i==0)
cout<<"No Data....!!!!!"<<endl;
f.close();
break;
case 5:
exit(0);
break;
f.close();
Out put:
Menu
1. Add
2. Update
3. Delete
4. Display
5. Exit
Enter Data
Age : 25
Place : pallichal
Menu
1. Add
2. Update
59
3. Delete
4. Display
5. Exit
Enter Data
Age : 26
Place : ooruttambalam
Menu
1. Add
2. Update
3. Delete
4. Display
5. Exit
ramesh
Age : 26
Place : trivandrum
Menu
1. Add
2. Update
3. Delete
4. Display
5. Exit
Student Details
Ramesh 26 trivandrum
60
Sreejith 26 ooruttambalam
Place : trivandrum
Menu
1. Add
2. Update
3. Delete
4. Display
5. Exit
suresh
Removed Successfully
Menu
1. Add
2. Update
3. Delete
4. Display
5. Exit
Aim: Write a C++ program to create copy command (command line arguments).
Program:
#include <iostream.h>
#include <conio.h>
#include <fstream.h>
#include <string.h>
class student
char nam[20];
int age;
public:
student()
61
{
strcpy(nam,"");
age=0;
void getstud()
cin>>nam;
cout<<"Age : ";
cin>>age;
void display()
cout<<"Age : "<<age<<endl;
};
student S;
char ch[20],check='y';
fstream f1,f2;
f1.open(argv[1],ios::in|ios::out);
clrscr();
cout<<"Enter Data"<<endl;
while(check=='y')
S.getstud();
f1.write((char*)&S, sizeof(S));
cin>>check;
62
}
f2.open(argv[2],ios::in|ios::out);
f1.seekg(0,ios::beg);
f2.seekp(0,ios::beg);
while(f1.read((char*)&S, sizeof(S)))
f2.write((char*)&S, sizeof(S));
f2.seekg(0,ios::beg);
while(f2.read((char*)&S, sizeof(S)))
S.display();
f1.close();
f2.close();
getch();
Thank GOD.
Thank Parents.
Thank you.
63