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.

Edited 4 Years Ago by dinosaur123: Incorrect info.

Attachments
MPS,PLAN3,WIRE,HDS100,239.38,1232,294916.16
MPS,PLAN3,PSS,PRG-UNV,89,2401,213689
MPS,PLAN3,PSS,CMJ455,46.64,1567,73084.88
*,FGOOD,PSS,ELPMBPJF,45,1300,58500
MPS,PLAN3,PSS,PRS-UNV,89,604,53756
*,FGOOD,PSS,CMJ500R1,51,1052,53652
MPS,PLAN3,PSS,PJF2-UNV,69,667,46023
MPS,PLAN3,PSS,PRG-UNV-W,89,398,35422
MPS,PLAN2,CSOS,IWB600-2SB,115,302,34730
MPS,PLAN2,CSOS,IWB600-UNIV,109,280,30520
MPS,PLAN3,PSS,PSTK-2955,175,145,25375
*,FGOOD,APNS,ELPMBP02,42.5,570,24225
*,FGOOD,CSOS,PS200,33,636,20988
MPS,PLAN3,PSS,PRS-KIT0811,109.44,179,19589.76
MPS,PLAN3,APNS,CMJ453,72.88,226,16470.88
MPS,PLAN3,APNS,WSP700,30.25,522,15790.5
*,FGOOD,PSS,MP300CM,73.51,200,14702
MPS,PLAN2,CSOS,IWB600-WB,148.25,84,12453
MPS,PLAN3,APNS,AEC0305,71.52,165,11800.8
MPS,PLAN3,PSS,PRS-UNV-W,89,128,11392
MPS,PLAN3,PSS,PSM-UNV,114.98,97,11153.06
MPS,PLAN3,APNS,AEC012018,47.52,233,11072.16
MPS,PLAN3,PSS,PRG-UNV-S,89,120,10680
MPS,PLAN3,APNS,AEC0406,76.32,131,9997.92
MPS,PLAN3,APNS,AEC0203,57.12,164,9367.68
MPS,PLAN3,APNS,CMJ450,63.42,133,8434.86
*,FGOOD,PSS,ELPMBPRG,69,120,8280
MPS,PLAN3,PSS,PRS-KIT1420,131.04,57,7469.28
MPS,PLAN3,APNS,EXT101,17.54,396,6945.84
INT_MPS,FGOOD,WIRE,HDS-R,155.37,43,6680.91
*,FGOOD,WIRE,HDS100-2,399.05,16,6384.8
MPS,PLAN2,FPSS,SR560M,373.92,664,248282.88
MPS,PLAN2,FPSS,SA740P,88,1883,165704
MPS,PLAN2,FPSS,SA752PU,175,903,158025
MPS,PLAN2,FPSS,PA740,46,3262,150052
MPS,PLAN1,FPSS,DS-VW765-LAND,290,506,146740
MPS,PLAN1,FPSS,ST650,59,1988,117292
MPS,PLAN2,FPSS,SA761PU,220,409,89980
*,FGOOD,FPSS,PN-SR760M,442.25,200,88450
MPS,PLAN1,FPSS,PF660,43,2054,88322
MPS,PLAN2,FPSS,FPS-1000,145.92,569,83028.48
MPS,PLAN1,FPSS,PLCM-UNL-CP,182.12,417,75944.04

Sorting with respect to two things (SalesGrp and PartNum) in a way that if the first elements are the same, then sort with respect to the second element, that is called lexicographical sorting (and alphabetical sorting is a kind of lexicographical sorting, where each element is a character).

In short, all you need to do to is define a comparison operator (or function) to compare the two ProductSummary objects. Here is that function:

// a custom less-than operator for class ProductSummary:
bool operator <(const ProductSummary& lhs, const ProductSummary& rhs) {
  if(lhs.getSalesGrp() < rhs.getSalesGrp())
    return true;
  else if(lhs.getSalesGrp() > rhs.getSalesGrp())
    return false;
  else // this means both SalesGrp are the same:
    return (lhs.getPartNum() < rhs.getPartNum());
};

You have a lot of work to do to make your ProductSummary class work correctly. But when you get there, you can use the above less-than operator and the bubble-sort algorithm that you posted already, and that should be it.

Ah!! That helps SO much!! I can't thank you enough!!
You are the best! I am going to work on this now, and I will share my end result once I get it to work.
Thank you!

This article has been dead for over six months. Start a new discussion instead.