School Name Collect name of school Must not exceed 28 characters Emblem Choice C
ID: 3691612 • Letter: S
Question
School Name Collect name of school Must not exceed 28 characters Emblem Choice Collect the teacher's choice of Choice must be from the set emblem for this board A,R,C,G,H,N) Year Taught Collect the starting year of the Four digit year school year (e.g. 2015-2016 will just be 2015) Storing the Information: After the information is input and validated, it must be stored in a file. A file is provided called allOrders.txt and includes 99 orders that have already been collected and saved in the proper format. Once the teacher has entered all data and the program has validated the data, the program will show an ASCII depiction of the plaque and small Page 7 of 19Explanation / Answer
Solution: Please follow these coding as shown in below..
#include<iostream>
#include<fstream>
#include<cstdio>
using namespace std;
class Student
{
int admno;
char name[50];
public:
void setData()
{
cout << " Enter admission no. ";
cin >> admno;
cout << "Enter name of student ";
cin.getline(name,50);
}
void showData()
{
cout << " Admission no. : " << admno;
cout << " Student Name : " << name;
}
int retAdmno()
{
return admno;
}
};
/*
* function to write in a file.
*/
void write_record()
{
ofstream outFile;
outFile.open("student.dat", ios::binary | ios::app);
Student obj;
obj.setData();
outFile.write((char*)&obj, sizeof(obj));
outFile.close();
}
/*
* function to display records of file
*/
void display()
{
ifstream inFile;
inFile.open("student.dat", ios::binary);
Student obj;
while(inFile.read((char*)&obj, sizeof(obj)))
{
obj.showData();
}
inFile.close();
}
/*
* function to search and display from file
*/
void search(int n)
{
ifstream inFile;
inFile.open("student.dat", ios::binary);
Student obj;
while(inFile.read((char*)&obj, sizeof(obj)))
{
if(obj.retAdmno() == n)
{
obj.showData();
}
}
inFile.close();
}
/*
* function to delete a record
*/
void delete_record(int n)
{
Student obj;
ifstream inFile;
inFile.open("student.dat", ios::binary);
ofstream outFile;
outFile.open("temp.dat", ios::out | ios::binary);
while(inFile.read((char*)&obj, sizeof(obj)))
{
if(obj.retAdmno() != n)
{
outFile.write((char*)&obj, sizeof(obj));
}
}
inFile.close();
outFile.close();
remove("student.dat");
rename("temp.dat", "student.dat");
}
/*
* function to modify a record
*/
void modify_record(int n)
{
fstream file;
file.open("student.dat",ios::in | ios::out);
Student obj;
while(file.read((char*)&obj, sizeof(obj)))
{
if(obj.retAdmno() == n)
{
cout << " Enter the new details of student";
obj.setData();
int pos = -1 * sizeof(obj);
file.seekp(pos, ios::cur);
file.write((char*)&obj, sizeof(obj));
}
}
file.close();
}
int main()
{
//Store 5 records in file
for(int i = 1; i <= 5; i++)
write_record();
//Display all records
cout << " List of records";
display();
//Search record
cout << " Search result";
search(100);
//Delete record
delete_record(100);
cout << " Record Deleted";
//Modify record
cout << " Modify Record 101 ";
modify_record(101);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.