Write a C++ program that reads sales data from a file called sales.txt. Each line of the file contains four data items: 1. A product number which is an integer 2. A product name which is a string no longer than 12 characters and which does not contain spaces 3. A unit price which is a double 4. A number of units sold which is an integer The program will output the two products which generated the highest total revenue (unit price * number sold). Your program must: 1. Read each line of the file storing the four items into the members of a dynamically allocated structure. 2. Store pointers to the structures in an array. 3. Sort the array of pointers by total revenue 4. Display the product name and total revenue of the top two products 5. The program must work for sales.txt files with anywhere from 2 to 100 products ---------- sales.txt file information: 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 ------- Help me fix my code so that it reflects the information needed: #include #include #include #include using namespace std; struct Sale { int num = 0; // Product number string name; // No longer than 12 characters with no spaces int soldUnits = 0; // Number of units sold double price = 0.0; // Price of Units }; int main() { ifstream inputFile; string saleName; int productNum, soldNumUnits; double unitPrice; Sale* salesArray[100]; Sale* aSale; int numberOfProducts = 0; inputFile.open("sales.txt"); // Continue reading until the end of file; while (inputFile >> saleName) { inputFile >> productNum >> unitPrice >> soldNumUnits; //Dynamically creates a new sale aSale = new Sale; aSale->num = productNum; aSale->name = saleName; aSale->price = unitPrice; aSale->soldUnits = soldNumUnits; //Store the pointer to the Sale struct in an array salesArray[numberOfProducts++] = aSale; } //Starts finding the total revenue from salesArray; double highest = (double)salesArray[0]->price * salesArray[0]->soldUnits; double secondHighest = (double)salesArray[0]->price * salesArray[0]->soldUnits; int pos1 = 0; int index = 1; int pos2 = 0; //Finds out the highest total revenue; while (index < numberOfProducts) { double tempHighest = (double)salesArray[index]->price * salesArray[index]->soldUnits; if (index < tempHighest) { pos1 = index; secondHighest = tempHighest; } index++; } //Displays the top selling product; cout << "The top selling product is " << salesArray[pos1]->name << " with total sales of " << highest << ". " << endl; //If there are less than 2 products, exit. if (numberOfProducts < 2) { cout << "ERROR: There must be more than 2 products." << endl; exit(0); }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Topic Video
Question

Write a C++ program that reads sales data from a file called sales.txt. Each line of the file contains four data items:
1. A product number which is an integer
2. A product name which is a string no longer than 12 characters and which does not contain spaces
3. A unit price which is a double
4. A number of units sold which is an integer
The program will output the two products which generated the highest total revenue (unit price * number sold).
Your program must:
1. Read each line of the file storing the four items into the members of a
dynamically allocated structure.
2. Store pointers to the structures in an array.
3. Sort the array of pointers by total revenue
4. Display the product name and total revenue of the top two products
5. The program must work for sales.txt files with anywhere from 2 to 100 products

----------

sales.txt file information:

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

-------

Help me fix my code so that it reflects the information needed:

#include <iostream>
#include <string>
#include <fstream>
#include <stdlib.h>

using namespace std;

struct Sale
{
int num = 0; // Product number
string name; // No longer than 12 characters with no spaces
int soldUnits = 0; // Number of units sold
double price = 0.0; // Price of Units
};

int main()
{
ifstream inputFile;
string saleName;
int productNum, soldNumUnits;
double unitPrice;
Sale* salesArray[100];
Sale* aSale;
int numberOfProducts = 0;
inputFile.open("sales.txt");

// Continue reading until the end of file;
while (inputFile >> saleName)
{
inputFile >> productNum >> unitPrice >> soldNumUnits;

//Dynamically creates a new sale
aSale = new Sale;
aSale->num = productNum;
aSale->name = saleName;
aSale->price = unitPrice;
aSale->soldUnits = soldNumUnits;

//Store the pointer to the Sale struct in an array
salesArray[numberOfProducts++] = aSale;

}

//Starts finding the total revenue from salesArray;
double highest = (double)salesArray[0]->price * salesArray[0]->soldUnits;
double secondHighest = (double)salesArray[0]->price * salesArray[0]->soldUnits;
int pos1 = 0;
int index = 1;
int pos2 = 0;

//Finds out the highest total revenue;
while (index < numberOfProducts)
{
double tempHighest = (double)salesArray[index]->price * salesArray[index]->soldUnits;
if (index < tempHighest)
{
pos1 = index;
secondHighest = tempHighest;
}
index++;
}

//Displays the top selling product;
cout << "The top selling product is " << salesArray[pos1]->name
<< " with total sales of " << highest << ". " << endl;

//If there are less than 2 products, exit.
if (numberOfProducts < 2)
{
cout << "ERROR: There must be more than 2 products." << endl;
exit(0);
}

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Instruction Format
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education