double ScoreToGrade(double);
void displayOne (struct students *studinfo , int , int );
void displayAll (struct students *studinfo, int );
void IDsort (struct students *studinfo, int );
struct students
{
int id;
string name;
string course;
int credit;
int score;
double grade;
};
int main()
{
ifstream inputFile;
const int SIZE = 999;
students studinfo[SIZE];
int counter = 0;
int data = 0;
string data2 = "";
string data3 = "";
int data4 = 0;
int data5 = 0;
inputFile.open("StudentRecords.txt");
/*
12546 Amy CS1 4 81
13455 Bill CS1 4 76
14328 Jim CS1 4 64
12546 Amy CS2 4 90
13455 Bill CS2 4 85
14328 Jim CS2 4 71
12546 Amy CS3 3 90
13455 Bill CS3 3 75
14328 Jim CS3 3 69
14388 Henry CS3 3 80
15667 Peter CS3 3 45
*/
if (inputFile.is_open())
{
while (inputFile >> studinfo[counter].id >> studinfo[counter].name >> studinfo[counter].course >> studinfo[counter].credit >> studinfo[counter].score)
{
studinfo[counter].grade = ScoreToGrade(studinfo[counter].score);
counter++;
}
inputFile.close();
}
else cout << "Unable to open file";
It's actually not that hard once you read up on <vector>. Start by adding #include <vector> to your include list. Then, replace arrays like student* studentInfo with vector<student> studentInfo. The good thing about <vector> is that it overloads operator [] so you don't have to change your array resolution brackets to function calls.
#1 (presumably) passes a pointer to array. The function accesses the elements of the caller's array via the pointer. Function does not know how many elements the array has (needs additional argument).
#3 passes a reference to an "array". The vector has member function size() that has count of items in the array. The function accesses the elements of the caller's array via the reference.
#2 creates a copy of the array that the function uses. Caller's array will not be changed.
#4 a reference, like #3, but function cannot modify the array and its elements.
You do create SIZE students at the start of main(), but fill and use only 'counter' of them. If the input would have more than SIZE students, then you would have a problem.
The vector approach would be to create empty vector, optionally preallocate memory, but create only as many students as necessary.
1 2 3 4 5 6 7 8 9 10 11 12 13
std::vector<students> studs; // empty
studs.reserve( SIZE ); // optional, studs is still empty
students record;
while ( inputfile >> record ... ) {
studs.emplace_back( record );
}
for ( size_t i=0; i < studs.size(); ++i ) {
std::cout << studs[i].name ...
}