Saturday, 23 June 2012

write a c++ program to create a class student

/*write a c++ program to create a class student having data member
-rollno
-name
-marks
write a necessary member function
1.to accept details of student store it into file "school.dat"
2.to read the details from file and display it
3.to update a given records into a file*/
#include<conio.h>
#include<string.h>
#include<iostream.h>
#include<iomanip.h>
#include<fstream.h>
class student
{
int rno,marks;
char name[20];
public:void accept()
       {
       cout<<"\nenter rollno:-";
       cin>>rno;
       cout<<"\nenter name:-";
       cin>>name;
       cout<<"\nenter marks:-";
       cin>>marks;
       }
       void display()
       {
       cout<<"\nrollno:-"<<rno;
       cout<<"\nname:-"<<name;
       cout<<"\nmarks:-"<<marks;
       }
       void update(char n[20])
       {
       if(strcmp(name,n)==0)
       {
       cout<<"\nenter the roll number:-";
       cin>>rno;
       cout<<"\nenter the marks:-";
       cin>>marks;
       }
       }
};
int main()
{
fstream f;
int i;
char n[20];
clrscr();
f.open("school.dat",ios::trunc|ios::in|ios::out);
student s[3];
for(i=0;i<3;i++)
{
s[i].accept();
f.write((char *)&s[i],sizeof(s[i]));
}
cout<<"\nenter the student name whose records is to be update:-";
cin>>n;
for(i=0;i<3;i++)
{
s[i].update(n);
f.write((char *)&s[i],sizeof(s[i]));
}
for(i=0;i<3;i++)
{
f.read((char *)&s[i],sizeof(s[i]));
s[i].display();
}
getch();
return 0;
}
/*

enter rollno:-123                                                              
                                                                               
enter name:-peer                                                               
                                                                               
enter marks:-23                                                                
                                                                               
enter rollno:-123                                                              
                                                                               
enter name:-ass                                                                
                                                                               
enter marks:-56                                                                
                                                                               
enter rollno:-465                                                              
                                                                               
enter name:-dfg                                                                
                                                                               
enter marks:-35                                                                
                                                                               
enter the student name whose records is to be update:-ass                      

enter the roll number:-67                                                      
                                                                               
enter the marks:-76                                                            
                                                                               
rollno:-123                                                                    
name:-peer                                                                     
marks:-23                                                                      
rollno:-67                                                                     
name:-ass                                                                      
marks:-76                                                                      
rollno:-465                                                                    
name:-dfg                                                                      
marks:-35
*/

No comments: