Getting an annoying error

This is the error i am getting from my compiler, not sure why but it is quite frustrating!!
For this i have to use structs and arrays of characters. Not supposed to use strings or class yet.
[Error] no match for 'operator[]' in 'game[i]'


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
bool file_write(char filename[], game_info & ,  const int & num_games)
{
	game_info game;
    bool success = true;
    ofstream file_out;
    file_out.open(filename);  //rewrite over the entire file!
    
    if (!file_out) //true -- connect; false -- problem
    {
       cout <<"CAN'T SAVE...\n\n";
       success = false;
    }
    else
    {
       for(i = 0; i < 20; ++i)
       {
        file_out << game[i].name_of_game <<"|";

	   
	   
	   }
       file_out.close();
       file_out.clear();
    }
    return success;
}

At line 17, game is treated as though it is an array. But game was defined on line 3 as a single object.
okay, i guess the problem is more than just what was in the tidbit of code i posted.

so here is the rest, with the change made to line 3 and 17


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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include <iostream>
#include <fstream>
using namespace std;
#include <cstring>
#include <cctype>

const int game_name = 35;
const int rating_of_game = 6;
const int platform = 30;
const int comments = 250;
const int summary = 400;
const int email = 35;


struct game_info
{
char name_of_game[game_name];
char game_rating[rating_of_game];
char game_platform[platform];
char game_comments[comments];
char summary_of_game[summary];
char email_address[email];
bool availability;
};


//prototypes
void readall(char prompt[], int max_size, char result[]);
void getting_game_info(game_info & game);
char display(game_info & game);
char ask_if_correct(char character);
void manage();
//bool load_games(char filename[], game_info & game, int & num);
//bool save_games(char filename[], int & num);
bool file_read(char filename[], game_info & game);
bool file_write(char filename[], game_info & game, int & num_games);
void getfilename(char filename[]);
void getfilename(char array[]);


int main(){
	char filename[32];
	int num_games = 0;
   game_info game[20];
   char character;
   
  
    /*cout <<"please enter a file name including the extension, under 32 chars";
	cin.get(filename, 34, '\n');
	cin.ignore();*/
 getfilename(filename);
  //file_write(filename, *game);
   file_read(filename, *game);
   file_write(filename, *game, num_games);
  
   manage();
 

}

void manage(){


        



	ifstream file_in;
	ofstream file_out;
	int num_games;
	//if(!load_games(filename, game_info & game, num_games))
	    //cout <<"we are starting from scratch, no games existing";
        
	
	game_info game[20];
	char character;
	char response;
	cout <<"would you like to enter a game? Enter eiher y or n";
	cin >>response;
	cin.ignore();
	while(response == 'y')
	{
for(int i = 0;i < 20 && response == 'y';++i ){

 getting_game_info(game[i]);
 cout <<endl<<endl <<endl;
 cout <<"Would you like to add more games? Type either lowercase y or n" <<endl;
 cin >> response;
 cin.ignore();
 }
 }
 } 
 
 


void readall(const char prompt[], int max_size, char result[])
{
	cout  <<prompt << '\n';
	cin.get(result, max_size, '\n');
	cin.ignore(100, '\n');
}	

void getting_game_info(game_info & game)
{
		
	char character;
           do {
	             readall("please enter name of game", game_name, game.name_of_game);
                 readall("please enter the rating of the game", rating_of_game, game.game_rating);
	             readall("please enter the game platform", platform, game.game_platform);
	             readall("please enter your comments about the game", comments, game.game_comments);
	             readall("please enter your summary of the game", summary, game.summary_of_game);
	             readall("please enter your email address", email, game.email_address);
        
                 cout <<"Is this information correct? Enter either y or n." << '\n';
                 cin >>character;
                 cin.ignore();
                 cout <<'\n';
           } while((character == 'n') || (character == 'N'));
    
     
}	
char display(game_info & game)
{
	cout <<"The name of the game is: "<<game.name_of_game << endl;
	cout << "The rating of the game is: "<<game.game_rating <<endl;
	cout <<"The game platform is: "<<game.game_platform <<endl;
	cout << "The game comments are: "<<game.game_comments <<endl;
	cout << "Summary of the game: "<<game.summary_of_game <<endl;
	cout << "Persons email address: "<<game.email_address <<endl;
}

