Tôi gặp khó khăn trong việc hiểu cách chuyển một tệp vào một hàm.Làm cách nào để chuyển một tệp vào một hàm?
tôi có tệp có 20 tên và 20 điểm kiểm tra cần được đọc bởi hàm. sau đó hàm sẽ gán các tên và điểm số cho một cấu trúc gọi là sinh viên.
câu hỏi của tôi là làm thế nào tôi sẽ viết một cuộc gọi hàm với các tham số thích hợp. ? để làm cho hàm của tôi đọc dữ liệu trong tệp. cảm ơn.
MÃ
// ask user for student file
cout << "Enter the name of the file for the student data to be read for input" << endl;
cout << " (Note: The file and path cannot contain spaces)" << endl;
cout << endl;
cin >> inFileName;
inFile.open(inFileName);
cout << endl;
// FUNCTION CALL how do i set this up properly?
ReadStudentData(inFile, student, numStudents);
void ReadStudentData(ifstream& infile, StudentType student[], int& numStudents)
{
int index = 0;
string lastName, firstName;
int testScore;
while ((index < numStudents) &&
(infile >> lastName >> firstName >> testScore))
{
if (testScore >= 0 && testScore <= 100)
{
student[index].studentName = lastName + ", " + firstName;
student[index].testScore = testScore;
index++;
}
}
numStudents = index;
}
sự cố bạn đang gặp phải với mã hiện tại của mình là gì? Tôi thấy rằng, bạn đang chuyển các thông số đúng cách. – iammilind