Help selecting largest value from a txt file

Hello, i'm looking for a bit of guidance on an issue i have.

I'm building an application that can read weather data from a .txt file. I have built my menus which navigate to different parts of the app that will pull various bits of data dependant on what the user is selecting eg(Yearly reports, monthly reports, average yearly temperature etc) However i'm struggling on a section where i am requiring the program to find the Highest max temperature within the text file. I have my program reading the file and converting all of the values from strings within the text file to float values within the program, however i can't for the life of me work out how to get the program to read and print out the largest number from the text file. Any guidance would be most helpful

The formatting of the file is as follows:
Year, month, max temp, min temp
1988, 12, 30.5, -1

and the code i have to read it is as follows

1
2
3
4
5
6
7
8
9
10
11
    ifstream Weatherdata("Weatherdata.txt");
    while (Weatherdata.good()) {
        getline(Weatherdata, year, ',');
        getline(Weatherdata, month, ',');
        getline(Weatherdata, maxtemp, ',');
        getline(Weatherdata, mintemp, ',');

        Yeardata = stoi(year);
        Monthdata = stoi(month);
        Max_Temp = stof(maxtemp);
        Min_Temp = stof(mintemp);
Last edited on
Have an extra variable for "max value found so far". When you loop across your file, check in each iteration if the current value is greater than the max value found so far. If it is, then update the max value found so far to match the current value.
Hey, i've managed to get it working to a degree thanks, i'm at this stage now, i'm using this to grab the higest values, however it will print out in descending order the top values it finds.

So first value is 23 then the next highest 25.6 and then onto 26.5 (using small set of data) however when it prints it will print out the first value followed by the next 2 values that exceed it as follows

Highest temp: 23
Highest temp: 25.6
Highest temp: 26.5


Also here's the code i've used
1
2
3
4
        if (Maxtemp > Hold_Maxtemp) {
            Hold_Maxtemp = Maxtemp;
            cout << "Highest temp:" << Hold_Maxtemp << endl;
        }


obviously rudimentary for now, which i will clean up after the fact, is there a way of it disregarding the previous value when it finds a higher one?
Last edited on
This is heavy-going obtaining the data. There's an easier way. Consider:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <fstream>
#include <iostream>

int main()
{
	std::ifstream Weatherdata("Weatherdata.txt");

	if (!Weatherdata)
		return (std::cout << "Cannot open file\n"), 1;

	int Yeardata {}, Monthdata {};
	double Max_Temp {}, Min_Temp {}, HighestMax {};
	char del {};

	while (Weatherdata >> Yeardata >> del >> Monthdata >> del >> Max_Temp >> del >> Min_Temp)
		if (Max_Temp > HighestMax)
			HighestMax = Max_Temp;

	std::cout << "The highest max temp is " << HighestMax << '\n';
}

Topic archived. No new replies allowed.