C++ vector coding

I have been asked to create a program that needs to read sales values from a sales text file and therefore will output a bar chart representing these values to a graph text file. the bars need to be created in the bar graph by displaying a row of asterisks. Each asterisk needs to represent £100 of sales

This is the coding I have so far, however i am confused as to how to include a vector within the program, and when i run the program no data is being displayed from the text file.

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#pragma once // stops duplicate library includes 
#include <iostream>
#include <string>
#include <fstream>
#include <vector>

using namespace std;

class SalesData
{
private:
	ifstream inputfile;
	ofstream outputfile;
	vector<int> salesData;
public:
	void loadDataFromFile(string filename);
	void saveBarChartToFile(string filename);
};



void SalesData::loadDataFromFile(string filename)
{
	//loop to read in 
	ifstream sales;
	int SalesData;

	inputfile.open("Sales.txt", ios::in);

	if (!inputfile) return; //check to see if file exists 

	while (inputfile >> SalesData) cout << SalesData << endl;	
	{
		//vector<int> myintvector;
		//for (int i = 0; i < 10; i++)
		//{
			//myintvector.push_back(i);
			//cout << SalesData << endl; //when EOF is reached, (input file >> salesdata) returns false 
		//}
	}
	inputfile.close();

}



void SalesData::saveBarChartToFile(string filename)
{
	//loop to read out 
	ofstream graph; //ofstream is an Output File Stream
	
	graph.open("Graph.txt", ios::out);
	graph << "Sales bar chart" << endl;
	graph << " (each * equals £100)" << endl;
	graph.close(); //close stream. You always have to close what you open 
	
}


int main()
{
	SalesData MyData;
	MyData.loadDataFromFile("Sales.txt");
	MyData.saveBarChartToFile("Graphs.txt");

	system("Pause");
	return 0;
}
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <cmath> // std::round

class SalesData
{
    private: std::vector<double> sales ;

    public:
        void loadDataFromFile( std::string filename );
        void saveBarChartToFile( std::string filename ) const;
};

void SalesData::loadDataFromFile( std::string filename )
{
    std::ifstream inputfile(filename); // open the file for input

    // add each sales amount read from the file to the vector
    double sales_amount ;
    while ( inputfile >> sales_amount ) sales.push_back(sales_amount) ;
}

void SalesData::saveBarChartToFile( std::string filename ) const
{
    std::ofstream graph(filename); // // open the file for output
    graph << "Sales bar chart\n (each * equals £100)\n" ;

    for( double sales_amount : sales ) // for each value in the vector
    {
        // number of asterisks == sales_amount/100 rounded to the nearest integer
        const std::size_t num_asterisks = std::round( sales_amount / 100 ) ;

        // write a string containing num_asterisks asterisks and a newline after that
        graph << std::string( num_asterisks, '*' ) << '\n' ;
    }
}

int main()
{
    SalesData MyData;
    MyData.loadDataFromFile("Sales.txt");
    MyData.saveBarChartToFile("Graph.txt");
}
Thank you but I have tried this and still when the program is being run it is just saying "Press any key to continue" and no numbers are being displayed.
> "will output a bar chart representing these values to a graph text file"

MyData.saveBarChartToFile("Graph.txt"); writes the bar chart to the file "Graph.txt".

Open the file in a text editor to see its contents;
or add this line as the last statement in main() to dump the contents of "Graph.txt"to stdout.
std::cout << std::ifstream("Graph.txt").rdbuf() ;
OP: if problem persists can you show us how Sales.txt looks like?
1000
500
1200
600
200
this is what is contained in the Sales.txt file. I have used the coding that JLBorges has given me but it is saying that num_Asterisks is undefined even though the coding is practically the same?
This is my coding so so far.
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#pragma once // stops duplicate library includes 
#include <iostream>
#include <string>
#include <fstream>
#include <vector>

using namespace std;

class SalesData
{
private:
	ifstream inputfile;
	ofstream outputfile;
	vector<double> sales;
public:
	void loadDataFromFile(string filename);
	void saveBarChartToFile(string filename);
};



void SalesData::loadDataFromFile(string filename)
{
	//loop to read in 
	inputfile.open("Sales.txt", ios::in);
		std::ifstream inputfile(filename);
	int salesAmount;
	if (!inputfile) return;
	while (inputfile >> salesAmount) sales.push_back(salesAmount);
	inputfile.close();

}

