OutFile not working

Hello everyone, I am new to c++, I am working on a project for school (completed) the only piece I am missing is that I need to also create an outputFile with different elements. Currently the only thing I would expect to be in the output file would be the firstName, lastName, and "User Name" and a time stamp. The file "name.txt" is created, but it is always blank when I open it... any ideas as to why? Also my entire code was to long to fit in this message, just know that Subtraction, Division, and Multiplication are coded like the Add(level) function, just for the other operators. Thanks


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
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <cmath>
#include <stdlib.h>
#include <ctime>

// Prototype goes here //
using namespace std;
char userinf(ofstream&);
char Math_menu(ofstream &, int level);
int difficulty_menu(ofstream&);
void Multiply(int level, ofstream&);
void Add(int level, ofstream&);
void Subtract(int level, ofstream&);
void Divide(int level, ofstream&);
//double randomGen(int level);


int main()
{
	ofstream outputFile;
	outputFile.open("name.txt");
	unsigned seed = time(NULL);
	srand(seed);
	userinf(outputFile);
	int level = difficulty_menu(outputFile);
	Math_menu(outputFile, level);


}

char userinf(ofstream &outputFile)
{
	string firstName;
	string lastName;
	cout << "Please enter your first name " << endl;
	cin >> firstName;
	outputFile << firstName;
	outputFile << lastName;
	cout << "Please enter your last name " << endl;
	cin >> lastName;
	outputFile << firstName << lastName;
	return 0;
}
int difficulty_menu(ofstream &outputFile)
{
	int level;
	int choice;
cout << "\t	---------------------------------------------------------" << endl;
	cout << "\tDifficulty Menu" << endl;
cout << "	---------------------------------------------------------" << endl;
	cout << "\tPlease choose your desired level of difficulty" << endl;
	cout << "\t1. Beginner " << endl;
	cout << "\t2. Intermediate " << endl;
	cout << "\t3. Advanced " << endl;
	cout << "\t4. Expert " << endl;
	cout << "\t5. Mathmatician" << endl;
	cout << "\t---------------------------------------------------------" << endl;
	cout << "\tEnter your choice[1 2 3 4 5]:";

	cin >> level;// paramater


	switch (level)
	{

	case 1:
		cout << "You haven Choosen level 1" << endl;
		break;
	case 2:
		cout << "you have choosen level 2" << endl;
		break;
	case 3:
		cout << "you have choosen level 3" << endl;
		break;
	case 4:
		cout << "you have choosen level 4" << endl;
		break;
	case 5:
		cout << "You have choosen level 5" << endl;
		break;
	default:
		cout << " Please make a selection 1-5" << endl;
	}

	system("pause");

	return level;
}


char Math_menu(ofstream &outputFile, int level)
{
	{
		bool test = true;
		while (test = true)
		{

			int basic;
			char input[100]; // array allows for more than on input.
			system("cls");

			cout << "ARITHMETIC PRACTICE PROGRAM" << endl;
			cout << "Hello, and welcome to the Math Skills Practice Program.\n"
				"This program allows you to practice your math skills.\n"
				"Choose what you want to practice in the menu shown below." << endl;
cout << "	---------------------------------------------------------" << endl;
			cout << "               ARITHMETIC PRACTICE PROGRAM" << endl;
			cout << "                        MAIN MENU" << endl;
cout << "	---------------------------------------------------------" << endl;
			cout << "               a.Addition" << endl;
			cout << "               b.Subtraction" << endl;
			cout << "               c.Multiplication" << endl;
			cout << "               d.Division" << endl;
			cout << "               q.Quit the program" << endl;
cout << "   ---------------------------------------------------------" << endl;
			cout << "               Enter your choice[a b c d q]:";


			cin >> input;
			basic = input[0]; // [0] uses the first char of our 100 list, since we do not use char it is not a declaration of size// 

			cout << " your level is " << level;

			switch (basic)
			{

			case  'a':
				cout << "you have choosen addition" << endl;
				Add(level, outputFile);
				break;
			case  'b':
				cout << "you have choosen Subtraction" << endl;
				Subtract(level, outputFile);
				break;
			case  'c':
				cout << "you have choosen Multiplication" << endl;
				Multiply(level, outputFile);
				break;
			case  'd':
				cout << "you have choosen Division" << endl;
				Divide(level, outputFile);
				break;
			case 'q':
				test = false;
				break;

			}

			system("pause");


		}
		return 0;
	}
}

