Need help with error line 89 File I/O

Need C++ Help. My instructor said the following after making many changes I am at lost it works when ofstream and ifstream are global but not when I move it down in main() please help:

"The following variables should be defined in main(), not global variables:

ofstream outf;
ifstream inf;

What will happen if you move them in main(). Localize them."

So I did this now I am getting an error on line 89. My code bellow followed by the error codes:

My 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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
#include <iostream>

#include<vector>

#include <string>

#include<sstream>

#include <fstream>

using namespace std;

//the files I used

//ofstream outf;

//ifstream inf;

class Question // super class

{

public:

string getQuestion()//gets the question

{

return question;

}

int getValue() //gets the point value of the question

{

return value;

}

virtual void setQuestion(string answer, int value)

{

}

virtual void printOptions()

{

}

virtual string getAnswer()

{

return answer;

}

private:

string question, answer;

int value;

};

class QuestionTF : public Question// class for true and false questions

{

public:

void setQuestion(string theQuestion, int pointValue)

{

string theAnswer;

question = theQuestion;

points = pointValue;

options = "true/false";

//get the answer from the file

getline(inf, theAnswer);

answer = theAnswer;

}

void printOptions()//prints the options for that question

{

cout << question << endl;

cout << answer << endl;

}

string getAnswer()//outputs the answer for that question

{

return answer;

}

private:

string question;

string answer;

int points;

string options;

};

class QuestionMC : public Question//class for multiple choice

{

public:

void setQuestion(string answerarray, int pointValue)

{

stringstream ss(answerarray);

string tok;

getline(ss, tok, ',');

numberOfOptions = stoi(tok);

getline(ss, tok, ',');

int i = 0;

while (getline(ss, tok, ','))

{

if (i>numberOfOptions)

break;

options[i] = tok;

i++;

}

answer = tok;

}

void printOptions()// prints the questions, options, and answer

{

char first = 'A';

cout << question << endl;

for (int count = 0; count<numberOfOptions; count++) {

cout << first++ << ". " << options[count] << endl;

}

cout << "Answer: " << answer << endl;

}

string getAnswer()// prints the answer

{

return answer;

}

private:

int value, numberOfOptions;

string question, answer;

string options[6];

};

int main() {

ofstream outf;

ifstream inf;

Question *myQuestions[10];

string questiontype, questiontxt;

string answertxt, optiontxt;

int numquestions = 4, questionvalue;

// this is something to write the test bank test file from the text given Week 2

// You might use a text file that you create separately and avoid this

// in Week 3 the focus is developing a test file section that can replace this.

outf.open("c:\\temp\\IP2_testbank.txt");

if (outf.is_open())

{

outf << "3\n";

outf << "TF 5\n";

outf << "There exists birds that cannot fly?\n";

outf << "true\n";

outf << "MC 10\n";

outf << "Who was the President of the USA in 1991?\n";

outf << "6\n";

outf << "Richard Nixon\n";

outf << "Gerald Ford\n";

outf << "Jimmy Carter\n";

outf << "Ronald Reagan\n";

outf << "George Bush Sr.\n";

outf << "Bill Clinton\n";

outf << "E\n";

outf << "TF 10\n";

outf << "The city of Boston hosted the 2004 Summer Olympics?\n";

outf << "false\n";

outf.close();

}

else cout << "Unable to open file";

//opening the testbank file and processing as a question of each type

inf.open("c:\\temp\\IP2_testbank.txt");

string line, theQuestion, theAnswer;

if (inf.is_open())

{

//get the number of questions from the first line in the file

getline(inf, line);

numquestions = stoi(line);

for (int count = 0; count<numquestions; count++) {

getline(inf, line);

//get the next line with the question type and the value of the question

int npos = line.size();

int prev_pos = 0;

int pos = 0;

while (line[pos] != ' ')

pos++;

questiontype = line.substr(prev_pos, pos - prev_pos);

prev_pos = ++pos;

questionvalue = stoi(line.substr(prev_pos, npos - prev_pos)); //Last word

//process a true/false question

if (questiontype == "TF")

{

myQuestions[count] = new QuestionTF;

getline(inf, theQuestion);

myQuestions[count]->setQuestion(theQuestion, questionvalue);

}

//process a multiple choice question

if (questiontype == "MC")

{

myQuestions[count] = new QuestionMC;

getline(inf, theQuestion);

//myQuestions[count]->setQuestion(theQuestion, questionvalue);

string str = "";

string line;

//get the number of choices from the file

getline(inf, line);

str = str + line + ",";

int numberOfOptions = stoi(line);

//question = theQuestion;

//value = pointValue;

//get the individual choice lines and load to options array

for (int count = 0; count<numberOfOptions; count++) {

getline(inf, line);

str = str + line + ",";

//options[count] = line;

}

//get the answer from the file and load into answer

getline(inf, line);

str = str + line;

myQuestions[count]->setQuestion(str, 0);

}

}

}

//print out the questions that have been processed

for (int count = 0; count<numquestions; count++)

{

myQuestions[count]->printOptions();

cout << endl;

}

getchar();

return 0;

}


