Hello,
I am having trouble with a program I am writing.
The problem is that I need to take information from a text file, read it, and then output the information into another text file.
I keep getting this output:
<ProductSummary>0015D7A00015D7A00015D7A00015D7A00015D7A00015D7A00015D7A00015D7A00015D7A00015D7A00015D7A00015D7A00015D7A00015D7A00015D7A00015D7A00015D7A00015D7A00015D7A00015D7A00015D7A00015D7A00015D7A00015D7A00015D7A00015D7A00015D7A00015D7A00015D7A00015D7A00015D7A00015D7A00015D7A00015D7A00015D7A00015D7A00015D7A00015D7A00015D7A00015D7A00015D7A00015D7A0
</ProductSummary>
I need it to output something like this:
Planner = " ";
CommGrp = " ";
SalesGrp = " ";
PartNum = " ";
AvgPrice = 0.00;
NumSold = 0;
PartTotal = 0.00;
I also need to sort the information, but I think once I get the information written to a text file correctly, I can figure out the rest.
Here is my code:
/*
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);
// 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);
Planner = nextPlanner;
CommGrp = nextCommGrp;
SalesGrp = nextSalesGrp;
PartNum = nextPartNum;
AvgPrice = atof(nextAvgPrice.c_str());
NumSold = atoi(nextNumSold.c_str());
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;
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;
//txtout << "Did it work?";
txtout << '\n'
<< "</ProductSummary>";
txtout.close();
return 0;
}
It should compile cleanly, but I am confused as to where to go from here.
If someone were to assist me, I would really appreciate it! Even advice would help!
Thank you so much!