1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
|
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct Student {
int id; // Student ID
char name[30]; // Student Name
int points; // Points earned
float gpa; // GPA
};
int getOption () {
int selection;
cout << "Options :\n";
cout << "1) Initialize the student file\n";
cout << "2) Add a new student\n";
cout << "3) Display a student\n";
cout << "4) Update a student\n";
cout << "5) End\n\b";
cout << "Please select an option : ";
cin >> selection;
while (selection < 1 || selection > 5) {
cout << "Please select a number between 1-5\n";
cin >> selection;
}
return selection;
}
void UpdateStudent(fstream &fileStudents) {
Student student;
long recNum;
// Get the Student ID to change
cout << "Enter the student ID that you want to change:";
cin >> recNum;
// Move to the record and read it.
fileStudents.seekg(recNum * sizeof(student), ios::beg);
fileStudents.read(reinterpret_cast<char *>(&student),
sizeof(student));
// Display the record contents
cout << "Name : ";
cout << student.name << endl;
cout << "Points Earned : ";
cout << student.points << endl;
cout << "GPA : ";
cout << student.gpa << endl;
// Get the new record data.
cout << "Begin entering the new student data :\n";
cout << "Name : ";
cin.ignore();
cin.getline(student.name, 30);
cout << "Points Earned : ";
cin >> student.points;
cout << "GPA : ";
cin >> student.gpa;
// Set position to start of the student record
fileStudents.seekp(recNum * sizeof(student), ios::beg);
fileStudents.write(reinterpret_cast<char *>(&student),
sizeof(student));
}
void DisplayStudent(fstream &fileStudents) {
Student student;
long id;
// Get the student ID to display
cout << "Enter the student ID to view ";
cin >> id;
// Move to the record and read it.
fileStudents.seekg(id * sizeof(student), ios::beg);
fileStudents.read(reinterpret_cast<char *>(&student), sizeof(student));
// Show the student's data
cout << "ID : ";
cout << student.id << endl;
cout << "Name : ";
cout << student.name << endl;
cout << "Points earned: ";
cout << student.points << endl;
cout << "GPA : ";
cout << student.gpa << endl;
}
void AddStudent(fstream &fileStudents) {
cout << "Please enter the Student information : \n";
Student student;
cout << "Name : ";
cin.ignore();
cin.getline(student.name, 30);
cout << "Points earned : ";
cin >> student.points;
cout << "GPA : ";
cin >> student.gpa;
fileStudents.write(reinterpret_cast<char *>(&student),
sizeof(student));
}
void InitializeStudentFile(fstream &fileStudents) {
Student student = {0, " ", 0, 0.0};
for (int id = 0; id < 100; id++) {
fileStudents.write(reinterpret_cast<char *>(&student), sizeof(student));
}
}
int main(int argc, char * argv[]) {
int option;
// Create a random access file and initialize with 100 empty records
fstream fileStudents ("Students.dat", ios::out | ios::binary);
do {
option = getOption();
switch (option) {
case 1: InitializeStudentFile(fileStudents);
break;
case 2: AddStudent(fileStudents);
break;
case 3: DisplayStudent(fileStudents);
break;
case 4: UpdateStudent(fileStudents);
break;
case 5: cout << "bye\n";
}
} while (option != 5);
fileStudents.close();
return 0;
}
| |