Error:
E0304 no instance of overloaded function "getline" matches argument list line 89
E0020 identifier "inf" is undefiened line 89
C2065 'inf: undeclared identifier

Please help me with this!!!!
You're trying to use a variable named inf. Inside that function, at line 89, there is no such variable. It doesn't exist. You can't use things that don't exist.

Inside a class function, the following variables exist:
Global variables
Class variables
Variables you passed into the function
Last edited on
So I believe I fixed my code but now my professor said the following and I really need help:

Professor feedback:

Objects or Classes do not have to know where the data is coming from whether from a file or from a user input. Classes should be stand alone so that any main program can implement them without binding to anything. In your case, you have designed it in such a way that your classes depend on a file.

Just moving the file variable declaration in the main() alone will not fix the problem. This is what I want you to experience. The program logic needed to be altered.


My updated 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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
#include <iostream>

#include<vector>

#include <string>

#include<sstream>

#include <fstream>

using namespace std;

//the files I used

//ofstream outf;

//ifstream inf;

class Question // super class

{

public:

	string getQuestion()//gets the question

	{

		return question;

	}

	int getValue() //gets the point value of the question

	{

		return value;

	}

	virtual void setQuestion(string answer, int value, ifstream& inf)

	{

	}

	virtual void printOptions()

	{

	}

	virtual string getAnswer()

	{

		return answer;

	}

private:

	string question, answer;

	int value;

};

class QuestionTF : public Question// class for true and false questions

{

public:

	void setQuestion(string theQuestion, int pointValue, ifstream& inf)

	{

		string theAnswer;

		question = theQuestion;

		points = pointValue;

		options = "true/false";

		//get the answer from the file

		getline(inf, theAnswer);

		answer = theAnswer;

	}

	void printOptions()//prints the options for that question

	{

		cout << question << endl;

		cout << answer << endl;

	}

	string getAnswer()//outputs the answer for that question

	{

		return answer;

	}

private:

	string question;

	string answer;

	int points;

	string options;

};

class QuestionMC : public Question//class for multiple choice

{

public:

	void setQuestion(string answerarray, int pointValue, ifstream& inf)

	{

		stringstream ss(answerarray);

		string tok;

		getline(ss, tok, ',');

		numberOfOptions = stoi(tok);

		getline(ss, tok, ',');

		int i = 0;

		while (getline(ss, tok, ','))

		{

			if (i>numberOfOptions)

				break;

			options[i] = tok;

			i++;

		}

		answer = tok;

	}

	void printOptions()// prints the questions, options, and answer

	{

		char first = 'A';

		cout << question << endl;

		for (int count = 0; count<numberOfOptions; count++) {

			cout << first++ << ". " << options[count] << endl;

		}

		cout << "Answer: " << answer << endl;

	}

	string getAnswer()// prints the answer

