Hi mng ! Chuyện là mình đang làm bài tập lớn sau khi code xong nó bị lỗi kí tự khi hiển thị không biết sai ở đâu anh chị em ở đây ai biết có thể giúp mình đc không ạ mình xin cảm ơn!!
đoạn code:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
struct SinhVien {
string maSV;
string tenSV;
string ngaySinh;
bool gioiTinh;
float diemTB;
};
void nhapDanhSachSinhVien() {
ofstream fileOut("sv.dat", ios::binary);
if (!fileOut) {
cout << "Khong the mo file de ghi du lieu!" << endl;
return;
}
int n;
cout << "Nhap so luong sinh vien: ";
cin >> n;
for (int i = 0; i < n; i++) {
SinhVien sv;
cout << "Nhap thong tin sinh vien thu " << i + 1 << ":" << endl;
cout << "Ma sinh vien: ";
cin >> sv.maSV;
cout << "Ten sinh vien: ";
cin.ignore();
getline(cin, sv.tenSV);
cout << "Ngay sinh (dd/mm/yyyy): ";
cin >> sv.ngaySinh;
cout << "Gioi tinh (1: Nam, 0: Nu): ";
cin >> sv.gioiTinh;
cout << "Diem trung binh: ";
cin >> sv.diemTB;
fileOut.write((char*)&sv, sizeof(sv));
}
fileOut.close();
}
void hienThiDanhSachSinhVien() {
ifstream fileIn("sv.dat", ios::binary);
if (!fileIn) {
cout << "Khong the mo file de doc du lieu!" << endl;
return;
}
cout << "Danh sach sinh vien:" << endl;
cout << "MaSV\tTenSV\tNgaySinh\tGioiTinh\tDiemTB" << endl;
SinhVien sv;
while (fileIn.read((char*)&sv, sizeof(sv))) {
cout << sv.maSV << "\t" << sv.tenSV << "\t" << sv.ngaySinh << "\t";
if (sv.gioiTinh) {
cout << "Nam\t\t";
} else {
cout << "Nu\t\t";
}
cout << sv.diemTB << endl;
}
fileIn.close();
}
bool soSanhDiemTBGiamDan(SinhVien sv1, SinhVien sv2) {
return sv1.diemTB > sv2.diemTB;
}
void sapXepDanhSachSinhVien() {
ifstream fileIn("sv.dat", ios::binary);
if (!fileIn) {
cout << "Khong the mo file de doc du lieu!" << endl;
return;
}
vector<SinhVien> danhSachSinhVien;
SinhVien sv;
while (fileIn.read((char*)&sv, sizeof(sv))) {
danhSachSinhVien.push_back(sv);
}
sort(danhSachSinhVien.begin(), danhSachSinhVien.end(), soSanhDiemTBGiamDan);
fileIn.close();
ofstream fileOut("sv.dat", ios::binary);
if (!fileOut) {
cout << "Khong the mo file de ghi du lieu!" << endl;
return;
}
for (int i = 0; i < danhSachSinhVien.size(); i++) {
fileOut.write((char*)&danhSachSinhVien[i], sizeof(danhSachSinhVien[i]));
}
fileOut.close();
}
void timSinhVienDiemTBThapNhat() {
ifstream fileIn("sv.dat", ios::binary);
if (!fileIn) {
cout << "Khong the mo file de doc du lieu!" << endl;
return;
}
SinhVien svMin;
bool isFirst = true;
SinhVien sv;
while (fileIn.read((char*)&sv, sizeof(sv))) {
if (isFirst || sv.diemTB < svMin.diemTB) {
svMin = sv;
isFirst = false;
}
}
fileIn.close();
cout << "Sinh vien co diem trung binh thap nhat:" << endl;
cout << "MaSV\tTenSV\tNgaySinh\tGioiTinh\tDiemTB" << endl;
cout << svMin.maSV << "\t" << svMin.tenSV << "\t" << svMin.ngaySinh << "\t";
if (svMin.gioiTinh) {
cout << "Nam\t\t";
} else {
cout << "Nu\t\t";
}
cout << svMin.diemTB << endl;
}
int main() {
int choice;
do {
cout << "MENU" << endl;
cout << "1. Nhap danh sach sinh vien" << endl;
cout << "2. Hien thi danh sach sinh vien" << endl;
cout << "3. Sap xep danh sach sinh vien theo diem trung binh giam dan" << endl;
cout << "4. Tim sinh vien co diem trung binh thap nhat" << endl;
cout << "0. Thoat" << endl;
cout << "Nhap lua chon cua ban: ";
cin >> choice;
switch (choice) {
case 1:
nhapDanhSachSinhVien();
break;
case 2:
hienThiDanhSachSinhVien();
break;
case 3:
sapXepDanhSachSinhVien();
hienThiDanhSachSinhVien();
break;
case 4:
timSinhVienDiemTBThapNhat();
break;
case 0:
cout << "Tam biet!" << endl;
break;
default:
cout << "Lua chon khong hop le!" << endl;
break;
}
} while (choice != 0);
return 0;
}
Phần lỗi