The Code given below can be used in TURBO C++ Compilers: -
#include <iostream.h>
#include <conio.h>
#include <stdio.h>
#include <string.h>
#include <fstream.h>
#include <stdlib.h>
class student
{
int roll_no;
char name[25];
char Class[4];
float marks;
char grade;
public:
void getdata()
{
cout << "\nEnter Student roll no : ";
cin >> roll_no;
cout << "\nEnter Student name: ";
cin >> name;
cout << "\nEnter Student class : ";
cin >> Class;
cout << "\nEnter Student marks : ";
cin >> marks;
if (marks >= 75)
grade = 'A';
else if (marks >= 60)
grade = 'B';
else if (marks >= 50)
grade = 'C';
else if (marks >= 40)
grade = 'D';
else
grade = 'F';
} // getdata() ends
int getrno()
{
return roll_no;
}
void putdata()
{
cout << "\nStudent roll no : " << roll_no;
cout << "\nStudent name: " << name;
cout << "\nStudent class : " << Class;
cout << "\nStudent marks : " << marks;
cout << "\nStudent grade: " << grade << "\n";
}
void modify()
{
cout << "Original details: \n";
cout << "Roll no : " << roll_no << "\n";
cout << "Name : " << name << "\n";
cout << "Class : " << Class << "\n";
cout << "Marks: " << marks << "\n";
cout << "Grade : " << grade << "\n";
// S.putdata();
char gr, nm[25], cl[4];
float mks;
cout << "\nNew name: (Enter \'.\' to retain the old name) ";
cin >> nm;
cout << "\nNewclass: (Enter \'.\' to retain the old class) ";
cin >> cl;
cout << "\nNew marks: (Enter \'-1\' to retain the old marks) ";
cin >> mks;
if (strcmp(nm, ".") != 0)
{
strcpy(name, nm);
}
if (strcmp(cl, ".") != 0)
{
strcpy(Class, cl);
}
if (mks != -1)
{
marks = mks;
if (marks >= 75)
grade = 'A';
else if (marks >= 60)
grade = 'B';
else if (marks >= 50)
grade = 'C';
else if (marks >= 40)
grade = 'D';
else
grade = 'F';
}
}
} S;
void addrec()
{
ofstream fout;
fout.open("student.dat", ios::out | ios::binary);
char ch = 'Y';
if (!fout)
{
cout << "File does not exist!\n";
exit(0);
}
while (ch == 'Y' || ch == 'y')
{
S.getdata(); // class function
fout.write((char *)&S, sizeof(S));
cout << "\nMore records?[Y/N]";
cin >> ch;
} // while loop ends
fout.close();
} // addrec() ends
void displayrec()
{
ifstream fin;
fin.open("student.dat", ios::in | ios::binary);
if (!fin)
{
cout << "File not found!\n";
exit(0);
}
else
while (fin)
{
fin.read((char *)&S, sizeof(S));
if (fin.eof())
break;
S.putdata();
cout << "\n";
}
fin.close();
} // displayrec() ends
void recsearch()
{
ifstream fin;
fin.open("student.dat", ios::in | ios::binary);
if (!fin)
{
cout << "File doesn\'t exist.\n";
exit(0);
}
int rno, flag = 0;
cout << "Enter the roll no of Student to search his/her record: ";
cin >> rno;
while (fin)
{
fin.read((char *)&S, sizeof(S));
if (fin.eof())
break;
if (S.getrno() == rno)
{
S.putdata();
flag++;
}
} // while loop ends
if (flag == 0)
cout << "Record not found!";
fin.close();
} // research() ends
void Delete()
{
fstream fio("student.dat", ios::in | ios::out | ios::app | ios::binary);
ofstream fout("temp.dat", ios::out | ios::binary);
int rno;
char confirm = 'n';
int flag = 0;
cout << "Enter roll no of student whose record is to be deleted: ";
cin >> rno;
while (!fio.eof())
{
fio.read((char *)&S, sizeof(S));
if (fio.eof())
break;
if (S.getrno() == rno)
{
S.putdata();
flag = 1;
cout << "\nAre you sure you want to delete this record??[Y/N]";
cin >> confirm;
if (confirm == 'n' || confirm == 'N')
{
fout.write((char *)&S, sizeof(S));
}
}
else
{
fout.write((char *)&S, sizeof(S));
}
}
if (flag == 0)
{
cout << "Record not found!\n";
}
fio.close();
fout.close();
remove("student.dat");
rename("temp.dat", "student.dat");
fio.open("student.dat", ios::in);
cout << "The file now contains the following records: \n";
while (fio)
{
fio.read((char *)&S, sizeof(S));
if (fio.eof())
break;
S.putdata();
cout << "\n";
}
fio.close();
} // delete() ends
int main()
{
clrscr();
int ch1;
char ch = 'Y';
while (ch == 'Y' || ch == 'y')
{
cout << "Enter choice: \n";
cout << "1.To add records in the file. \n";
cout << "2.To display all the records \n";
cout << "3.To search a record stored in the file. \n";
cout << "4.To modify a record in the file.\n";
cout << "5.To delete a record in the file.\n";
cout << "6. Exit.\n";
cin >> ch1;
switch (ch1)
{
case 1:
{
addrec();
break;
}
case 2:
{
displayrec();
break;
}
case 3:
{
recsearch();
break;
}
case 4:
{
fstream fio;
fio.open("student.dat", ios::in | ios::out | ios::binary);
int rno;
long pos;
int flag = 0;
cout << "Enter roll no of student whose record is to be modified:\n";
cin >> rno;
while (!fio.eof())
{
pos = fio.tellg();
fio.read((char *)&S, sizeof(S));
if (S.getrno() == rno)
{
S.modify();
fio.seekg(pos);
fio.write((char *)&S, sizeof(S));
flag++;
break;
}
} // while ends
if (flag == 0)
{
cout << "record not found!!\n";
continue;
}
fio.seekg(0);
/*cout<<"Now the file contains\n";
while(fio)
{
if(fio.eof())
break;
fio.read((char*)&S,sizeof(S));
S.putdata();
}*/
cout << "Record Modified!\n";
fio.close();
break;
}
case 5:
{
Delete();
break;
}
case 6:
{
ch = 'N';
exit(0);
} // exit
} // switch ends
cout << "Would you like to continue? [Y/N]\n";
cin >> ch;
} // while ends
getch();
return 0;
} // main() ends;
The Code given below can be used in gcc/g++ Compilers: -
#include <iostream>
#include <string.h>
#include <fstream.h>
using namespace std;
class student
{
int roll_no;
char name[25];
char Class[4];
float marks;
char grade;
public:
void getdata()
{
cout << "\nEnter Student roll no : ";
cin >> roll_no;
cout << "\nEnter Student name: ";
cin >> name;
cout << "\nEnter Student class : ";
cin >> Class;
cout << "\nEnter Student marks : ";
cin >> marks;
if (marks >= 75)
{
grade = 'A';
}
else if (marks >= 60)
{
grade = 'B';
}
else if (marks >= 50)
{
grade = 'C';
}
else if (marks >= 40)
{
grade = 'D';
}
else
{
grade = 'F';
}
} // getdata() ends
int getrno()
{
return roll_no;
}
void putdata()
{
cout << "\nStudent roll no : " << roll_no;
cout << "\nStudent name: " << name;
cout << "\nStudent class : " << Class;
cout << "\nStudent marks : " << marks;
cout << "\nStudent grade: " << grade << "\n";
}
void modify()
{
cout << "Original details: \n";
cout << "Roll no : " << roll_no << "\n";
cout << "Name : " << name << "\n";
cout << "Class : " << Class << "\n";
cout << "Marks: " << marks << "\n";
cout << "Grade : " << grade << "\n";
// S.putdata();
char gr, nm[25], cl[4];
float mks;
cout << "\nNew name: (Enter \'.\' to retain the old name) ";
cin >> nm;
cout << "\nNewclass: (Enter \'.\' to retain the old class) ";
cin >> cl;
cout << "\nNew marks: (Enter \'-1\' to retain the old marks) ";
cin >> mks;
if (strcmp(nm, ".") != 0)
{
strcpy(name, nm);
}
if (strcmp(cl, ".") != 0)
{
strcpy(Class, cl);
}
if (mks != -1)
{
marks = mks;
if (marks >= 75)
{
grade = 'A';
}
else if (marks >= 60)
{
grade = 'B';
}
else if (marks >= 50)
{
grade = 'C';
}
else if (marks >= 40)
{
grade = 'D';
}
else
{
grade = 'F';
}
}
}
} S;
void addrec()
{
ofstream fout;
fout.open("student.dat", ios::out | ios::binary);
char ch = 'Y';
if (!fout)
{
cout << "File does not exist!\n";
exit(0);
}
while (ch == 'Y' || ch == 'y')
{
S.getdata(); // class function
fout.write((char *)&S, sizeof(S));
cout << "\nMore records?[Y/N]";
cin >> ch;
} // while loop ends
fout.close();
} // addrec() ends
void displayrec()
{
ifstream fin;
fin.open("student.dat", ios::in | ios::binary);
if (!fin)
{
cout << "File not found!\n";
exit(0);
}
else
while (fin)
{
fin.read((char *)&S, sizeof(S));
if (fin.eof())
{
break;
}
S.putdata();
cout << "\n";
}
fin.close();
} // displayrec() ends
void recsearch()
{
ifstream fin;
fin.open("student.dat", ios::in | ios::binary);
if (!fin)
{
cout << "File doesn\'t exist.\n";
exit(0);
}
int rno, flag = 0;
cout << "Enter the roll no of Student to search his/her record: ";
cin >> rno;
while (fin)
{
fin.read((char *)&S, sizeof(S));
if (fin.eof())
{
break;
}
if (S.getrno() == rno)
{
S.putdata();
flag++;
}
} // while loop ends
if (flag == 0)
cout << "Record not found!";
fin.close();
} // research() ends
void Delete()
{
fstream fio("student.dat", ios::in | ios::out | ios::app | ios::binary);
ofstream fout("temp.dat", ios::out | ios::binary);
int rno;
char confirm = 'n';
int flag = 0;
cout << "Enter roll no of student whose record is to be deleted: ";
cin >> rno;
while (!fio.eof())
{
fio.read((char *)&S, sizeof(S));
if (fio.eof())
{
break;
}
if (S.getrno() == rno)
{
S.putdata();
flag = 1;
cout << "\nAre you sure you want to delete this record??[Y/N]";
cin >> confirm;
if (confirm == 'n' || confirm == 'N')
{
fout.write((char *)&S, sizeof(S));
}
}
else
{
fout.write((char *)&S, sizeof(S));
}
}
if (flag == 0)
{
cout << "Record not found!\n";
}
fio.close();
fout.close();
remove("student.dat");
rename("temp.dat", "student.dat");
fio.open("student.dat", ios::in);
cout << "The file now contains the following records: \n";
while (fio)
{
fio.read((char *)&S, sizeof(S));
if (fio.eof())
{
break;
}
S.putdata();
cout << "\n";
}
fio.close();
} // delete() ends
int main()
{
int ch1;
char ch = 'Y';
while (ch == 'Y' || ch == 'y')
{
cout << "Enter choice: \n";
cout << "1.To add records in the file. \n";
cout << "2.To display all the records \n";
cout << "3.To search a record stored in the file. \n";
cout << "4.To modify a record in the file.\n";
cout << "5.To delete a record in the file.\n";
cout << "6. Exit.\n";
cin >> ch1;
switch (ch1)
{
case 1:
{
addrec();
break;
}
case 2:
{
displayrec();
break;
}
case 3:
{
recsearch();
break;
}
case 4:
{
fstream fio;
fio.open("student.dat", ios::in | ios::out | ios::binary);
int rno;
long pos;
int flag = 0;
cout << "Enter roll no of student whose record is to be modified:\n";
cin >> rno;
while (!fio.eof())
{
pos = fio.tellg();
fio.read((char *)&S, sizeof(S));
if (S.getrno() == rno)
{
S.modify();
fio.seekg(pos);
fio.write((char *)&S, sizeof(S));
flag++;
break;
}
} // while ends
if (flag == 0)
{
cout << "record not found!!\n";
continue;
}
fio.seekg(0);
cout << "Record Modified!\n";
fio.close();
break;
}
case 5:
{
Delete();
break;
}
case 6:
{
ch = 'N';
exit(0);
} // exit
} // switch ends
cout << "Would you like to continue? [Y/N]\n";
cin >> ch;
} // while ends
return 0;
} // main() ends;
#ENJOY CODING
Post a Comment
FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP