Hello again!
I am still working on a program, and I am now working on sorting an array.
Well, the array is a bunch of strings read from a text file. Some of the strings were turned into integers.
I need to take the strings and find a way to sort them in alphabetical order.
There are many lines that are the same (it's like a bunch of people having the same first name), so I also have to check the next line for alphabetical order.
The first line read is the Sales Group, and the next line read is the Product Number (mixed of numbers and letters).
Finally, these sorted strings are to be outputted into new text files (this is the part I can handle).
I have looked online and in my C++ book everywhere for a similar scenario, but none of the bubblesorts and other sorts and shown examples were even close, so I am here to get some direction on what to do, or how I would modify some already-existing code? I am sorry I don't have any code for the sort I have started, but I have some general direction:
int max_size = 42;
for (int Current = 0; Current < END; Current++)
{
for (int Compare = Current + 1; Compare < END; Compare++)
{
if (Name [Compare] < Name[Current])
{
Temp = Name[Compare];
Name[Compare] = Name[Current];
Name[Current] = Temp;
}
}
}
Here is the full code I have been working on. Please ignore all of my comment-outs. I have been trying things out, but it should be straight forward, for the most part:
/*
Write a program which will read in an input file containing
a list of products and details about each product. The program
will sort the products by product group and part number. The
program should summarize sales of each product, both quantity
of units and dollars of sales. The program will output two
files, a summary file containing part information and amount
sold; the second will be the sorted list of part numbers by part
group.
Requirements:
- Write a C++ program which will read in the input file partdetail.txt (File layout below)
- Create a class which is made up of private variables for each field in the input file
- Write the following public member functions to include in the class
- A function that outputs to a file called Part Summary which will output:
- Each Sales Group
- The total count of parts sold from that sales group
- The total dollars sold from each sales group
- the percentage each group is compared to the grand total of parts sold
- Hint: You will need a total dollar accumulator or a member function that
calculates the grand total of all sales in the class
- A function that sorts all of the values in the class array
- Hint: You will need to declare an array to read in each data file into the
class you create. This member function should then sort the values in the array.
- The sort criteria is:
- Primary sort on Sales Group
- Secondary sort on Part
- Write a function to output the sorted partdetail file to a file named sortedparts.txt
- All file output should contain proper formatting (without commas**): IE, if the field represents dollars,
then a dollar sign should be present. If a field represents a %, then the
% symbol should be present.
**Extra points
*/
#include <iostream>
#include <fstream>
#include <cctype>
#include <cstdlib>
#include <string>
using namespace std;
class ProductSummary
{
public:
ProductSummary();
char PS[42];
friend ostream& operator << (ostream& outxml, const ProductSummary& PS);
void ReadProductFile(ProductSummary, ifstream& in_file);
ProductSummary (string getPlanner, string getCommGrp, string getSalesGrp, string getPartNum, double getAvgPrice, int getNumSold, double getPartTotal);
string
getPlanner,
getCommGrp,
getSalesGrp,
getPartNum;
double getAvgPrice;
int getNumSold;
double getPartTotal;
// void calc
private:
string
Planner,
CommGrp,
SalesGrp,
PartNum;
double AvgPrice;
int NumSold;
double PartTotal;
};
ProductSummary::ProductSummary()
{
//This is the format:
Planner = " ";
CommGrp = " ";
SalesGrp = " ";
PartNum = " ";
AvgPrice = 0.00;
NumSold = 0;
PartTotal = 0.00;
}
void ProductSummary::ReadProductFile(ProductSummary, ifstream& in_file)
{
//placeholders for the position in the string (in file partdetail.txt)
int current_pos,
prev_pos;
//Names of all of the strings to be read in
string
file_input,
nextPlanner,
nextCommGrp,
nextSalesGrp,
nextPartNum,
nextAvgPrice,
nextNumSold,
nextPartTotal;
getline(in_file, file_input);
current_pos = file_input.find(",");
prev_pos = current_pos + 1;
nextPlanner = file_input.substr(0, current_pos);
prev_pos = current_pos + 1;
current_pos = file_input.find(",", prev_pos);
nextCommGrp = file_input.substr(prev_pos, current_pos - prev_pos);
prev_pos = current_pos + 1;
current_pos = file_input.find(",", prev_pos);
nextSalesGrp = file_input.substr(prev_pos, current_pos - prev_pos);
prev_pos = current_pos + 1;
current_pos = file_input.find(",", prev_pos);
nextPartNum = file_input.substr(prev_pos, current_pos - prev_pos);
prev_pos = current_pos + 1;
current_pos = file_input.find(",", prev_pos);
nextAvgPrice = file_input.substr(prev_pos, current_pos - prev_pos);
prev_pos = current_pos + 1;
current_pos = file_input.find(",", prev_pos);
nextNumSold = file_input.substr(prev_pos, current_pos - prev_pos);
prev_pos = current_pos + 1;
nextPartTotal = file_input.substr(current_pos + 1, file_input.length() - current_pos);
current_pos = file_input.find(" ", prev_pos);
getPlanner = Planner = nextPlanner;
getCommGrp = CommGrp = nextCommGrp;
getSalesGrp = SalesGrp = nextSalesGrp;
getPartNum = PartNum = nextPartNum;
getAvgPrice = AvgPrice = atof(nextAvgPrice.c_str());
getNumSold = NumSold = atoi(nextNumSold.c_str());
getPartTotal = PartTotal = atof(nextPartTotal.c_str());
}
/*ostream& operator << (ostream& outxml, const ProductSummary& PS)
{
outxml << PS.Planner;
return outxml;
}*/
/*
void Something2, ReadFile??
{
This is where the calculations go.
}
void Something3, ReadFile??
{
This might be where the first and second SORTS go. Maybe separate sections?
}
Don't forget!
Something4 = atoi(Something.c_str());
turns string into integer
*/
int main()
{
ProductSummary PS[42];
//typedef ProductSummary* PSPtr;
//PSPtr pPS;
ofstream txtout;
ifstream in_file;
int i, final_count, size=1;
txtout.open("sortedparts.txt");
if (txtout.fail())
exit(1);
in_file.open("partdetail.txt");
if (in_file.fail())
exit(2);
i = 0;
while (!in_file.eof())
{
PS[i].ReadProductFile(PS[i], in_file);
//PS[i].calc?(PS[i])
i++;
}
/*
pPS = new ProductSummary[42];
in_file.close();
in_file.open("partdetail.txt");
if (in_file.fail())
exit(2);
i = 0;
while (!in_file.eof())
{
pPS[i].ReadProductFile(pPS[i], in_file);
//calc?
i++;
}
*/
final_count = i;
in_file.close();
txtout << "<ProductSummary>";
for (i = 0; i < final_count; i++)
//txtout << PS[i].getPlanner << ' ' << PS[i].getCommGrp << ' ' << PS[i].getPartNum << '\n';
//txtout << PS[i].getCommGrp<< '\n';
//txtout << PS[i].getSalesGrp<< '\n';
//txtout << PS[i].getPartNum<< '\n';
//txtout << PS[i].getAvgPrice<< '\n';
txtout << PS[i].getNumSold<< '\n';
//txtout << PS[i].getPartTotal<< '\n';
//txtout << PS[i].getCommGrp;
//txtout << "Did it work?";
txtout << '\n'
<< "</ProductSummary>";
txtout.close();
return 0;
}
Also, I attached what the input file looks like. The order of the information from left to right per line is Planner, Commodity Group, Sales Group, Part Number, Avg Price, Number Shipped, Sales Made. As you can see, the two I am after are in the third and fourth position.
I really appreciate all of the help I have gotten thus far on my previous inquiry, and thanks in advance for anyone who may have any idea as to what I could do to make any of this sorting work.