Thursday, July 21, 2011

Sample project in C++

#include<iostream.h>
#include<process.h>
 #include<stdio.h>
 #include<conio.h>
 #include<fstream.h>
 class student
 {
 // int rlno;
 char na[30];
 public:
 int rlno;
 void getdata()
 {
 cout<<"\n Enter Name ";
 gets(na);
 cout<<"\n Enter roll number ";
 cin>>rlno;

}



void show()

{

// cout<<"\n Name is "<<na;

// cout<<"\n Roll number is "<<rlno;

cout<<endl<<rlno<<"\t\t\t\t"<<na;

}

};



void main()

{

clrscr();

fstream fp,temp;//objects to control files.

student stu;

int ch=0;

while(ch!=5)

{

clrscr();

cout<<"\n1. Add Student";

cout<<"\n2. List all";

cout<<"\n3. Edit";

cout<<"\n4. Delete";

cout<<"\n5. Exit";

cout<<"\n Enter your choice: ";

cin>>ch;

switch(ch)

{

case 1:

fp.open("mystu.txt",ios::app);

char ch1='y';

while(ch1=='y' || ch1=='Y')

{

cout<<"\n Enter Student details";

stu.getdata();

fp.write((char*)&stu,sizeof(stu));

cout<<"\n Do you want to add more records ";

fflush(stdin);

cin>>ch1;





}

fp.close();

break;



case 2:

clrscr();

int r=1;

fp.open("mystu.txt",ios::in);

cout<<"\nRoll Number\t\t\tName";

while(fp.read((char*)&stu,sizeof(stu)))

{

if(r>10)

{

cout<<"\n Press any key to cont...";

getch();

clrscr();

cout<<"\nRoll Number\t\t\tName";

r=1;

}

else

{

stu.show();

r++;

}

}

getch();

fp.close();



break;



case 3:

fp.open("mystu.txt",ios::in);

temp.open("temp.txt",ios::out);

int rl;

int fl=0;

char cnf='y';

cout<<"\n Enter roll number you want to edit";

cin>>rl;

while(fp.read((char*)&stu,sizeof(stu)))

{

if(stu.rlno==rl)

{

stu.show();

cout<<"\n Press y to edit or any key to ignore Update record";

fflush(stdin);

cin>>cnf;



if(cnf=='y' || cnf=='Y')

{

stu.getdata();

temp.write((char*)&stu,sizeof(stu));

fl=1;

}

else

{

temp.write((char*)&stu,sizeof(stu));

}

}

else

{



temp.write((char*)&stu,sizeof(stu));

}

}

if(fl==1)

cout<<"\n Record Updated";

else

cout<<"\n No Matching record found or Update ignored by user";

getch();



fp.close();

temp.close();

remove("mystu.txt");

rename("temp.txt","mystu.txt");



break;



case 4:



fp.open("mystu.txt",ios::in);

temp.open("temp.txt",ios::out);

int rld;

int fld=0;

char cnfd='y';

cout<<"\n Enter roll number you want to edit";

cin>>rld;

while(fp.read((char*)&stu,sizeof(stu)))

{

if(stu.rlno==rld)

{

stu.show();

cout<<"\n Press y to Delete or any key to save record";

fflush(stdin);

cin>>cnfd;



if(cnfd=='y' || cnfd=='Y')

{



fld=1;

}

else

{

temp.write((char*)&stu,sizeof(stu));

}

}

else

{



temp.write((char*)&stu,sizeof(stu));

}

}

if(fld==1)

cout<<"\n Record Deleted";

else

cout<<"\n No Matching record found or Saved by User";

getch();



fp.close();

temp.close();

remove("mystu.txt");

rename("temp.txt","mystu.txt");





break;



case 5:

cout<<"\n Thanks for using our software";

getch();

exit(0);

break;



default:

cout<<"\n Invalid Option";

getch();





}//main switch



}//while loop



}//main























No comments:

Post a Comment