fstream and overloading questions

Hello, I posted earlier about my school project giving me some trouble, and while that was resolved, I've run in to some more, much more daunting issues.

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
#include<iostream>
#include<conio.h>
#include<stdio.h>		//load libraries
#include<stdlib.h>
#include<time.h>
#include<fstream>
#include<string>
using namespace std;

	
	string inFile;		//declare file names
	string outFile;

	instream.open("inFile.txt");   //open the files
	outstream.open("outFile.txt");

	ifstream instream(inFile.data());	//get the data
	ofstream outstream(outFile.data());     //and declare fstream names


	if (instream.fail())
	{
	cout << "Input file opening failed.\n";  //confirm file opened
	exit(1);
	}

	if (outstream.fail())
	{
	cout << "Output file opening failed.\n"; //confirm file opened
	exit(1);
	}
	instream.close();
	outstream.close();  //close the files



class MathProblem 	//create mathproblem class
{
private:		
	
	int first;
	int second;
	int getOper;		//private variables
	int answer;
	int correct;
	char operate;
	
public:

	void showOther();
	void showProblem();		//public member functions
	void setProblem();
	void getAnswer();
};	

void MathProblem::setProblem()	//start setProblem
{

	srand(time(0));	//changes the rand() each time function is called
	first = rand() % 60 + 12;	//makes a random integer between 12 and 72
	second = rand() % 10 + 1;	//makes a random integer between 1 and 10
	getOper = rand() % 4 + 1;	//makes a random integer between 1 and 4, then selects an operator based on that number and the comparisons below
	
	if (getOper == 1)
	{
		operate = '+';		//if getOper is 1, the equation is addition
	}
	if (getOper == 2)
	{
		operate = '-';		//if getOper is 2, the equation is subtraction
	}
	if (getOper == 3)
	{
		operate = '*';		//if getOper is 3, the equation is multiplication
	}
	if (getOper == 4)
	{
		operate = '/';		//if getOper is 4, the equation is division
	}	

}

void MathProblem::showProblem()
{
	outstream << "For division, don't forget to round up!" << endl;	//reminding user to round up for division, because calculating % is lol
	outstream << first << operate << second << " = " << endl;		//display problem
	
	if (getOper == 1)
	{
	correct = first + second;		//using the getOper comparisons found in setProblem, display appropriate equation
	}
	if (getOper == 2)
	{
	correct = first - second;		//using the getOper comparisons found in setProblem, display appropriate equation
	}
	if (getOper == 3)
	{
	correct = first * second;		//using the getOper comparisons found in setProblem, display appropriate equation
	}
	if (getOper == 4)
	{
	correct = first / second;		//using the getOper comparisons found in setProblem, display appropriate equation
	}
}

void MathProblem::getAnswer()
{
	instream >> answer;		//get answer from the user
}

void MathProblem::showOther()
{

	outstream << "Your answer was: " << answer << endl;		//tell the user what their answer was
	outstream << "The correct answer was: " << correct << endl;		//tell the user the correct answer
	outstream << "Were you correct? ";		//compare the users answer with the correct answer
	if (answer == correct)
	{
		outstream << "1";		//display '1' if the user was right
	}
	else
	{
		outstream << "0";		//display '0' if the user was wrong
	}
}

class MathProblemWithAnswer : public MathProblem		//create MathProblemWithAnswer class, and inherit MathProblem
{
private:

	int right;		//private variable

public:
	void setProblem(int first, int second, char oper8);
	void getAnswer(int input);			//set overloaded functions with their paramaters
	void showOther(int compare);
};

void MathProblemWithAnswer::setProblem(int num1, int num2, char oper8)		//use the same basic function, but different variables that are stated here
{
	srand(time(0));
	num1 = rand() % 60 + 12;
	num2 = rand() % 10 + 1;
	char randOper = rand() % 4 + 1;
	
	if (randOper == 1)
	{
		oper8 = '+';
	}
	if (randOper == 2)
	{
		oper8 = '-';
	}
	if (randOper == 3)
	{
		oper8 = '*';
	}
	if (randOper == 4)
	{
		oper8 = '/';
	}	
}

void MathProblemWithAnswer::getAnswer(int input)	
{
	instream >> input;	//get answer from the user
}

void MathProblemWithAnswer::showOther(int compare)		//compare the input with the correct answer, like before
{

	outstream << "Your answer was: " << right << endl;
	outstream << "The correct answer was: " << compare << endl;		
	outstream << "Were you correct? ";
	if (right == compare)
	{
		outstream << "1";
	}
	else
	{
		outstream << "0";
	}
	
}



void main()
{
	MathProblem problem;
	problem.setProblem();		//call all o' dem functions
	problem.showProblem();
	problem.getAnswer();
	problem.showOther();

	MathProblemWithAnswer problemAnswer;
	void setProblem();
	void getAnswer();			//call dem functions too
	void showOther();	

}


Basically, everything above the class MathProblem declaration is giving me trouble. I've tried putting it all in a function, in the main, as a class, moving it around, nothing will work. all of my 'outstream' instances are saying they have an undeclared identifier, although I did have it working (I believe when it was in my int main())

the bolded If statement says expected a declaration, only one stream.close() is underlined...

basically everythings a mess, and nothing I do seems to get me anywhere, and all the errors seem to be completely irrelevent (like saying 'expected a ;' when there is one before it)

Also, my MathProblemWithAnswer business is supposed to demonstrate I understand inheritance, but my lab instructor explained it miserably, I think it's basically supposed to do the exact same thing as MathProblem, but use overloaded functions, but I don't know how to declare that in the Main(), as adding parameters to the () after the function name always yields errors.

I apologize for such a long post, but it's pretty late and I'm getting desperate for some guidance, any help will go a long way! Thank you.

tl;dr:

fstreams are hard, were barely taught them.
You need to put line 11-33 into a function (and call that function).

Line 17/18 must be placed before 14/15. An object must be instantiated (line 17/18) before it can be accessed (line 14/15).

On line 17/18 the streams will be opened so you can omit line 14/15.

Since the data() function of string does not necessarily has a trailing 0 you need to change line 17/18 to:
1
2
	ifstream instream(inFile.c_str());	//get the data
	ofstream outstream(outFile.c_str());     //and declare fstream names 


to notify the user use cout not outstream
to get input from the user use cin not instream
since you don't need outstream/instream you can discard line 11-33 entirely

lines 197-199 are function declarations not calls. Omit void to call them
Topic archived. No new replies allowed.