reading from a file using a two dimensional array

hi, i have been going crazy trying to figure this out so if anyone can help that would be great. i have created a class with a data item of type char which is a two dimensional array.

there are three functions first i want to read a file which i am able to do but the second function wants me to display the textfile using a two dimensional array.

this is my code at the moment:

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using std::ifstream;
using std::ofstream;
using std::endl;
using std::cout;

using namespace std;

class auditorium
{
private:
seats [14][30];
public:
void readSeatsConfig();
void displaySeatsConfig();
void getSeatClass();
};

void auditorium::readSeatsConfig(){
ifstream infile;
ofstream outfile;

infile.open("seatsConfig.txt");

if (infile.fail())
{
cout <<"file does not exist"<<endl;
}
else{
cout<<"Reading text file"<<endl;
}


infile.close();
};


void auditorium::displaySeatsConfig(char s[][30], int sizedimension1){
ofstream outfile;


for (int i1=0; i1 < sizedimension1; i1++){
for (int i2; i2 < 30; i2++)
{
outfile << s[i1][i2]<<endl;
}
}
outfile.close();
};


And im not quite sure why this isnt working.

the outcome of this function should display this:

BBBBBAAAAAAAAAAAAAAAAAAAABBBBB
BBBBBBBAAAAAAAAAAAAAAAABBBBBBB
BBBBBBBBBAAAAAAAAAAAABBBBBBBBB
CCBBBBBBBBBAAAAAAAABBBBBBBBBCC
CCCCBBBBBBBBBAAAABBBBBBBBBCCCC
CCCCCCBBBBBBBBAABBBBBBBBCCCCCC
CCCCCCCCBBBBBBBBBBBBBBCCCCCCCC
CCCCCCCCCCBBBBBBBBBBCCCCCCCCCC
CCCCCCCCCCCCBBBBBBCCCCCCCCCCCC
CCCCCCCCCCCCCCBBCCCCCCCCCCCCCC
DDCCCCCCCCCCCCCCCCCCCCCCCCCCDD
DDDDCCCCCCCCCCCCCCCCCCCCCCDDDD
DDDDDDCCCCCCCCCCCCCCCCCCDDDDDD
DDDDDDDDCCCCCCCCCCCCCCDDDDDDDD
DDDDDDDDDDCCCCCCCCCCDDDDDDDDDD

and for some reason it will not show in my outfile.txt???

if you could please help this solution that would be great....im only a beginner so detail would be much appreciated thank you in advance

but where are you reading the file and outputting the file??
as i can see, you are opening the file and closing it. thats it.

paste that part of your code.
This is the new code i had came up with although im still getting an error when running this class through a application.

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
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using std::ifstream;
using std::ofstream;
using std::endl;
using std::cout;

using namespace std;

class auditorium
{
      private:
              char seats [14][30];
      public:
             void readSeatsConfig();
             void displaySeatsConfig(char seats [][30], int size);
             void getSeatClass(char);
             };
             
void auditorium::readSeatsConfig(){
     ifstream infile;
    
    
    infile.open("seatsConfig.txt");
    
    if (infile.fail())
    {
       cerr <<"file does not exist"<<endl;
       }
    else{
         cerr<<"Reading text file"<<endl<<endl;
         }
    
    
    infile.close();
}; 
 
void auditorium::displaySeatsConfig(char [][30], int size){
  
  
  
  ofstream myfile ("seatsConfig.txt");
  if (myfile.is_open())
  {
    while (! myfile.eof() )
    {
      for (int index1=0; index1<size; index1++){
          for (int index2=0; index2<30; index2++)
          {
              myfile << seats[index1][index2]<<endl;
              
              }
              }
    }
    myfile.close();
  }

  else cout << "Unable to open file";
         };
    


and this is my main program

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <fstream>
#include <string>
#include "auditorium.h"
using std::ifstream;
using std::ofstream;
using std::endl;
using std::cerr;

using namespace std;

int main()
{
    auditorium a;
    a.readSeatsConfig();
    a.displaySeatsConfig(char[][30], int size);
    
    
    system("pause");
    
    return 0;
}


im also trying to get a seat which will be in the function getSeatClass, so is there a way to select a value by row and buy column in a two dimensional array ive been trying to find codes over the internet but i havent found anything yet :(
Last edited on
I hope this helps you understand. If you have questions about it, feel free to ask.
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
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using std::ifstream;
using std::ofstream;
using std::endl;
using std::cout;

using namespace std;

class auditorium
{
      private:
              char seats [14][30];
      public:
             void readSeatsConfig();
             void displaySeatsConfig(); // no need for parameters, it gets the info from seats
             char getSeatClass(int, int); // you want to find the class by giving row and column,
// therefor you must return a char and take two ints for row and column
};
             
void auditorium::readSeatsConfig(){
     ifstream infile;
    infile.open("seatsConfig.txt");
    
    if (infile.fail())
    {
       cerr <<"file does not exist"<<endl;
       }
    else{
		cerr<<"Reading text file"<<endl<<endl;
		for (int i = 0; i < 14; ++i) // this is where you need your nested for loops to read in
			for (int j = 0; j < 30; ++j) // just like you did in displaySeatsConfig, but you're
// retreaving data from file, so use >>
				infile >> seats[i][j];     
	}
    infile.close();
}
 
void auditorium::displaySeatsConfig() {
  // we're reading in with the readSeastsConfig() function, here we just want display from seats.
// We're displaying, so use cout
	for (int index1=0; index1<14; index1++) {
                for (int index2=0; index2<30; index2++)
                        cout << seats[index1][index2];
		cout << endl;
	}
}

char auditorium::getSeatClass(int row, int column) {
	// just use the [][] to get the char from the corresponding row and column
	return seats[row][column];
}
Last edited on
Topic archived. No new replies allowed.