[HELP] Reading a 1D and 2D array from a file

I am trying to read two different arrays from a text file, then print them using cout. The first column in the file should be passed to companyNames, and the remaining data should be passed to salesData in the format of rows by columns. I then send pass the arrays to the showData class which should display all of the information like this:

http://i58.tinypic.com/ejzi3d.png

I have a file named data.txt which looks like this:
1
2
3
4
5
6
ABC 40000 50000 60000 70000 
DEF 35000 45000 55000 65000 
GHI 25000 26000 27000 28000 
JKL 31000 32000 33000 34000 
MNO 42000 43000 44000 45000 
GCP 10000 20000 30000 40000 



Here is my 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
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
#include <iostream>
#include <iomanip>
#include <fstream>
#include <istream>
#include <string>

using namespace std;

const int NUM_COMPANIES = 6; //rows of salesData
const int NUM_QUARTERS = 4; //columns of salesData

int main()
{


	string companyNames[NUM_COMPANIES]; //declare a one dimensional string array to hold the names of 6 different companies
	double salesData[NUM_COMPANIES][NUM_QUARTERS]; //a two dimensional double array to hold the sales data for the four quarters in a year
	// There should be NO INITIALIZATION of the array in the declaration.
	void readData(double[][NUM_QUARTERS], string[], int, int); //a function ReadData that reads data from the file and initializes the arrays
	void showData(double[][NUM_QUARTERS], string[], int, int); //The function will print to the screen the contents of the two arrays.


	readData(salesData, companyNames, NUM_COMPANIES, NUM_QUARTERS); // call the function readData, and pass in what is needed
	showData(salesData, companyNames, NUM_COMPANIES, NUM_QUARTERS);

	system("pause");
	return 0;
}

void readData(double salesData[][NUM_QUARTERS], string companyNames[], int NUM_COMPANIES, int NUM_QUARTERS) //In Main, call a function WriteData 
{
	ifstream file("C:\\temp\\data.txt"); // readData will get the information from the Data.txt file
	file.open("C:\\temp\\data.txt");
	file.get();

	for (int row = 0; row < NUM_COMPANIES; row++) // outer loop goes to the next row
	{
		companyNames[row]= file.get(); // the name of the company is on a line followed by the sales data of the 4 quarters.

		for (int col = 0; col < NUM_QUARTERS; col++)// inner loop goes therough the position in the particular row
		{
			salesData[row][col] = file.get(); //read sales data from the file in order
		}
		
	}

	file.close(); // close file after completion 

}

void showData(double salesData[][NUM_QUARTERS], string companyNames[], int NUM_COMPANIES, int NUM_QUARTERS)
{
	for (int  comp= 0; comp < 6; comp++)  // print company name row of salesData
	{
		cout << "Company: " << companyNames[comp] << " "; 
		for (int qtr = 0; qtr < 4; qtr++)
		{
			cout << " QTR " << qtr+1 << " : " << salesData[comp][qtr]; //print sales data for each quarter
		}
		cout << endl; // go to next line after each iteration of the loop
	}
}


The issue is that the values from the text file are not being put into the string.
Last edited on
inf.txt :
ABC 40000 50000 60000 70000
DEF 35000 45000 55000 65000
GHI 25000 26000 27000 28000
JKL 31000 32000 33000 34000
MNO 42000 43000 44000 45000
GCP 10000 20000 30000 40000

program:
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
#include <iostream>
#include <fstream>
using namespace std;

int main ()
{ 	string s[6];
	int ar[6][4];

	ifstream in("inf.txt");
	if(in.is_open() )
	{
	int i=0;
	while(in)
		{
			in>> s[i];
			for(int j=0; j<4; j++)
				{
					in >> ar[i][j];
				}
				
			
		i++;	
		}
	
	in.close();	
	}

	cout << s[2] << " $" << ar[2][0] << endl; // GHI $25000	
	
	return 0;	
}
Last edited on
Hey,

The problem with the previous solution is you may have thousands of companies.

