Read text file into array of int, float and strings

I have a text file, data.txt

1
2
3
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.

I get error in line 39.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <fstream>
#include <string>

using namespace 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;
}
Last edited on
Topic archived. No new replies allowed.