inputting data into vectors and outputting them into a text file

Hi everyone,
Any assistance on my nearly finished program for one of my assignments would be great.
Your program should read sales values from a file called ‘sales.txt’ and outpuFor example, ‘sales.txt’ file contained these values:
1000
500
1200
600
200

Here is what the ‘graph.txt’ file should contain after the program is executed:
SALES BAR CHART
(each * equals £100)
Store 1: **********
Store 2: *****
Store 3: ************
Store 4: ******
Store 5: **
ts a bar graph representing these values to a file called ‘graph.txt’. Create each bar in the bar graph by displaying a row of asterisks. Each asterisk should represent £100 of sales.

could anyone please help me as to why the vectors wont print the values to the new text file called "graph.txt" thankyou.(I also can't change any of the private or public classes, due to the assignments requirements.)
P.S. I'm a beginner at programming, please take that into consideration :)
void 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();
}

void saveBarChartToFile(string graph)
{
unsigned int size = salesrecord.size();
int value;
ofstream outfile;
outfile.open("C:\\Users\\Owner\\Documents\\Visual Studio 2015\\Projects\\SalestoGraph\\SalestoGraph\\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();
}

Last edited on
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

Last edited on
Okay, I will get on that as soon as I can, in regards to displaying one asterisk for every £100 of sales, will that work as I need it too?

Thank you for your help
Last edited on
Your code for displaying asterisks, I could see that you were nearly there, though this loop needs some adjustment:
1
2
3
4
for (int i = 0; i > value; i++)
{
cout << "*";
}

The > should be < and value needs to be whatever is in the vector for that store.

Or as an alternative, you could just construct a string of the required length as needed, see examples s6a and s6b
http://www.cplusplus.com/reference/string/string/string/
i followed your advice and managed to get the program to output to the text file, however only the following is outputting to the file:
SALES BAR CHART
Each '*' Represents $100
4
5
6

I am having difficulties trying to output stores 1 to 5.
as for displaying the row of asterisks for each store, do i enter the actual figures into multiple vectors or just use the value definition.
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+2 << '\n';
}
outfile.close();
}
any help would be apperciated.
Try printing out the value of salesrecord.size()

That should tell you how many values were read from the input file and stored in the vector. If it is not the same as the number of values in the file, then something went wrong during reading from the file. If it does match, then something is going wrong during the output file processing.

Also, to get the details, use salesrecord[i] where i is in the range 0 to salesrecord.size()-1
Topic archived. No new replies allowed.