How to find the year for highest value ?

I have a file that consist of 30 rows and 3 column. I have to find the highest value of the second column with the year. for example the file is like :

2012 24 800
2013 20 304
2014 30 589
2015 28 400

i need to get the output 30 and 2014 , i already get the coding for finding the highest but i can't find the year. this is my coding :

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;

int highest (ifstream& fromFile);
int smallest (ifstream& fromFile);
int average (ifstream& fromFile);

int main()
{
    string data;
    ifstream fromFile("data.txt");

	if (!fromFile) 
	return (cout << " ERROR : cannot open file.\n"), 1;

	while (getline(fromFile, data))
	cout << data << endl;

	fromFile.clear();
	fromFile.seekg(0);

	int year {}, price {}, production {};
        fromFile >> year >> price >> production;

        highest(fromFile);
    
    return 0;
}

int highest (ifstream& fromFile)
{
        fromFile.clear();
        fromFile.seekg(0);

        int highest_price ;
        
        for (int year {}, price {}, production {};
           fromFile >> year >> price >> production;)
    {
           highest_price = price;
         
	   if (price > highest_price)
	   highest_price = price;

    }

        std::cout << "The highest price is " << highest_price << '\n';
     

    return 0;

}
Last edited on
So create another variable called highest_year, and use that to keep track of the year in the same way you track the price.
Why are you first displaying the contents of the file? What if the file had several million lines?

L27. Why are you reading but ignoring the data on the first line? What if the highest value occurred on this first line?

In the for loop L41 onwards, every time round the loop you set highest_price to price???

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
#include <iostream>
#include <fstream>

void highest(std::ifstream& fromFile);

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

	if (!fromFile)
		return (std::cout << " ERROR : cannot open file.\n"), 1;

	highest(fromFile);
}

void highest(std::ifstream& fromFile)
{
	fromFile.clear();
	fromFile.seekg(0);

	int highest_price {}, high_year {};

	for (int year {}, price {}, production {}; fromFile >> year >> price >> production; )
		if (price > highest_price) {
			highest_price = price;
			high_year = year;
		}

	std::cout << "The highest price is " << highest_price << " for year " << high_year << '\n';
}



The highest price is 30 for year 2014

ouh i misunderstood it , i though L27 is to declare the the variable in file first . i have one question . The year { } , price { } and production { } , if i'm going to explain it to other people , should i say that i read the data from file by vector ?
one more thing for L20 and 21 , i use string to print out the whole data from the file . It was based on my notes. But can i know why we use string ? and is there other way beside using string ?
std::string is the preferred c++ way of handling variable length text/characters. Another way would be to be a fixed size array of char - but that has issues re size and can be more difficult to manipulate as std::string has lots of provided member functions.

The program doesn't use a vector - and doesn't need to. It reads the file line by line and processes each line as required and the reads the next line.

int year{} is just defining year as type int with default initialisation (which for an int is 0).
> int year{} is just defining year as type int with default initialisation (which for an int is 0).

This is value initialisation; default initialisation of an object of type int does nothing.

Default initialisation: https://en.cppreference.com/w/cpp/language/default_initialization
Value initialization: https://en.cppreference.com/w/cpp/language/value_initialization
Fair enough. I've always heard initialising with {} called default initialising as opposed to initialising with an explicit value. I guess some of us are a bit lax in using the 'correct' C++ terminology. Mea Culpa....... :)
does it mean that it initialize that int year start with 0 ?

There is a different when i use int year; and when i use int year { }

and i only get the right value for whatever i do when i use int year {}. i thought maybe that is bcuase the year is a data from file.
when i want to find highest or lowest i use the { } but when i want to find average or sum i just use int year;

and it work why is it act ?
int year;

defines a variable called year of type int but doee NOT provide an initial value. It's initial value is unknown (whatever happens to be in the memory locations used for this variable at the time).

int month {6};

defines a variable called month of type int and provides an initial value of 6.

int day {};

defines a variable called day of type int and initialises it with the default value for the specified type (0 for int).
1
2
3
4
5
6
for ( int year;
      fromFile >> year;
      )
{
  // use year
}

Note that in this the very first thing that is done to the 'year' is to set its value with the fromFile >> year

Therefore, when (and if) we get to the body of the function, the 'year' has a known* value. *Well, whatever integer is in 'fromFile'.

Initialization of 'year' is not crucial, because its value will be set first.


The 'highest_price' is totally different case. The first action with 'highest_price' is to read its current value:
1
2
3
int highest_price {};
// ...
if (price > highest_price)

The initial value has to be set before the loop. Initialization is the logical place.

Furthermore, the initial value of 'highest_price' must be less or equal to lowest possible price. If it is not, then you could have case where all the prices are lower than the initial 'highest_price' and the algorithm would fail.

when i want to find highest or lowest i use the { } but when i want to find average or sum i just use int year;

and it work why is it act ?

Show your code for average or sum.
Topic archived. No new replies allowed.