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); }
Write a C++
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);
}

Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 1 images









