finding max cost and printing string

Jul 29, 2018 at 11:49pm
So I am having trouble finding the max price from a list of parts that is in a .txt file. I need to find what the highest cost is and cout the part number (string). The rest of my program works fine, just this part has got me stumped. The .txt file appears like:
partnumber (string), category (char), quantity(int), cost(double) which are all stored in a struct.

I know the code below is wrong/missing the part where I would display the string but like I mentioned... I'm a bit lost. Any direction would be appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
string highest(const vector <Parts> &pVector)
{
	
	double max = 0;
	int size = pVector.size();
	for (int i = 0; i < size; i++)
	{
		if (pVector[i].cost > max)		
			max = pVector[i].cost;	
	}
	cout << max << endl;

	return 0;
}
Jul 29, 2018 at 11:56pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
string highest(const vector<Parts>& parts)
{
	if (parts.size() == 0)
	    return "";

	size_t max = 0;
	for (size_t i = 1; i < parts.size(); i++)
	{
		if (parts[i].cost > parts[max].cost)
			max = i;
	}

	return parts[max].partnumber;
}

Last edited on Jul 29, 2018 at 11:58pm
Jul 30, 2018 at 1:13am
Thank you for your response, I knew I wasn't far off. I don't think I covered size_t yet, but after your input I did some reading on it(http://www.cplusplus.com/reference/cstring/size_t/). All worked. Thanks again.
Topic archived. No new replies allowed.