comparing strings

Hello cpp!

I have 3 arrays, each contains 5 values with 3 different datatypes. Int, float and string.

I want to print the largest value in them. Now, I got 2 problems and 1 question.

Problems:

1) In my code, the output for the first value of int array is 130, it should be 13.

2) When I run the function that calculates the largest value on my string array, the program just breaks.

questions:

1) How can I use for loop in my code to insert data.txt into my array instead of just doing it manually, which is stupid.

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


my code:
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
44
45
46
47
48
49
50
51
52
53
54
55
56
  #include <iostream>
#include <fstream>
#include <string>

using namespace std;

template <typename MyNewDataType>
MyNewDataType maxValue(MyNewDataType arr[])
{
	MyNewDataType temp = 0;
	
	for (int i = 0; i < 5; i++)
	{
		if (arr[i] > temp)
		{
			temp = arr[i];
		}
	}

	cout << "The largest value in this array is: " << temp;

	return 0;
}

int main()
{
	//arrays
	int intarray[5];
	float floatarry[5];
	string stringarray[5];

	//read file and insert into arrays
	string line;
	ifstream myfile("data.txt");
	if (myfile.is_open())
	{
		myfile >> intarray[0] >> intarray[1] >> intarray[2] >> intarray[3] >> intarray[4];
		myfile >> floatarry[0] >> floatarry[1] >> floatarry[2] >> floatarry[3] >> floatarry[4];
		myfile >> stringarray[0] >> stringarray[1] >> stringarray[2] >> stringarray[3] >> stringarray[4];
	}

	int z;
	z = maxValue(intarray);
	cout << z << endl;

	float i;
	i = maxValue(floatarry);
	cout << i << endl;

	/*string e;
	e = maxValue(stringarray);
	cout << e << endl;*/

	system("Pause");
	return 0;
}
Your third question:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>
#include <fstream>

int main()
{
    using namespace std;

    ifstream file("data.txt");
    if(file.is_open())
    {
        string myArray[5]; // or whatever size you need

        for(int i = 0; i < 5; i++) // change 5 to the same size as above
        {
            file >> myArray[i]; // strings are stored in the array at position[i]
        }
    }
return(0);
}


Sorry, but I do not know what templates are or why you are using them for this assignment (I know you do not need a template, but I understand the teacher may be requiring it). Good luck.
Last edited on
I changed "Data" to "sand", but it is working properly now...what you wanted to do was not return a value...but make it a void...then, since you are "cout"ing something inside the function, just call the function in main and it will output the result...

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
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

template <class T>
void maxValue(T arr[])
{
	T temp = 0;
	
	for (int i = 0; i < 5; i++)
	{
		if (arr[i] > temp)
		{
			temp = arr[i];
		}
	}

	cout << "The largest value in this array is: " << temp;

}

int main()
{
	//arrays
	int ia[5];
	float fa[5];
	string s[5];

	//read file and insert into arrays
	string line;
	ifstream myfile;
	myfile.open("sand.txt");

		for(int i = 0; i < 5; i++)
			myfile>> ia[i];
		for(int i = 0; i < 5; i++)
			myfile>> fa[i];
		for(int i = 0; i < 5; i++)
			myfile>> s[i];

		myfile.close();
	for(int i = 0; i < 5; i++)
		cout<< s[i] << " ";
	cout<< endl;

	maxValue(ia);
	cout << endl;

	maxValue(fa);
	cout << endl;

	return 0;
}
Last edited on
closed account (SECMoG1T)
Hi
1
2
/// your line 10
MyNewDataType temp = 0;


If MyNewDataType is a string then the function will throw a logic error, you can't intialize a string from an integral literal.

1
2
3
/// might be it can be safer if you intialize your temp to the value of the first index.
MyNewDataType temp=arr [0]; //then iterate from the second index.
Topic archived. No new replies allowed.