	{

		return answer;

	}

private:

	int value, numberOfOptions;

	string question, answer;

	string options[6];

};

int main() {

	ofstream outf;

	ifstream inf;

	Question *myQuestions[10];

	string questiontype, questiontxt;

	string answertxt, optiontxt;

	int numquestions = 4, questionvalue;

	// this is something to write the test bank test file from the text given Week 2

	// You might use a text file that you create separately and avoid this

	// in Week 3 the focus is developing a test file section that can replace this.

	outf.open("c:\\temp\\IP2_testbank.txt");

	if (outf.is_open())

	{

		outf << "3\n";

		outf << "TF 5\n";

		outf << "There exists birds that cannot fly?\n";

		outf << "true\n";

		outf << "MC 10\n";

		outf << "Who was the President of the USA in 1991?\n";

		outf << "6\n";

		outf << "Richard Nixon\n";

		outf << "Gerald Ford\n";

		outf << "Jimmy Carter\n";

		outf << "Ronald Reagan\n";

		outf << "George Bush Sr.\n";

		outf << "Bill Clinton\n";

		outf << "E\n";

		outf << "TF 10\n";

		outf << "The city of Boston hosted the 2004 Summer Olympics?\n";

		outf << "false\n";

		outf.close();

	}

	else cout << "Unable to open file";

	//opening the testbank file and processing as a question of each type

	inf.open("c:\\temp\\IP2_testbank.txt");

	string line, theQuestion, theAnswer;

	if (inf.is_open())

	{

		//get the number of questions from the first line in the file

		getline(inf, line);

		numquestions = stoi(line);

		for (int count = 0; count<numquestions; count++) {

			getline(inf, line);

			//get the next line with the question type and the value of the question

			int npos = line.size();

			int prev_pos = 0;

			int pos = 0;

			while (line[pos] != ' ')

				pos++;

			questiontype = line.substr(prev_pos, pos - prev_pos);

			prev_pos = ++pos;

			questionvalue = stoi(line.substr(prev_pos, npos - prev_pos)); //Last word

																		  //process a true/false question

			if (questiontype == "TF")

			{

				myQuestions[count] = new QuestionTF;

				getline(inf, theQuestion);

				myQuestions[count]->setQuestion(theQuestion, questionvalue, inf);

			}

			//process a multiple choice question

			if (questiontype == "MC")

			{

				myQuestions[count] = new QuestionMC;

				getline(inf, theQuestion);

				//myQuestions[count]->setQuestion(theQuestion, questionvalue);

				string str = "";

				string line;

				//get the number of choices from the file

				getline(inf, line);

				str = str + line + ",";

				int numberOfOptions = stoi(line);

				//question = theQuestion;

				//value = pointValue;

				//get the individual choice lines and load to options array

				// count variable is already defined, so I changed it to _count

				for (int _count = 0; _count<numberOfOptions; _count++) {

					getline(inf, line);

					str = str + line + ",";

					//options[count] = line;

				}

				//get the answer from the file and load into answer

				getline(inf, line);

				str = str + line;

				myQuestions[count]->setQuestion(str, 0, inf);

			}

		}

	}

	//print out the questions that have been processed

	for (int count = 0; count<numquestions; count++)

	{

		myQuestions[count]->printOptions();

		cout << endl;

	}

	getchar();

	return 0;

}


Please assist and let me know what I am doing wrong? As far as vector goes I would like explanation on how to do this because I am not to familiar with it professor gives me feedback but I am having a hard time keeping up.
why did you double-space everything? Trying to make us scroll twice as far?

Perhaps he meant to use the base class of ifstream, so perhaps rework to take an istream reference. After that rework, it'd be able to accept ifstream, istringstream, cin.
I apologize icy1 When I copied my code in from visual studio it formatted that way. I will make sure not to do that next time. So I replaced ifstream with istream for my functions to reference I am not entirely sure if I am doing this right?
Last edited on
Topic archived. No new replies allowed.