4 1 13 3 2
1.1 4.1 8.1 5.2 2.3
the student is in class
I want to read this text file lines into an array of 5 indices, ex: myarr[5];
and then, after I store those 5 ints, flots and strings, I want to find the largest value, for numbers, I find greatest number and for strings, I find the one with the alphabetical order first.
I am using function template because I want to use generic data type so my lines are read without problems.
#include <iostream>
#include <fstream>
#include <string>
usingnamespace std;
template <class T>
T maxFun(T a[])
{
T maxValue = 0;
int temp = 0;
for (int i = 0; i < 5; i++)
{
if (a[i] > temp)
{
temp = a[i];
}
}
cout << "The largest value in this array is: " << temp;
}
int main()
{
string arr[5];
//reading what's in the file and inserting to function
string line;
ifstream myfile("data.txt");
if (myfile.is_open())
{
for (int i = 0; i < 5; i++)
{
myfile >> arr[i];
}
}
else cout << "Unable to open file";
//calling function
maxFun(arr[5]);
system("Pause");
return 0;
}