void SalesData::saveBarChartToFile(string filename)
{
	//loop to read out 

	std::ofstream graph(filename); //ofstream is an Output File Stream
	graph.open("Graph.txt", ios::out);
	graph << "Sales bar chart\n (each * equals £100)\n";
	for (double salesAmount : sales)
		const std::size_t numAsterisks = std::round(salesAmount / 100);
	graph << std::string( numAsterisks, '*') << '\n';
	graph.close(); //close stream. You always have to close what you open 

}


int main()
{

	SalesData MyData;
	MyData.loadDataFromFile("Sales.txt");
	MyData.saveBarChartToFile("Graphs.txt");
	std::cout << std::ifstream("Graph.txt").rdbuf();
	system("Pause");
	return 0;
}

OP: repeat after me ... when JLBorges sends me a solution I shall not touch even a comma of it but study it super carefully to make myself a better programmer

saying that num_Asterisks is undefined
that's because numAsterisks goes out of scope after the for statetment unless you explicitly scope it as JLBorges did b/w lines 31 and 37 of his/her program and you should do this even for one line of code. You also need to #include <cmath> for std::round (again, as JLBorges did) and do not include the actual file names in the class methods (as s/he did not, see?). These will be passed to the methods in main(). Finally, be aware that #pragma once is non-standard, though widely supported, preprocessor directive https://en.wikipedia.org/wiki/Pragma_once

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#pragma once // stops duplicate library includes
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <cmath>


using namespace std;

class SalesData
{
private:
	ifstream inputfile;
	ofstream outputfile;
	vector<double> sales;
public:
	void loadDataFromFile(string filename);
	void saveBarChartToFile(string filename);
};



void SalesData::loadDataFromFile(string filename)
{
	//loop to read in
	//inputfile.open("D:\\input.txt", ios::in);
		std::ifstream inputfile(filename);
	int salesAmount;
	if (!inputfile) return;
	while (inputfile >> salesAmount) sales.push_back(salesAmount);
	inputfile.close();

}

void SalesData::saveBarChartToFile(string filename)
{
	//loop to read out

	std::ofstream graph(filename); //ofstream is an Output File Stream
	graph << "Sales bar chart\n (each * equals £100)\n";
	for (double salesAmount : sales)
    {
        const std::size_t numAsterisks = std::round(salesAmount / 100);
        graph << std::string( numAsterisks, '*') << '\n';
    }

}


int main()
{

	SalesData MyData;
	MyData.loadDataFromFile("D:\\input.txt");
	MyData.saveBarChartToFile("D:\\output.txt");
	std::cout << std::ifstream("D:\\output.txt").rdbuf();
//	system("Pause");
	return 0;
}
Last edited on
This is my final code. No error messages are being produced when I am building the solution. However, when it is running, there is still no data being displayed within the Graph.txt file. Can anyone help me to solve this please?
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#pragma once // stops duplicate library includes 
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <cmath>

using namespace std;

class SalesData
{
private:
	ifstream inputfile;
	ofstream outputfile;
	vector<double> sales;
public:
	void loadDataFromFile(string filename);
	void saveBarChartToFile(string filename);
};



void SalesData::loadDataFromFile(string filename)
{
	//loop to read in 
	std::ifstream inputfile("Sales.txt");
	int salesAmount;
	if (!inputfile) return;
	while (inputfile >> salesAmount) sales.push_back(salesAmount);
	inputfile.close();

}

void SalesData::saveBarChartToFile(string filename)
{
	//loop to read out 
	std::ofstream graph("Graph.txt"); //ofstream is an Output File Stream
									  //graph.open("Graph.txt", ios::out);
	graph << "Sales bar chart\n (each * equals £100)\n";
	for (double salesAmount : sales)
	{
		const std::size_t numAsterisks = std::round(salesAmount / 100);
		graph << std::string(numAsterisks, '*') << '\n';
		//graph.close(); //close stream. You always have to close what you open

	}
}


	int main()
	{

		SalesData MyData;
		MyData.loadDataFromFile("Sales.txt");
		MyData.saveBarChartToFile("Graphs.txt");
		std::cout << std::ifstream("Graph.txt").rdbuf();
		system("Pause");
		return 0;
	}
Last edited on
Re-ran it again w/o any problems but just to be sure try removing the filenames from the methods. Also, make sure you actually run the program and not just compile it
Sorry what do you mean by remove the filenames from the methods? yeah the program is running
yeah the program is running

good!
Sorry what do you mean by remove the filenames from the methods?

refer to lines 18 and 27 of JLBorges' - the names of the files are passed to the methods, not hard-coded therein
Topic archived. No new replies allowed.