Help with opening a file as an argument

I am having an issue in the function fillSquare. My problem is that I can't figure out how to take the argument and put the data from the file into the array. I also can't seem to open the file. Lines 101-122

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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
 #include <iostream>
#include <iomanip>
#include <cstdlib>
#include <fstream>

using namespace std;

//Place the class definition after this line

class LoShuMagicSquare
{
public:

twod();

void fillSquare( const char[]);
void printSquare();

bool isMagic();

private:
	#define row 3
	#define column 3
	int square[row][column];
};
LoShuMagicSquare::twod(){
	int square[row][column] = {
		{0, 0, 0} ,
		{0, 0, 0} ,
		{0, 0, 0}
	};
}


int main()
{
//Create a LoShuMagicSquare object that will be used to test the 4 puzzles
LoShuMagicSquare puzzle;


//Puzzle 1 using loshu_puzzle1.txt. The object will be filled, displayed,
//and then tested to see if it is a valid solution

cout << "Puzzle 1:" << endl << endl;
puzzle.fillSquare( "loshu_puzzle1.txt");

puzzle.printSquare();

cout << endl << "Is it magic? " << ( puzzle.isMagic() ? "Yes": "No" ) << endl << endl << endl;


//Puzzle 2 using loshu_puzzle2.txt. The object will be filled, displayed,
//and then tested to see if it is a valid solution
cout << "Puzzle 2:" << endl << endl;

puzzle.fillSquare( "loshu_puzzle2.txt");

puzzle.printSquare();

cout << endl << "Is it magic? " << ( puzzle.isMagic() ? "Yes": "No" ) << endl << endl << endl;


//Puzzle 3 using loshu_puzzle3.txt. The object will be filled, displayed,
//and then tested to see if it is a valid solution
cout << "Puzzle 3:" << endl << endl;

puzzle.fillSquare( "loshu_puzzle3.txt");

puzzle.printSquare();

cout << endl << "Is it magic? " << ( puzzle.isMagic() ? "Yes": "No" ) << endl << endl << endl;


//Puzzle 4 using loshu_puzzle4.txt. The object will be filled, displayed,
//and then tested to see if it is a valid solution
cout << "Puzzle 4:" << endl << endl;

puzzle.fillSquare( "loshu_puzzle4.txt");

puzzle.printSquare();

cout << endl << "Is it magic? " << ( puzzle.isMagic() ? "Yes": "No" ) << endl << endl << endl;


return 0;
}

//Code the methods below this line

/***************************************************************
Function:  void fillSquare

Use: opens the input file then puts the values into the array

Arguments: const char

Returns: nothing

Note: 
***************************************************************/
void LoShuMagicSquare::fillSquare( const char[]){
ifstream infile;
infile.open(const char[]);
int data[row][column];
if(infile.fail()){
cout << "File failed to open" << endl;
}
else {

	while(!infile.eof()){
		for(int i = 0; i < row; i++){
		
			for(int j = 0; j < column; j++){
			
			infile >> data[i][j];
			}
		}
	}
}
infile.close();
}

/***************************************************************
Function:  void printSquare

Use: prints the values from the array

Arguments: none

Returns: nothing

Note: 
***************************************************************/

void LoShuMagicSquare::printSquare(){
	
		for(int i = 0; i < row; i++){
		
			for(int j = 0; j < column; j++){
				cout << square[i][j];
			}
		}
}
/***************************************************************
Function:  bool isMagic

Use: determines if the square is indeed magic

Arguments: none

Returns: true or false

Note: 
***************************************************************/
bool LoShuMagicSquare::isMagic(){
}
Last edited on
You need to give the function parameter a name so that you have a way to pass it to the open function.

1
2
3
4
void LoShuMagicSquare::fillSquare( const char filename[]){
ifstream infile;
infile.open(filename);
...

Topic archived. No new replies allowed.