I'm doing excercises to do with functions for college and one of the questions is
A function MaxScore that may be passed the name of a file that contains a set of positive
integer scores and will return the largest score in the file.
I tried to send the file down to the function and loop through the elements of the file to find the largest.
The problem is im not sure how to actually pass the file down to the function so I can work with the elemements inside it.
I know this is totally wrong but I'm only trying to give an idea of what I'm trying to do. This is my first time using a file in a function, I would appreciate if I could be told what I'm doing wrong.
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
usingnamespace std;
int MaxScore(ifstream& infile, int size);
int main()
{
int array[12];
ifstream infile("array.dat"); //Opens file "Array.dat" for array Initialization.
ofstream outputFile("output.dat"); //Opens file "output.dat" file for array value storage.
for (int i = 0; i < 12; i++)
infile >> array[i]; //Populates elements "array" with data from "Array.dat" file.
MaxScore(infile, 12);
cout << " The largest number in the file = " << MaxScore(infile, 12);
}
int MaxScore(ifstream& infile, int size)
{
int number = infile[0];
for (int i = 0; i < 12; i++)
{
if (infile[i] > number)
number = infile[i]; // Gets largest number in the file.
}
return number;
}
int MaxScore(string fileName)
{
// Your code here
}
int main()
{
string file="array.dat";
cout << " The largest number in the file = " << MaxScore(file) << endl;
return 0;
}