Best selling items program

Hi i am writing a program that will read a sales.txt file and output the top 2 items sold and their gross sales. unfortunately i can only get the program to output the 2 top sellers but not sure how to get their gross sales displayed also, my output should look like this :
The top selling product is Elec_Drill with total sales of $6031.08
The second top selling product is Hammer with total sales of $4800.95

this is the file
1002 Hammer 23.65 203
1024 Nails 6.95 400
1276 Screwdriver 13.95 251
1385 Elec_Drill 45.69 132
1462 Air_Filter 7.95 500

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include "stdafx.h"
#include <iostream>
#include <string>
#include<fstream>

using namespace std;

//structure declaration
struct Product {
	int productNumber;
	string productName;
	double unitPrice;
	int unitsSold;
};

int main() {
	struct Product p[100];
	int i = 0, j;
	int totalProducts;

	ifstream infile;
	infile.open("sales.txt");

	//Read data from file
	while (!infile.eof()) {
		infile >> p[i].productNumber;
		infile >> p[i].productName;
		infile >> p[i].unitPrice;
		infile >> p[i].unitsSold;
		i++;
	}
	totalProducts = i;

	//Sort the products by revenue
	for (i = 0; i < totalProducts - 1; i++) {

		for (j = i + 1; j < totalProducts; j++) {

			if (p[i].unitPrice * p[i].unitsSold > p[j].unitPrice
				* p[j].unitsSold) {

				int tempNumber = p[i].productNumber;
				p[i].productNumber = p[j].productNumber;
				p[j].productNumber = tempNumber;

				string tempName = p[i].productName;
				p[i].productName = p[j].productName;
				p[j].productName = tempName;

				double tempPrice = p[i].unitPrice;
				p[i].unitPrice = p[j].unitPrice;
				p[j].unitPrice = tempPrice;

				int tempUnits = p[i].unitsSold;
				p[i].unitsSold = p[j].unitsSold;
				p[j].unitsSold = tempUnits;

			}
		}
	}

	//Display two products which generated the highest total revenue
	cout << "First Product with Highest Revenue is: " "Product Name: " << p[totalProducts - 1].productName << endl;
	
	
    cout << "\nSecond Product with Highest Revenue is: "  "Product Name: " << p[totalProducts - 2].productName << endl;
	
	

	return 0;
}
Last edited on
I don't understand your problem. You've already did this calculation once (see line 39) so now all you should need is to print out that calculation in your print statement.

Also do you realize that one of the benefits of using a structure is that you can "swap" an entire structure at a time, no need to "swap" all the individual elements?

1
2
3
				    Product temp = products[i];
				    products[i] = products[j];
				    products[j] = temp;


Also using eof() to control the input loop is usually wrong. You should be using the actual file read to control the loop, also since you're using dangerous raw arrays you need to insure you never exceed the array limits. Also beware of subtraction in your array index, you need to insure you don't try to access the array out of bounds. This could happen if your file read fails to read any information.


Thank you for your help. I completely forgot that on line 39, I got it to output correctly now. I'll try to address the other issues you mentioned.
Im also having trouble with getting the first and second products highest revenue can you please share how you got it?
Topic archived. No new replies allowed.