Mọi người cho em hỏi: khi em đọc mảng sinh viên, em để vòng lặp while(!filein.eof())
thì bị lỗi không thể thực hiện được ạ. Em mới học c++ nên mong mọi người chỉ giáo. Em cảm ơn.
Đề bài:
/*
hoang duy - 0949771945 - 16/11/1994 - 4.5 5.5 6.5
Tran Minh Cuong - 0917552182 - 18/12/1998 - 10 10 10
Minh Hoang - 0912155022 - 12/12/2004 - 9 9 9.5
Minh Vi - 0943219155 - 28/12/1996 - 6.5 10 9.7
*/
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
using namespace std;
struct sinhvien
{
string name, phone;
int day, month, year;
float toan, ly, hoa;
};
typedef struct sinhvien sv;
void doc1sinhvien(ifstream& filein, sv& x)
{
getline(filein, x.name, '-'); // đọc tới dầu gạch '-' => bị thừa 1 khoảng trắng => sau khi đọc getline con trỏ sẽ lùi về 1 vị trí => ở sau dấu '-'
x.name.erase(x.name.begin() + (x.name.length() - 1)); // xóa ký tự cuối cùng
filein.seekg(1, 1);
filein >> x.phone;
filein.seekg(3, 1);
filein >> x.day;
filein.seekg(1, 1);
filein >> x.month;
filein.seekg(1, 1);
filein >> x.year;
filein.seekg(3, 1);
filein >> x.toan >> x.ly >> x.hoa;
string temp; // đọc ký tự cuối xuống dòng
getline(filein, temp);
}
void ghi1sinhvien(ofstream& fileout, sv x)
{
fileout << "\nten: " << x.name;
fileout << "\nsdt: " << x.phone;
fileout << "\nSinh nhat: " << x.day << "/" << x.month << "/" << x.year;
fileout << "\ntoan: " << x.toan;
fileout << "\nly: " << x.ly;
fileout << "\nhoa: " << x.hoa;
fileout << "\n------------------------------------------";
}
void docmangsinhvien(ifstream& filein, vector<sv>& arr)
{
for (int i = 0; i < 4; i++)
{
sv x;
doc1sinhvien(filein, x);
arr.push_back(x);
}
filein.close();
}
void ghimangsinhvien(ofstream& fileout, vector<sv> arr)
{
for (int i = 0; i < arr.size(); i++)
{
ghi1sinhvien(fileout, arr[i]);
}
fileout.close();
}
float tinhdiemtrungbinh(sv x)
{
return (x.toan + x.ly + x.hoa) / 3;
}
void hoanvi(sv& x, sv& y)
{
sv temp;
temp = x;
x = y;
y = temp;
}
void giamdantheodiemtrungbinh(vector<sv> &arr)
{
for (int i = 0; i < arr.size() - 1; i++)
{
for (int j = i + 1; j < arr.size(); j++)
{
if (tinhdiemtrungbinh(arr[i]) < tinhdiemtrungbinh(arr[j]))
{
hoanvi(arr[i], arr[j]);
}
}
}
}
int main()
{
ifstream filein("input.txt");
vector<sv> arr;
docmangsinhvien(filein, arr);
ofstream fileout("output.txt");
giamdantheodiemtrungbinh(arr);
ghimangsinhvien(fileout, arr);
system("pause");
return 0;
}