Question: I've been working on a program about sales to output the top 2 selling products, but I'm at an impasse. The program is supposed to

I've been working on a program about sales to output the top 2 selling products, but I'm at an impasse. The program is supposed to have dynamically allocated structures and pointers stored to the structures in arrays. This is what I have so far, but it doesn't have what I have mentioned. How can I fix this?

#include #include #include using namespace std;

struct Product { int product_num; string productName; double unitPrice; int unitsSold; };

int main() { struct Product productStructure[100]; int i = 0, j; int productTotal; ifstream infile; infile.open("sales.txt");

while (!infile.eof()) { infile >> productStructure[i].product_num; infile >> productStructure[i].productName; infile >> productStructure[i].unitPrice; infile >> productStructure[i].unitsSold; i++; } productTotal = i;

for (i = 0; i < productTotal - 1; i++) {

for (j = i + 1; j < productTotal; j++) { if (productStructure[i].unitPrice * productStructure[i].unitsSold > productStructure[j].unitPrice * productStructure[j].unitsSold) {

int tempNumber = productStructure[i].product_num; productStructure[i].product_num = productStructure[j].product_num; productStructure[j].product_num = tempNumber;

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

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

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

} } }

cout << "The top selling product is " << productStructure[productTotal - 1].productName << " with total sales of $" << productStructure[productTotal - 1].unitPrice * productStructure[productTotal - 1].unitsSold << endl; cout << "The second top selling product is " << productStructure[productTotal - 2].productName << " with total sales of $" << productStructure[productTotal - 2].unitPrice * productStructure[productTotal - 2].unitsSold << endl;

return 0; }

The sales.txt is as follows

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

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!