bool file_read(char filename[], game_info & game)
{   
	ifstream file_in;
	
	
	int num_games;
	int i = num_games;
	file_in.open(filename, ios::app);
	
	    if (!file_in) //not connected
       {
		cerr <<"problem";
		return false;
		}
	    
    //Connected to the file and ready to read
    file_in.get(game.name_of_game, game_name,'|');
    file_in.ignore(100,'|');
    /*while(read && !read.eof()) //previous read is successful
    {
        read.get(games[num].description,SIZE_DESC,'|');
        read.ignore(100,'|');
        read.get(games[num].category, SIZE_CAT, '|');
        read.ignore(100,'|');
        read.get(games[num].rating, SIZE_RATING, '|');
        read.ignore(100,'|');
        read >> games[num].length;
        read.ignore();
        ++num;
        read.get(games[num].title,SIZE_TITLE,'|');
        read.ignore(100,'|');
    }*/
    file_in.close();
    return true;
}


bool file_write(char filename[],  int &num_games)
{
	game_info game[20];
    bool success = true;
    ofstream file_out;
    file_out.open(filename);  //rewrite over the entire file!
    
    if (!file_out) //true -- connect; false -- problem
    {
       cout <<"CAN'T SAVE...\n\n";
       success = false;
    }
    else
    {
       for(int i = 0; i < 20; ++i)
       {
        file_out << game[i].name_of_game <<"|";

	   
	   
	   }
       file_out.close();
       file_out.clear();
    }
    return success;
}


/*bool file_write(char filename[], game_info & game){
	ofstream file_out;
	file_out.open(filename,ios::app);
	file_out<<game.name_of_game, game_name, '\n';
	file_out.close();
	file_out.clear();
}
*/

void getfilename(char array[])
{
    readall("Please enter the name of a file limited to 31 characters: ", 31, array);
    //strcat(array,".txt");
}
I'm not sure whether there was a specific question there.

anyway, the prototype declaration at line 36
 
bool file_write(char filename[], game_info & game, int & num_games);

should match the function definition at line 171
 
bool file_write(char filename[],  int &num_games)
The problem is the error i listed above, it is giving that error and i am not quite sure why, or how to fix it.

even with the changes listed above the same problem is there.
Well, I took the trouble to compile the code posted above, and the main error was the one I pointed out in my last post. Though I did not attempt to run it, as I've no idea whether it is complete or a work in progress.
sorry if i am being confusing.
My problem i am having is i am trying to have the for statement in line 185 iterate through the array of games and for some reason the operators in the for statement and the place i put [i] do not match
giving the error

[Error] no match for 'operator[]' in 'game[i]'
When I compile the code, these are the errors I get:

Untitled19.cpp: In function 'int main()':
Untitled19.cpp:45:9: warning: unused variable 'character' [-Wunused-variable]
Untitled19.cpp: In function 'void manage()':
Untitled19.cpp:70:6: warning: unused variable 'num_games' [-Wunused-variable]
Untitled19.cpp:76:7: warning: unused variable 'character' [-Wunused-variable]
Untitled19.cpp: In function 'char display(game_info&)':
Untitled19.cpp:132:1: warning: no return statement in function returning non-void [-Wreturn-type]
Untitled19.cpp: In function 'bool file_read(char*, game_info&)':
Untitled19.cpp:140:6: warning: unused variable 'i' [-Wunused-variable]
Untitled19.cpp: At global scope:
Untitled19.cpp:171:6: warning: unused parameter 'num_games' [-Wunused-parameter]
Untitled19.cpp: In function 'bool file_read(char*, game_info&)':
Untitled19.cpp:140:10: warning: 'num_games' may be used uninitialized in this function [-Wuninitialized]
Untitled19.cpp:54: undefined reference to `file_write(char*, game_info&, int&)'
collect2.exe: error: ld returned 1 exit status

Notice the error you describe is not among them. Are you sure the code posted is the same version which is giving the error described?
Are you compiling with g++?
Because i just compiled with g++ (what my teacher wants us to use) and still got the same error i said.
Your problem is simple, you are accessing array index 'i' of your game variable. Your game variable is not an array. Instead, you want to do: game.name_of_game[i] where the brackets are after the variable that is an array.
Topic archived. No new replies allowed.