How to compare a single value from each line of an input file?

I'm trying to write a code that will compare a value from each line of an input, and rank the top two based on that value (in this case their weekly sales). But I can't think of a way to do this as the number of input lines won't be consistent (So if/elseif for each individual line would not work)

Here's the input (the employee's name, their hours worked, their weekly sales):
John,40,813
Phil,40,722
Jessica,40,959
Yugo,20,399

So what I want the output to say is:
First: Jessica, 959
Second: John, 813


This is my code so far, it's just the basic skeleton that will read in all the data. If you guys have any hints at all I would love to hear them!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void weeklysales(string inputfile, string outputfile)
{
	ifstream input(inputfile);
	ofstream output(outputfile);

	string name; 
	int hours, sales;

	getline(input, name, ',');

	while (!input.fail())
	{
		input >> hours;
		input.ignore();
		input >> sales;

		getline(input, name, ',');
	}

	input.close();
	output.close();
}
Last edited on
closed account (48T7M4Gy)
1. Open the file
2. Read in the values (name, sales) line by line and store them in an array(s) or vector - a while loop
3. Close the file
4. Sort the data - you can design your own sort routine or use the library sort functions (STL algorithm)
5. Read off the relevant two

Use the tutorials on this website for guidance on the various components in the pseudocode. :)
Thank you for the response kemort, but I should have mentioned I cannot use any arrays, vectors, or the like. The sorting has to be done line by line, which is where I'm having so much trouble coming up with a code
You are mistaken. You cannot sort "line by line." (It is impossible.) Please revisit your requirements.
Topic archived. No new replies allowed.