The program isn't reading correctly from the sales file into the vector. This is the current code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
void SalesData::loadDataFromFile(string sales)
{
unsigned int size = sales.size();
string line;
ifstream infile;
infile.open("sales.txt", ios::in);
if (infile.fail())
{
cerr << "Error opening file" << endl;
exit(1);
}
while (getline(infile, line))
{
infile >> sales;
salesrecord.push_back[sales];
for (unsigned int i = 0; i > size; i++)
{
cout << sales[i];
}
}
infile.close();
}
|
Since, as you said, "
(I also can't change any of the private or public classes, due to the assignments requirements.)" you should be making use of the
ifstream
variable declared as a private member of the class. And, don't specify the filename "sales.txt" here, use the value of the parameter
string sales
.
Then read all the integer sales figures from the file, and push them into the vector.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
void SalesData::loadDataFromFile(string sales)
{
infile.open(sales);
if (infile.fail())
{
cerr << "Error opening file" << endl;
exit(1);
}
int value;
while (infile >> value)
{
salesrecord.push_back(value);
}
infile.close();
}
|
The second function has a number of issues too:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
void SalesData::saveBarChartToFile(string graph)
{
unsigned int size = salesrecord.size();
int value;
ofstream outfile;
outfile.open("graph.txt");
while (outfile.is_open())
{
for (unsigned int i = 0; i < size; i++)
{
outfile << size << endl;
graph = value / 100;
cout << endl;
cout << "SALES BAR CHART" << endl;
cout << "Each '*' Represents $100" << endl;
cout << "Store 1: " << size << endl;
cout << "Store 2: " << size << endl;
cout << "Store 3: " << size << endl;
cout << "Store 4: " << size << endl;
cout << "Store 5: " << size << endl;
for (int i = 0; i > value; i++)
{
cout << "*";
}
}
}
outfile.close();
}
|
The function doesn't use the existing
ofstream
variable, nor does it use the parameter
string graph
There's an infinite loop problem,
while (outfile.is_open())
will either not execute at all (if the file is not open) or will loop forever.
At lines 17 to 21, there are details for stores 1 to 5. But how does the program know there are five stores? shouldn't it use the values from the vector instead of making that assumption.
Instead of
cout <<
the function should use
outfile <<
I would start with something like this as an ouline. I omitted some of the details inside the loop:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
void SalesData::saveBarChartToFile(string graph)
{
outfile.open(graph);
if (!outfile.is_open())
{
cerr << "Error opening output file\n";
return;
}
outfile << "SALES BAR CHART\n";
outfile << "Each '*' Represents $100\n";
for (unsigned int i = 0; i < salesrecord.size(); i++)
{
outfile << i+1 << '\n';
}
outfile.close();
}
|
Content of graph.txt:
SALES BAR CHART
Each '*' Represents $100
1
2
3
4
5 |