void Add(int level, ofstream &outputFile)
{
	double number1;
	double number2;
	double realAnswer;
	double rand_answer;
	int multi_choice[4];
	int position = rand() % 4;





	if (level == 1) {

		number1 = (rand() % 10 + 1);
		number2 = (rand() % 10 + 1);
		for (position = 0; position < 4; position++){
			multi_choice[position] = (rand() % 10 + 1);
		}
	}

	if (level == 2) {

		number1 = (rand() % (99 - 10) + 10);
		number2 = (rand() % (99 - 10) + 10);
		for (position = 0; position < 4; position++){
			multi_choice[position] = (rand() % (99 - 10) + 10);
		}
	}

	if (level == 3) {

		number1 = (rand() % (999 - 100) + 100);
		number2 = (rand() % (999 + 100) + 100);
		for (position = 0; position < 4; position++){
			multi_choice[position] = (rand() % (999 + 100) + 100);
		}
	}
	if (level == 4) {

		number1 = (rand() % (9999 - 1000) + 1000);
		number2 = (rand() % (9999 - 1000) + 1000);
		for (position = 0; position < 4; position++){
			multi_choice[position] = (rand() % (9999 - 1000) + 1000);
		}
	}
	if (level == 5) {

		number1 = (rand() % (99999 - 10000) + 10000);
		number2 = (rand() % (99999 - 10000) + 10000);
		for (position = 0; position < 4; position++){
			multi_choice[position] = (rand() % (99999 - 10000) + 10000);
		}
	}

	cout << " Please answer the following question: \n" << endl;
	cout << " " << number1 << endl;
	cout << "+" << number2 << "\n________" << endl;


	char answer;
	int loopCounter = 0;


	realAnswer = number1 + number2;

	for (; loopCounter < 3; loopCounter++){

		int a_position = rand() % 5;
		if (a_position != 4){
			multi_choice[a_position] = realAnswer;
		}

		char letter_realAnswer = a_position + 97;

		cout << "'a'" << multi_choice[0] << endl;
		cout << "'b'" << multi_choice[1] << endl;
		cout << "'c'" << multi_choice[2] << endl;
		cout << "'d'" << multi_choice[3] << endl;
		cout << "'e'"    "None of the above" << endl;

		cin >> answer;

		if (answer == letter_realAnswer) {
			cout << "congrats, thats right!" << endl;
			break;
		}
		else {
			cout << "Please try again" << endl;

		}

	}

}
Last edited on
closed account (48T7M4Gy)
Instead of wading through all of the above have you tried writing a small test program to write to a file?
closed account (48T7M4Gy)
Another thing to do is do a search on the file name. You may find that the filled out file is in another directory.
Hello asper18,

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
kemort has a good point, but I would first check to see if the file is actually open. I like using this bit of code until I come up with something better:
1
2
3
4
5
6
7
8
9
10
11
if (inFile.is_open())
{
	std::cout << "\n File " << fileName << " is open" << std::endl;
	Sleep(SLEEP_TIME);
else
{
	std::cout << "\n File " << fileName << " did not open" << std::endl;
	Sleep(SLEEP_TIME);
	exit(1);
}
 

The Sleep() function works for me in Windows, but may not work for everyone. SLEEP_TiME is a #define in a header file I include in my programs. 1000 is equal to one second.

I have looked over your code several times, but have yet to find where you have actually written to the output file.
If your posted code will compile I check it out.

Hope that helps,

Andy
Last edited on
Hello asper18,

Upon further review and testing I found where you write to the output file. I also changed outputFile.open("name.txt"); to outputFile.open("name.txt", std::ios::app);. Usually when I am testing a program I like to use outputFile.open("name.txt", std::ios::trunc | std::ios::ate);. I have found that when using “trunc” that you need to use “ate” instead of “app”.

In the actual output line I added a space between first and last names.

In the math_menu function you have these two lines
1
2
bool test = true;
while (test = true)

In the while condition I think you meant to say “==” not “=”. The “=” sets test to true each time the while condition is checked which is the same as saying while(1) which is an endless loop. All you really need is while(test) and when test is set to false the while loop will end.

Hope that helps,

Andy
Hello Everyone, Thank you for your input... I wrote a small program separate from this that I was able to get input from, so I feel like I set it up right, as i set up the same way. I don't believe I would know how to write it as a function and use it. I'm not sure how to check that it is in the right directory, but I use visual studios 2015, you click on open, file, and there is a file called name.txt there. The program needs to write the name of the user, record their difficulty level, record their math problem, and answers.

in the code

outputFile << fistName;
outputFile << lastName;

doesn't that write the fist and last name to the output file?

Thanks again
closed account (48T7M4Gy)
It should work, try it and see what you get :)

http://www.cplusplus.com/doc/tutorial/files/ I find is always useful.
Last edited on
IT WORKS!!!!! Thank you guys so much!!!
Topic archived. No new replies allowed.