001 #include<iostream> 002 #include<fstream> 003 #include<iomanip> 004 using namespace std; 005 006 007 class student 008 { 009 protected: 010 char name[50]; 011 // int p_marks, c_marks, m_marks, e_marks, cs_marks; 012 // double per; 013 // char grade; 014 // void calculate(); //function to calculate grade 015 public: 016 int rollno; 017 virtual void getdata(); //function to accept data from user 018 virtual void showdata() ; //function to show data on screen 019 // friend void show_tabular(); 020 int retrollno() ; 021 }; //class ends here 022 023 class marks:public student 024 { 025 int p_marks, c_marks, m_marks, e_marks, cs_marks; 026 double per; 027 char grade; 028 void calculate(); 029 public: 030 //int retrollno(); 031 void getdata(); 032 void showdata(); 033 //friend void show_tabular(); 034 035 }; 036 037 038 void marks::calculate() 039 { 040 per=(p_marks+c_marks+m_marks+e_marks+cs_marks)/5.0; 041 if(per>=60) 042 grade='A'; 043 else if(per>=50) 044 grade='B'; 045 else if(per>=33) 046 grade='C'; 047 else 048 grade='F'; 049 } 050 051 void student::getdata() 052 { 053 cout<<"\nEnter The roll number of student "; 054 cin>>rollno; 055 cout<<"\n\nEnter The Name of student "; 056 057 cin>>name; 058 } 059 void marks::getdata() 060 { 061 062 cout<<"\nEnter The marks in physics out of 100 : "; 063 cin>>p_marks; 064 cout<<"\nEnter The marks in chemistry out of 100 : "; 065 cin>>c_marks; 066 cout<<"\nEnter The marks in maths out of 100 : "; 067 cin>>m_marks; 068 cout<<"\nEnter The marks in english out of 100 : "; 069 cin>>e_marks; 070 cout<<"\nEnter The marks in computer science out of 100 : "; 071 cin>>cs_marks; 072 calculate(); 073 } 074 075 void marks::calculate() 076 { 077 per=(p_marks+c_marks+m_marks+e_marks+cs_marks)/5.0; 078 if(per>=60) 079 grade='A'; 080 else if(per>=50) 081 grade='B'; 082 else if(per>=33) 083 grade='C'; 084 else 085 grade='F'; 086 } 087 088 void student::getdata() 089 { 090 cout<<"\nEnter The roll number of student "; 091 cin>>rollno; 092 cout<<"\n\nEnter The Name of student "; 093 094 cin>>name; 095 } 096 void marks::getdata() 097 { 098 099 cout<<"\nEnter The marks in physics out of 100 : "; 100 cin>>p_marks; 101 cout<<"\nEnter The marks in chemistry out of 100 : "; 102 cin>>c_marks; 103 cout<<"\nEnter The marks in maths out of 100 : "; 104 cin>>m_marks; 105 cout<<"\nEnter The marks in english out of 100 : "; 106 cin>>e_marks; 107 cout<<"\nEnter The marks in computer science out of 100 : "; 108 cin>>cs_marks; 109 calculate(); 110 } 111 112 void student::showdata() 113 { 114 cout<<"\nRoll number of student : "<<rollno; 115 cout<<"\nName of student : "<<name; 116 } 117 void marks::showdata() 118 { 119 cout<<"\nMarks in Physics : "<<p_marks; 120 cout<<"\nMarks in Chemistry : "<<c_marks; 121 cout<<"\nMarks in Maths : "<<m_marks; 122 cout<<"\nMarks in English : "<<e_marks; 123 cout<<"\nMarks in Computer Science :"<<cs_marks; 124 cout<<"\nPercentage of student is :"<<per; 125 cout<<"\nGrade of student is :"<<grade; 126 } 127 128 //show_tabular() 129 //{ 130 // cout<<rollno<<setw(6)<<" "<<name<<setw(10)<<p_marks<<setw(4)<<c_marks<<setw(4)<<m_marks<<setw(4) 131 // <<e_marks<<setw(4)<<cs_marks<<setw(8)<<per<<setw(6)<<grade<<endl; 132 //} 133 134 int student::retrollno() 135 { 136 return rollno; 137 } 138 139 140 //*************************************************************** 141 // function declaration 142 //**************************************************************** 143 144 void write_student(); //write the record in binary file 145 void display_all(); //read all records from binary file 146 void display_sp(int); //accept rollno and read record from binary file 147 void modify_student(int); //accept rollno and update record of binary file 148 void delete_student(int); //accept rollno and delete selected records from binary file 149 void class_result(); //display all records in tabular format from binary file 150 void result(); //display result menu 151 void intro(); //display welcome screen 152 void entry_menu(); //display entry menu on screen 153 154 155 //*************************************************************** 156 // THE MAIN FUNCTION OF PROGRAM 157 //**************************************************************** 158 159 160 int main() 161 { 162 char ch; 163 cout.setf(ios::fixed|ios::showpoint); 164 cout<<setprecision(2); // program outputs decimal number to two decimal places 165 intro(); 166 do 167 { 168 169 cout<<"\n\n\n\tMAIN MENU"; 170 cout<<"\n\n\t01. RESULT MENU"; 171 cout<<"\n\n\t02. ENTRY/EDIT MENU"; 172 cout<<"\n\n\t03. EXIT"; 173 cout<<"\n\n\tPlease Select Your Option (1-3) "; 174 cin>>ch; 175 switch(ch) 176 177 { 178 case '1': result(); 179 break; 180 case '2': entry_menu(); 181 break; 182 case '3': 183 break; 184 default :cout<<"\a"; 185 } 186 }while(ch!='3'); 187 return 0; 188 } 189 190 //*************************************************************** 191 // function to write in file 192 //**************************************************************** 193 194 void write_student() 195 { 196 marks st; 197 student st2; 198 ofstream outFile; 199 outFile.open("student.dat",ios::binary|ios::app); 200 st2.getdata(); 201 st.getdata(); 202 outFile.write(reinterpret_cast<char> (&st), sizeof(student)); 203 outFile.close(); 204 cout<<"\n\nStudent record Has Been Created "; 205 cin.ignore(); 206 cin.get(); 207 } 208 209 //*************************************************************** 210 // function to read all records from file 211 //****************************************************************void display_all() 212 { 213 student st2; 214 marks st; 215 ifstream inFile; 216 inFile.open("student.dat",ios::binary); 217 if(!inFile) 218 { 219 cout<<"File could not be open !! Press any Key..."; 220 cin.ignore(); 221 cin.get(); 222 return; 223 } 224 cout<<"\n\n\n\t\tDISPLAY ALL RECORD !!!\n\n"; 225 while(inFile.read(reinterpret_cast<char> (&st), sizeof(student))) 226 { 227 st2.showdata(); 228 st.showdata(); 229 cout<<"\n\n====================================\n"; 230 } 231 inFile.close(); 232 cin.ignore(); 233 cin.get(); 234 } 235 236 //*************************************************************** 237 // function to read specific record from file 238 //**************************************************************** 239 240 void display_sp(int n) 241 { 242 cout<<n<<"\n"; 243 marks st; 244 student st2; 245 ifstream inFile; 246 inFile.open("student.dat",ios::binary); 247 if(!inFile) 248 { 249 cout<<"File could not be open !! Press any Key..."; 250 cin.ignore(); 251 cin.get(); 252 return; 253 } 254 bool flag=false; 255 while(inFile.read(reinterpret_cast<char> (&st), sizeof(student))) 256 { 257 cout<<"THEEEEEEEEEEEEE\n";// i did these two lines to know where im wrong 258 cout<<st.rollno<<"\n";//the ouput of this line is 49 no matter which roll no is entered 259 if(st2.retrollno()==n) 260 { 261 cout<<st2.rollno; 262 st2.showdata(); 263 st.showdata(); 264 flag=true; 265 } 266 } 267 inFile.close(); 268 if(flag==false) 269 cout<<"\n\nrecord not exist"; 270 cin.ignore(); 271 cin.get(); 272 } 273 274 //*************************************************************** 275 // function to modify record of file 276 //**************************************************************** 277 278 void modify_student(int n) 279 { 280 bool found=false; 281 marks st; 282 student st2; 283 fstream File; 284 File.open("student.dat",ios::binary|ios::in|ios::out); 285 if(!File) 286 { 287 cout<<"File could not be open !! Press any Key..."; 288 cin.ignore(); 289 cin.get(); 290 return; 291 } 292 while(!File.eof() && found==false) 293 { 294 295 File.read(reinterpret_cast<char> (&st), sizeof(student)); 296 if(st.retrollno()==n) 297 { 298 st2.showdata(); 299 st.showdata(); 300 cout<<"\n\nPlease Enter The New Details of student"<<endl; 301 st2.getdata(); 302 st.getdata(); 303 int pos=(-1)*static_cast<int>(sizeof(st)); 304 File.seekp(pos,ios::cur); 305 File.write(reinterpret_cast<char> (&st), sizeof(student)); 306 cout<<"\n\n\t Record Updated"; 307 found=true; 308 } 309 } 310 File.close(); 311 if(found==false) 312 cout<<"\n\n Record Not Found "; 313 cin.ignore(); 314 cin.get(); 315 } 316 317 //*************************************************************** 318 // function to delete record of file 319 //**************************************************************** 320 321 void delete_student(int n) 322 { 323 324 marks st; 325 ifstream inFile; 326 inFile.open("student.dat",ios::binary); 327 if(!inFile) 328 { 329 cout<<"File could not be open !! Press any Key..."; 330 cin.ignore(); 331 cin.get(); 332 return; 333 } 334 ofstream outFile; 335 outFile.open("Temp.dat",ios::out); 336 inFile.seekg(0,ios::beg); 337 while(inFile.read(reinterpret_cast<char> (&st), sizeof(student))) 338 { 339 if(st.retrollno()!=n) 340 { 341 outFile.write(reinterpret_cast<char> (&st), sizeof(student)); 342 } 343 } 344 outFile.close(); 345 inFile.close(); 346 remove("student.dat"); 347 rename("Temp.dat","student.dat"); 348 cout<<"\n\n\tRecord Deleted .."; 349 cin.ignore(); 350 cin.get(); 351 } 352 353 //*************************************************************** 354 // function to display all students grade report 355 //**************************************************************** 356 357 void class_result() 358 { 359 360 marks st; 361 ifstream inFile; 362 inFile.open("student.dat",ios::binary); 363 if(!inFile) 364 { 365 cout<<"File could not be open !! Press any Key..."; 366 cin.ignore(); 367 cin.get(); 368 return; 369 } 370 cout<<"\n\n\t\tALL STUDENTS RESULT \n\n"; 371 cout<<"==========================================================\n"; 372 cout<<"R.No Name P C M E CS %age Grade"<<endl; 373 cout<<"==========================================================\n"; 374 //while(inFile.read(reinterpret_cast<char> (&st), sizeof(student))) 375 //{ 376 // st.show_tabular(); 377 //} 378 cin.ignore(); 379 cin.get(); 380 inFile.close(); 381 } 382 383 //*************************************************************** 384 // function to display result menu 385 //**************************************************************** 386 387 void result() 388 { 389 char ch; 390 int rno; 391 marks st; 392 cout<<"\n\n\n\tRESULT MENU"; 393 cout<<"\n\n\n\t1. Class Result"; cout<<"\n\n\t2. Student Report Card"; 394 cout<<"\n\n\t3. Back to Main Menu"; 395 cout<<"\n\n\n\tEnter Choice (1/2/3)? "; 396 cin>>ch; 397 398 switch(ch) 399 { 400 case '1' : class_result(); break; 401 case '2' : cout<<"\n\n\tEnter Roll Number Of Student : "; cin>>rno;st.rollno=rno; 402 display_sp(rno); break; 403 case '3' : break; 404 default: cout<<"\a"; 405 } 406 } 407 408 //*************************************************************** 409 // INTRODUCTION FUNCTION 410 //**************************************************************** 411 412 void intro() 413 { 414 cout<<"\n\n\n\t\t STUDENT"; 415 cout<<"\n\n\t\tREPORT CARD"; 416 cout<<"\n\n\t\t PROJECT"; 417 418 cout<<"\n\n\n\tMADE BY : SULABH AGRAWAL"; 419 cout<<"\n\tSCHOOL : CAMBRIDGE SCHOOL"; 420 cin.get(); 421 } 422 423 //*************************************************************** 424 // ENTRY / EDIT MENU FUNCTION 425 //**************************************************************** 426 427 void entry_menu() 428 { 429 char ch; 430 int num; 431 cout<<"\n\n\n\tENTRY MENU"; 432 cout<<"\n\n\t1.CREATE STUDENT RECORD"; 433 cout<<"\n\n\t2.DISPLAY ALL STUDENTS RECORDS"; 434 cout<<"\n\n\t3.SEARCH STUDENT RECORD "; 435 cout<<"\n\n\t4.MODIFY STUDENT RECORD"; 436 cout<<"\n\n\t5.DELETE STUDENT RECORD"; 437 cout<<"\n\n\t6.BACK TO MAIN MENU"; 438 cout<<"\n\n\tPlease Enter Your Choice (1-6) "; 439 cin>>ch; 440 441 switch(ch) 442 { 443 case '1': write_student(); break; 444 case '2': display_all(); break; 445 case '3': cout<<"\n\n\tPlease Enter The roll number "; cin>>num; 446 display_sp(num); break; 447 case '4': cout<<"\n\n\tPlease Enter The roll number "; cin>>num; 448 modify_student(num);break; 449 case '5': cout<<"\n\n\tPlease Enter The roll number "; cin>>num; 450 delete_student(num);break; 451 case '6': break; 452 default: cout<<"\a"; entry_menu(); 453 } 454 }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)