trouble with array and files

Here is my assignment
im having trouble with the reading the file function


Let’s consider an input file that contains student id (int), first name (string), last name (string), and grade (float). We assume that the file contains between 1 and 100 grades.

Write a program that:

• Declare the structure named student composed of id (int), first (string), last (string), and grade (float). • Asks the user for the name of the input file. • Reads the grades from the file to fill up an array of type student. This file should be out of order on purpose. • Calculates and displays the maximum grade in the array with name. • Calculates and displays the minimum grade in the array with name. • Calculates and displays the average grade • Calculates and displays how many students were processed. • Produce a file with the information above based on a file name provided by the user. • The main idea is to explore the benefits of using structs. So every task should be done after the whole array is filled up • The files must contain at least 5 records • All items must be done with functions w/ parameters.
// asks the user for a file name and returns the file name string getFileName () { // To be completed
}




// fill out array using data from the file void fillArrayFromFile (ifstream& ins, student studentArray[],int& numberOfStudents) { // To be completed
}



// calculate the maximum grade in the array float calculateMaximumGrade (student studArray[],int numberOfStudents) { // To be completed
}
// calculate the minimum grade in the array float calculateMinimumGrade (student studArray[],int numberOfStudents) { // To be completed
}
// calculate the average grade in the array float calculateAverageGrade (student studArray[],int numberOfStudents) { // To be completed
}






*****************************************
here is what ive worked on so far.




#include<iostream>
#include<string>
#include<fstream>
#include <iomanip>

using namespace std;

struct student {
int id;
string first;
string last;
float grade;

};

// asks the user for a file name and returns the file name
string getFileName()
{

ifstream ins; // declare stream variable name ins


string filename;

cout << "Please enter a file name to open: ";
getline(cin, filename);

return filename;

}







// fill out array using data from the file
void fillArrayFromFile(ifstream& ins, student studentArray[], int& numberOfStudents)
{
double sum;
int count;

if (ins.fail()) {
cout << "Oops, something went wrong";
exit(1);
}


ins.open(filename); // open file

ins >> id>> first >> last >> grade; // read in data from file

while (!ins.eof( )) //if not at end of file, continue reading numbers
{

cout << firstname << " " << lastname << " " << " Grade " << grade << endl; //print numbers to screen
sum=sum+grade;
ins >> id>>firstname >> lastname >> grade; //get next number from file

count++;
}
ins.close( );








}










// calculate the maximum grade in the array
float calculateMaximumGrade(student studArray[], int
numberOfStudents)
{
// To be completed
}










// calculate the minimum grade in the array
float calculateMinimumGrade(student studArray[], int
numberOfStudents)
{
// To be completed
}










// calculate the average grade in the array
float calculateAverageGrade(student studArray[], int
numberOfStudents)
{
// To be completed
}




int main() {








system("pause");
return 0;
}



my question is how do i use use the name of the file in the fillArrayFromFile(), when its being used in another fucntion getFileName();

and how do i use my array for this part, with regards to my parameters in fillArrayFromFile(ifstream& ins, student studentArray[], int& numberOfStudents)

also how do i use the variables in the fillArrayFromFile by using a structure
As I understand the assignment you call getFileName in main and store the filename in a variable. Then you create an array of students, open a stream and pass the array and the stream to fillArrayFromFile. Then you pass the array to calculateMinimumGrade, calculateMaximumGrade and calculateAverageGrade storing their return values also in main. Finally you get the name of the output file and save the results there.
closed account (48T7M4Gy)
http://www.cplusplus.com/forum/general/204136/
Topic archived. No new replies allowed.