I\'m having trouble with sorting. I have to find the product most sold and least
ID: 670434 • Letter: I
Question
I'm having trouble with sorting. I have to find the product most sold and least sold using the sort function. for some reason it is not working. I need help. I have provided my code and the txt file. the code runs, but the sorting part doesn't do it right.
I also have to find the item that generated the most income. Please help me thanks
#include<iostream>
#include<fstream>
#include<cmath>
#include<iomanip>
#include<string>
using namespace std;
const int SIZE = 100;
void readFile(string & date, int amount_sold[], float price[], string item_name[], int & numLines, char & c);
void totalSales(int amount_sold[], float price[], string item_name[], int & numLines, float & sum);
void sort(int listLength, int amount_sold[], float price[], string item_name[], bool(*compare)(int, int));
void displayToFile(string date,int listLength, int amount_sold[], float price[], string item_name[], int numLines, float sum);
int main()
{
int listLength =0;
int amount_sold[SIZE];
float price[SIZE];
string item_name[SIZE];
string date;
char c;
float sum = 0;
int numLines = 0;
int *compare;
readFile(date, amount_sold, price, item_name, numLines, c);
totalSales(amount_sold, price, item_name, numLines, sum);
displayToFile(date, listLength, amount_sold, price, item_name, numLines, sum);
return 0;
}
// function to read file
void readFile(string & date, int amount_sold[], float price[], string item_name[], int & numLines, char & c)
{
ifstream infile;
infile.open("sales.txt");
cout << "Sales on " << endl;
//if statement if file can't be found
if (!infile)
{
cout << "Error, file couldn't be found";
cout << endl;
exit(1);
}
getline(infile, date);
cout << date << endl;
// while loop to display the list of sales
while (!infile.eof() && numLines < SIZE)
{
infile >> amount_sold[numLines] >> c >> price[numLines];
getline(infile, item_name[numLines]);
cout << endl;
cout << amount_sold[numLines] << " " << c << price[numLines] << " " << item_name[numLines];
++numLines;
}
cout << endl << endl;
}
// function to calculate the sales of the day
void totalSales(int amount_sold[], float price[], string item_name[], int & numLines, float & sum)
{
//total sales of the day
for (int i = 0; i < numLines; ++i)
{
sum += (amount_sold[i]) * (price[i]);
}
cout << "Total sales: " << sum;
cout << endl << endl;
}
// function to sort
void sort(int listLength, int amount_sold[], float price[], string item_name[], bool(*compare)(int, int))
{
int temp;
string temp2;
//sorting to find product most sold
for (int i = 0; i < listLength-1; i++)
{
for (int j = i+1; j < listLength; j++)
{
if ((*compare) (amount_sold[i], amount_sold[j]));
{
//swap amount
temp = amount_sold[i];
amount_sold[i] = amount_sold[j];
amount_sold[j] = temp;
//swap price
temp = price[i];
price[i] = price[j];
price[j] = temp;
//swap item name
temp2 = item_name[i];
item_name[i] = item_name[j];
item_name[j] = temp2;
}
}
}
}
// function to display information to report.txt file
void displayToFile(string date, int listLength, int amount_sold[], float price[], string item_name[], int numLines, float sum)
{
ofstream outfile;
outfile.open("report.txt");
outfile << "Sales on: " << date << endl << endl;
//output total sales
outfile << "Total sales: " << sum;
cout << endl;
outfile << endl << endl;
//output product most sold and least sold
outfile << "Most sold: " << *item_name;
outfile << endl;
cout << "Most sold: " << *item_name;
cout << endl;
outfile << "Least sold: " << *item_name;
outfile << endl << endl;
cout << "Least sold: " << *item_name;
cout << endl << endl;
outfile.close();
}
//==========================================================================
txt file
February 12, 2015
57 $1.80 Premium Milk
30 $2.00 Berrytale
25 $3.50 Boroh Danish
15 $1.89 Marble Taro
65 $2.54 Chocolate Chip Bowl
44 $1.45 Mango Bread
53 $3.15 Coffee Raisin Bread
32 $2.00 Milk Pudding
20 $1.50 Red Bean Bread
34 $2.15 Cheese Bread
49 $3.75 Brioche
Explanation / Answer
#include<iostream>
#include<fstream>
#include<cmath>
#include<iomanip>
#include<string>
using namespace std;
const int SIZE = 100;
void readFile(string & date, int amount_sold[], float price[], string item_name[], int & numLines, char & c);
void totalSales(int amount_sold[], float price[], string item_name[], int & numLines, float & sum);
void sort(int listLength, int amount_sold[], float price[], string item_name[]);
void displayToFile(string date,int listLength, int amount_sold[], float price[], string item_name[], int numLines, float sum);
int main()
{
int listLength =0;
int amount_sold[SIZE];
float price[SIZE];
string item_name[SIZE];
string date;
char c;
float sum = 0;
int numLines = 0;
int *compare;
listLength=numLines;
readFile(date, amount_sold, price, item_name, numLines, c);
totalSales(amount_sold, price, item_name, numLines, sum);
sort(numLines,amount_sold,price,item_name);
cout<<"number of entries: "<<numLines<<endl;
for (int i = 0; i < numLines; i++){
cout << amount_sold[i] << " " << c << price[i] << " " << item_name[i]<<endl;
}
displayToFile(date, listLength, amount_sold, price, item_name, numLines, sum);
return 0;
}
// function to read file
void readFile(string & date, int amount_sold[], float price[], string item_name[], int & numLines, char & c)
{
ifstream infile;
infile.open("sales.txt");
cout << "Sales on " << endl;
//if statement if file can't be found
if (!infile)
{
cout << "Error, file couldn't be found";
cout << endl;
}
getline(infile, date);
cout << date << endl;
// while loop to display the list of sales
while (!infile.eof() && numLines < SIZE)
{
infile >> amount_sold[numLines] >> c >> price[numLines];
getline(infile, item_name[numLines]);
cout << endl;
cout << amount_sold[numLines] << " " << c << price[numLines] << " " << item_name[numLines];
++numLines;
}
cout << endl << endl;
}
// function to calculate the sales of the day
void totalSales(int amount_sold[], float price[], string item_name[], int & numLines, float & sum)
{
//total sales of the day
for (int i = 0; i < numLines; ++i)
{
sum += (amount_sold[i]) * (price[i]);
}
cout << "Total sales: " << sum;
cout << endl << endl;
}
// function to sort
void sort(int numLines, int amount_sold[], float price[], string item_name[])
{
int temp;
float temp3;
string temp2;
//sorting to find product most sold
for (int i = 0; i < numLines; ++i){
for (int j = 0; j < numLines - i - 1; ++j)
if (amount_sold[j] < amount_sold[j + 1])
{
//swap amount
temp = amount_sold[j];
amount_sold[j] = amount_sold[j+1];
amount_sold[j+1] = temp;
//swap price
temp3 = price[j];
price[j] = price[j+1];
price[j+1] = temp3;
//swap item name
temp2 = item_name[j];
item_name[j] = item_name[j+1];
item_name[j+1] = temp2;
}
}
}
// function to display information to report.txt file
void displayToFile(string date, int listLength, int amount_sold[], float price[], string item_name[], int numLines, float sum)
{
ofstream outfile;
outfile.open("report.txt");
outfile << "Sales on: " << date << endl << endl;
//output total sales
outfile << "Total sales: " << sum;
cout << endl;
outfile << endl << endl;
//output product most sold and least sold
outfile << "Most sold: " << *item_name;
outfile << endl;
cout << "Most sold: " << *item_name;
cout << endl;
outfile << "Least sold: " << *item_name;
outfile << endl << endl;
cout << "Least sold: " << item_name[numLines-1];
cout << endl << endl;
outfile.close();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.