Mam trudności ze zrozumieniem, jak przekazać plik do funkcji.Jak przekazać plik do funkcji?
Mam plik z 20 nazwami i 20 wynikami testów, które muszą być odczytane przez funkcję. funkcja następnie przypisze imiona i wyniki do struktury zwanej uczniem.
moje pytanie brzmi: jak napisałbym wywołanie funkcji z odpowiednimi parametrami. ? aby moja funkcja odczytała dane w pliku. dzięki.
KOD
// 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;
}
na jaki problem napotykasz przy bieżącym kodzie? Uważam, że poprawnie przekazujesz parametry. – iammilind