Hey guys,
i'm new here and i'm also new to C++. I have a task where I have the user input a file name and then the program loads it into an array of type struct then finds the top 3 values and outputs them. I am so stuck right now and don't know what to do?
the text files they give me have this text in it
s9000004 6 9 6 70
s7000001 10 10 12 75
s8000002 9 7 10 65
s8000004 6 9 6 50
s7000002 9 7 10 45
s7000004 6 9 6 50
s0000002 9 7 10 35
s0000004 6 9 6 50
s0000001 10 15 15 75
which is the student ID and three scores for assignments and the final exam score. Basically the program adds up the scores and finds the top three students and outputs them.
This is what the output should look like
Enter the filename of the student data file: in2.txt
There are 9 students in total.
Top Student List
s0000001 85.00
s7000001 77.00
s8000002 65.00
Press any key to continue . . .
and this is my code so far
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
const int TOP_NUM = 3;
struct studentType
{
string id; // unique id
float assignments[3]; // scores for 3 assignments - in the scope of [0,15]
float exam, total; // member exam is the exam score in [0, 100]; member total is the total score in [0, 100]
};
bool readStudents(string fName, studentType stuList[], int& size);
// pre: none
// post: read students id and scores into the one-dimentional array of struct: stuList
// The actual size of the array is returned by reference paramenter size
// this function returns false if an error occurred when opening or reading the specified file, otherwise returns true
void printStudents(const studentType stuList[], int size);
// pre: none
// post: print the id, and the total score in array StuList
// size stores the the number of elements in stuList
// declare other functions if necessary
int main()
{
studentType studentList[30]; // the array used for storing the information of students from a file
int numOfStudents = 0; // numOfStudents is the actual number of students in the data file
string fileName; // it stores the file name of the data file
// declare other variables if necessary
cout << "Enter the filename of the student data file: " << flush; // don't modify it
cin >> fileName; // don't modify it
cout << endl; // don't modify it
if (!readStudents(fileName, studentList, numOfStudents)) // don't modify it
cout << "Input file error." << endl; // don't modify it
else // don't modify it
{ // don't modify it
// write your code here for processing
// don't modify the rest code
if (numOfStudents >= 2)
cout << "There are " << numOfStudents << " students in total. " << endl << endl; // don't modify it
else // don't modify it
cout << "There is " << numOfStudents << " student in total. " << endl << endl; // don't modify it; there is 1 student
cout << "Top Student List" << endl; // don't modify it
// write your code to diplay top students
} // don't modify it
cout << endl; // don't modify it
system("pause"); // don't modify it
return 0; // don't modify it
}
void printStudents(const studentType stuList[], int size);
{
}
bool readStudents(string fName, studentType stuList[], int& size);
{
}
I'm not sure where to go from here. I'm not sure what to write in the functions?
can anybody help me?