Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

c++

#include<iostream>#include<fstream>#include<iomanip>using namespace std;// the class that stores dataclass student{int rollno;char name[50];int eng_marks, math_marks, sci_marks, lang2_marks, cs_marks;double average;char grade;public:void getdata();void showdata() const;void calculate();int retrollno() const;}; //class ends herevoid student::calculate(){average=(eng_marks+math_marks+sci_marks+lang2_marks+cs_marks)/5.0;if(average>=90)grade='A';else if(average>=75)grade='B';else if(average>=50)grade='C';elsegrade='F';}void student::getdata(){cout<<"
Enter student's roll number: ";cin>>rollno;cout<<"

Enter student name: ";cin.ignore();cin.getline(name,50);cout<<"
All marks should be out of 100";cout<<"
Enter marks in English: ";cin>>eng_marks;cout<<"
Enter marks in Math:  ";cin>>math_marks;cout<<"
Enter marks in Science:  ";cin>>sci_marks;cout<<"
Enter marks in 2nd language:  ";cin>>lang2_marks;cout<<"
Enter marks in Computer science:  ";cin>>cs_marks;calculate();}void student::showdata() const{cout<<"
Roll number of student : "<<rollno;cout<<"
Name of student : "<<name;cout<<"
English : "<<eng_marks;cout<<"
Maths : "<<math_marks;cout<<"
Science : "<<sci_marks;cout<<"
Language2 : "<<lang2_marks;cout<<"
Computer Science :"<<cs_marks;cout<<"
Average Marks :"<<average;cout<<"
Grade of student is :"<<grade;}int  student::retrollno() const{return rollno;}//function declarationvoid create_student();void display_sp(int);//display particular recordvoid display_all(); // display all recordsvoid delete_student(int);//delete particular recordvoid change_student(int);//edit particular record//MAINint main(){char ch;cout<<setprecision(2); do{char ch;int num;system("cls");cout<<"


	MENU";cout<<"

	1.Create student record";cout<<"

	2. Search student record";cout<<"

	3. Display all students records ";cout<<"

	4.Delete student record";cout<<"

	5.Modify student record";cout<<"

	6.Exit";cout<<"

What is your Choice (1/2/3/4/5/6) ";cin>>ch;system("cls");switch(ch){case '1': create_student(); break;case '2': cout<<"

	Enter The roll number "; cin>>num;display_sp(num); break;case '3': display_all(); break;case '4': cout<<"

	Enter The roll number: "; cin>>num;delete_student(num);break;case '5': cout<<"

	Enter The roll number "; cin>>num;change_student(num);break;case '6': cout<<"Exiting, Thank you!";exit(0);}}while(ch!='6');return 0;}//write student details to filevoid create_student(){student stud;ofstream oFile;oFile.open("student.dat",ios::binary|ios::app);stud.getdata();oFile.write(reinterpret_cast<char *> (&stud), sizeof(student));oFile.close();     cout<<"

Student record Has Been Created ";cin.ignore();cin.get();}// read file recordsvoid display_all(){student stud;ifstream inFile;inFile.open("student.dat",ios::binary);if(!inFile){cout<<"File could not be opened !! Press any Key to exit";cin.ignore();cin.get();return;}cout<<"


		DISPLAYING ALL RECORDS

";while(inFile.read(reinterpret_cast<char *> (&stud), sizeof(student))){st.showdata();cout<<"

====================================
";}inFile.close();cin.ignore();cin.get();}//read specific record based on roll numbervoid display_sp(int n){student stud;ifstream iFile;iFile.open("student.dat",ios::binary);if(!iFile){cout<<"File could not be opened... Press any Key to exit";cin.ignore();cin.get();return;}bool flag=false;while(iFile.read(reinterpret_cast<char *> (&stud), sizeof(student))){if(stud.retrollno()==n){  stud.showdata();flag=true;}}iFile.close();if(flag==false)cout<<"

record does not exist";cin.ignore();cin.get();}// modify record for specified roll numbervoid change_student(int n){bool found=false;student stud;fstream fl;fl.open("student.dat",ios::binary|ios::in|ios::out);if(!fl){cout<<"File could not be opened. Press any Key to exit...";cin.ignore();cin.get();return;}     while(!fl.eof() && found==false){fl.read(reinterpret_cast<char *> (&stud), sizeof(student));if(stud.retrollno()==n){stud.showdata();cout<<"
Enter new student details:"<<endl;stud.getdata();    int pos=(-1)*static_cast<int>(sizeof(stud));    fl.seekp(pos,ios::cur);    fl.write(reinterpret_cast<char *> (&stud), sizeof(student));    cout<<"

	 Record Updated";    found=true;}}File.close();if(found==false)cout<<"

 Record Not Found ";cin.ignore();cin.get();}//delete record with particular roll numbervoid delete_student(int n){student stud;ifstream iFile;iFile.open("student.dat",ios::binary);if(!iFile){cout<<"File could not be opened... Press any Key to exit...";cin.ignore();cin.get();return;}ofstream oFile;oFile.open("Temp.dat",ios::out);iFile.seekg(0,ios::beg);while(iFile.read(reinterpret_cast<char *> (&stud), sizeof(student))){if(stud.retrollno()!=n){oFile.write(reinterpret_cast<char *> (&stud), sizeof(student));}}oFile.close();iFile.close();remove("student.dat");rename("Temp.dat","student.dat");cout<<"

	Record Deleted ..";cin.ignore();cin.get();}
Source by hackr.io #
 
PREVIOUS NEXT
Tagged:
ADD COMMENT
Topic
Name
4+5 =