Here is another solution. It will work with any file size. You will need to trim each line:

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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <iostream>
#include <sstream>
#include <fstream>
#include <vector>
#include <list>

class Company
{
public:

   Company(const std::string companyName) : _companyName(companyName) {}

   inline std::string getCompanyName() const { return _companyName; }
   inline Company & addData(const std::string value) { _data.push_back(value); return *this; }
   inline size_t numOfSalesData() const { return _data.size(); }
   inline std::string getQuarterData(const unsigned int quarter) { return _data[quarter]; }

   static const Company * parseLine(const std::string line)
   {
      if (true == line.empty())
      {
         return NULL;
      }

      std::istringstream iss(line);
      std::string data;
      iss >> data;

      Company * const company = new (std::nothrow) Company(data);
      if (NULL == company)
      {
         return NULL;
      }

      while (false == iss.eof())
      {
         iss >> data;	
         company->addData(data);
      }

      return company;
   }

private:
   
   typedef std::vector<std::string>                 DataVector;
   typedef std::vector<std::string>::iterator       DataVectorIterator;
   typedef std::vector<std::string>::const_iterator DataVectorConstIterator;

   friend std::ostream & operator<<(std::ostream & os, const Company & company);

   const std::string _companyName;

   DataVector _data;
};

std::ostream & operator<<(std::ostream & os, const Company & company)
{
   os << "Company Name: " << company._companyName << " ";
   unsigned short int quarter = 1;
   for (Company::DataVectorConstIterator it = company._data.begin(); 
         it != company._data.end(); 
         std::cout << "QTR " << quarter << ": " << *it << " ",
         ++it, ++quarter);

   return os;
}

void readData(std::ifstream & file, std::list<const Company *> & companies)
{
   while (false == file.eof())
   {
      std::string line;
      std::getline(file, line);
      const Company * const company = Company::parseLine(line);
      if (NULL != company)
      {
         companies.push_back(company);
      }
   }
}

void printData(std::ostream & os, const std::list<const Company *> & companies)
{
   for (std::list<const Company *>::const_iterator it = companies.begin(); 
         it != companies.end(); 
         os << **it << std::endl,
         ++it);
}

void freeData(std::list<const Company * > & companies)
{
   for (std::list<const Company *>::const_iterator it = companies.begin(); 
         it != companies.end();
         delete *it,
         ++it);

   companies.clear();
}

int main(const int argc, const char * const argv[])
{
   if (2 > argc)
   {
      std::cerr << "Please specify the filename." << std::endl;
      return 1;
   }

   std::ifstream file(argv[1]);
   if (file.is_open())
   {
      std::list<const Company * > companies;
      readData(file, companies);
      file.close();
      printData(std::cout, companies);
      freeData(companies);
   }
   else
   {
      std::cerr << "Failed to open the file." << std::endl;
   }

   system("pause");

   return 0;
}
i simply showed how to keep data in array, which OP asked - didn't do the complete homework. while doing this kind(level) of programming i assumed the OP know/aware of dynamic memory or vector.
and the program is much simpler..
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
#include <iostream>
#include <fstream>
using namespace std;

int main ()
{ 
	int lines=-1; 
	string *s;
	string st;
	ifstream in("C:\\temp\\data.txt");
	if(in.is_open() )
	{
		while(in)
		{
			getline(in, st);
			lines++;	
		}
	//cout << "lines " << lines << endl;	
	s = new string [5*lines];	
	in.clear();	
	in.seekg(0);
	
	int i=0;
	while(in)
		{
			in>> s[i]; i++;
		}
	
	in.close();	
	}
	else { cerr<<"unable to open file\n"; exit(1); }

	
for (int i=0; i<5*lines ; i+=5)
cout << "Company: " << s[i] << " QTR "<< i%5+1 << ": $" << s[i+1] 
                            << " QTR "<< i%5+2 << ": $" << s[i+2] <<endl;



	delete[] s;
	return 0;
}
Topic archived. No new replies allowed.