Help with creating and writing to a file in a specific format.

Hi I am having trouble figuring out where to start with my project. What I need to do is instead of me creating the Test.txt file and inputting the needed information in a specific format(below), I need to allow the user to create the file, input the information in the specified format, then save it and print it out to the screen. In the end the output on the screen should look the same as it does now. I have posted my Test.txt file I have created (which I need to change to allow the user to create) and an example of the format it needs to be in below along with my code I have now. I am taking online classes and the teacher has been supposedly sick and nowhere to be found the whole first three weeks of the class (Which I have already filed a claim with the school) so I am basically trying to teach myself and it is not working very well. Any help will be GREATLY appreciated. Thank you for any help in advance.

FORMAT EXAMPLE OF THE TEST.TXT FILE:

3 - How many questions
TF 5 - What type of question / value of question
There exist birds that cannot fly? - Question
true - answer
MC 10 - What type of question / value of question
Who was the President of the USA in 1991? - Question
6 - how many choices
Richard Nixon - choice 1 (a)
Gerald Ford - choice 2 (b)
Jimmy Carter - choice 3 (c)
Ronald Reagan - choice 4 (d)
George Bush Sr. - choice 5(e)
Bill Clinton - choice 6 (f)
E - answer
TF 10 - What type of question / value of question
The city of Boston hosted the 2004 Summer Olympics? - Question
false - answer

END OF TEST.TXT FILE

MY TEST.TXT FILE I HAVE NOW:
6
TF 5
5 + 5 = 55?
false
MC 10
What is 9 - 6?
6
Two (a)
Three (b)
Six (c)
Seven (d)
Nine (e)
Ten (f)
B
TF 5
10 * 500 = 5000?
true
TF 5
9,625 / 35 = 275?
true
MC 10
What is 100 * 5?
6
Two hundred (a)
Three hundred (b)
Six hundred (c)
Seven hundred (d)
Nine hundred (e)
Five hundred (f)
F
MC 10
What is 1000 - 956?
6
Nineteen (a)
Fifty four (b)
Fourty six (c)
Fourty four (d)
Seventy four (e)
Fifty six (f)
D

END OF MY TEST.TXT FILE

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
#include <iostream>
#include <string>
#include <fstream>



class Quiz
{

private:

	int score;


	int numberOfQuestions;
	int numberOfAnswers;
	std::string questionType;
	int questionValue;
	std::string question;
	std::string mcAnswers[10];
	std::string tfAnswer;

	std::fstream quizData;

	bool openQuiz(std::string);
	void closeQuiz();
	void doQuestions();

public:
	Quiz();
	bool Run(std::string);

};

Quiz::Quiz()
{
	score = 0;
}


bool Quiz::openQuiz(std::string Test)
{
	quizData.open(Test, std::ios::in);
	if (quizData.fail())
		return false;
	else
		return true;

}


void Quiz::closeQuiz()
{
	quizData.close();
}

void Quiz::doQuestions()
{
	std::string numberOfQuestionsData;
	std::string questionValueData;
	std::string numberOfAnswersData;
	std::string mcData;



	getline(quizData, numberOfQuestionsData);
	numberOfQuestions = stoi(numberOfQuestionsData);
	for (int i = 0; i < numberOfQuestions; i++) {
		getline(quizData, questionType, ' ');
		if (questionType == "TF") {
			getline(quizData, questionValueData);
			questionValue = stoi(questionValueData);
			getline(quizData, question);
			std::cout << "Question #" << i + 1 << ": True of False: " << question << " Points value = " << questionValue << std::endl;
			getline(quizData, tfAnswer);
			std::cout << tfAnswer << std::endl;
		}
		if (questionType == "MC") {
			getline(quizData, questionValueData);
			questionValue = stoi(questionValueData);
			getline(quizData, question);
			std::cout << "Question #" << i + 1 << ": Multiple Choice: " << question << " Points value = " << questionValue << std::endl;
			getline(quizData, numberOfAnswersData);
			numberOfAnswers = stoi(numberOfAnswersData);
			for (int k = 0; k < numberOfAnswers; k++) {
				getline(quizData, mcAnswers[k]);
				std::cout << mcAnswers[k] << std::endl;
			}
			getline(quizData, tfAnswer);
			std::cout << tfAnswer << std::endl;
		}

	}
	if (questionType != "MC", "TF") {
		std::cerr << "Either there has been an error! Or this is the end of the file. Check the file to be sure." << std::endl;
	}
	

}

bool Quiz::Run(std::string Test)
{
	if (openQuiz(Test)) {
		doQuestions();
		closeQuiz();
	}
	else
		return false;
	return true;
}


int main(int argc, char* argv[])
{
	Quiz Game;

	if (!Game.Run("Test.txt"))
		std::cerr << "Failed to run due to missing quiz data.";

	std::cin.ignore();
	return 0;
}
it looks like a good start... what exactly is it not doing correctly?


The program I have now works well. I need to change it to allow the user to create the Test.txt file then input the information in the exact format I created it. Since my teacher is nowhere to be found and hasn't responded to any email for help I have been trying to research and figure this out my self but I am lost at this point.
you want a function, in your code, that lets the user build a file?

I would do it with a menu, if that is what you want.
ask the user what they want to do (process existing file, make new file) and if they choose make new file, you will start asking questions and getting answers..
how many questions? (read it in, and store it so you can loop)
for each question..
is it TF or MC?
... if MC how many... etc
just prompt, get input, and write the values to the file.

It isn't much different from what you have now. Open the file the same way, write to it with << instead of >>.

prompt and respond look like

cout <<"what do you want to do << endl;
cin >> response;
... handle response ... etc

give it a try. You've done a great job already if self taught, but if you get stuck, ask a specific question and youll get help here.
Last edited on
Thanks for your response. I agree a menu would work nice in this situation. My problem is I don't know how to do it. Like I said my teacher has not taught us how to do basically anything. I am kind of piecing stuff together hoping it works. The first thing I need to do is create a function let's say I call it menu. Within the menu I will ask the user what he or she want to do, options being create a new .txt file or open an existing one. That part I can handle. After that I'm not sure how to go about implementing it. Maybe something like this? Although I keep getting an exception thrown saying invalid argument at memory location xxxxxxxxx when I select e or E from the menu. In the xthrow.cpp it says // report an invalid_argument error but I think I can figure this out later.

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
#include <iostream>
#include <string>
#include <fstream>




class Quiz
{

private:

	int score;


	int numberOfQuestions;
	int numberOfAnswers;
	std::string questionType;
	int questionValue;
	std::string question;
	std::string mcAnswers[10];
	std::string tfAnswer;

	std::fstream quizData;

	bool openQuiz(std::string);
	void closeQuiz();
	void doQuestions();

public:
	Quiz();
	bool Run(std::string);

};

Quiz::Quiz()
{
	score = 0;
}


bool Quiz::openQuiz(std::string Test)
{
	quizData.open(Test, std::ios::in);
	if (quizData.fail())
		return false;
	else
		return true;

}


void Quiz::closeQuiz()
{
	quizData.close();
}

void Quiz::doQuestions()
{
	std::string numberOfQuestionsData;
	std::string questionValueData;
	std::string numberOfAnswersData;
	std::string mcData;



	getline(quizData, numberOfQuestionsData);
	numberOfQuestions = stoi(numberOfQuestionsData);
	for (int i = 0; i < numberOfQuestions; i++) {
		getline(quizData, questionType, ' ');
		if (questionType == "TF") {
			getline(quizData, questionValueData);
			questionValue = stoi(questionValueData);
			getline(quizData, question);
			std::cout << "Question #" << i + 1 << ": True of False: " << question << " Points value = " << questionValue << std::endl;
			getline(quizData, tfAnswer);
			std::cout << tfAnswer << std::endl;
		}
		if (questionType == "MC") {
			getline(quizData, questionValueData);
			questionValue = stoi(questionValueData);
			getline(quizData, question);
			std::cout << "Question #" << i + 1 << ": Multiple Choice: " << question << " Points value = " << questionValue << std::endl;
			getline(quizData, numberOfAnswersData);
			numberOfAnswers = stoi(numberOfAnswersData);
			for (int k = 0; k < numberOfAnswers; k++) {
				getline(quizData, mcAnswers[k]);
				std::cout << mcAnswers[k] << std::endl;
			}
			getline(quizData, tfAnswer);
			std::cout << tfAnswer << std::endl;
		}

	}
	if (questionType != "MC", "TF") {
		std::cerr << "Either there has been an error! Or this is the end of the file. Check the file to be sure." << std::endl;
	}


}

bool Quiz::Run(std::string Test)
{
	if (openQuiz(Test)) {
		doQuestions();
		closeQuiz();
	}
	else
		return false;
	return true;
}

void menu()
{
	char selection = ' ';
	do
	{

		std::cout << "Please select an option from the list below." << std::endl;
		//display the menu
		std::cout << "Menu";
		std::cout << "======" << std::endl;
		std::cout << "1 - Create new file" << std::endl;
		std::cout << "2 - Open existing file" << std::endl;
		std::cout << "E - Press E to exit" << std::endl << std::endl;
		std::cout << "Enter selection: ";
		// read user selection
		std::cin >> selection;


		switch (selection)
		{
		case '1':
			std::cout << "You selected - Create new file" << std::endl;
			//something such as call to another function that creates a new file?
			//this is where i am lost 
			break;

		case '2':
			std::cout << "You selected - Open existing file" << std::endl;
			//something such as call to another function that opens an existing file?
			//this is where i am lost
			break;

		case 'E':
		case 'e':
			std::cout << "Thank you." << std::endl;
			break;

			//if  1, 2, 3 or E is not selected then go to the default case
		default: std::cout << "Invalid selection. Please try again";
			// no break in the default case
		}
		std::cout << std::endl << std::endl;
	}

	while (selection != 'E' && selection != 'e');

	std::cout << "Thank you" << std::endl;


}
int main(int argc, char* argv[])
{
	menu();
	Quiz Game;

	if (!Game.Run("Test.txt"))
		std::cerr << "Failed to run due to missing quiz data.";

	std::cin.ignore();
	return 0;
}
I figured out the problem with the invalid argument exception that was thrown so now this is what i have
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
#include <iostream>
#include <string>
#include <fstream>



class Quiz
{

private:

	int score;


	int numberOfQuestions;
	int numberOfAnswers;
	std::string questionType;
	int questionValue;
	std::string question;
	std::string mcAnswers[10];
	std::string tfAnswer;

	std::fstream quizData;

	bool openQuiz(std::string);
	void closeQuiz();
	void doQuestions();

public:
	Quiz();
	bool Run(std::string);

};

Quiz::Quiz()
{
	score = 0;
}


bool Quiz::openQuiz(std::string Test)
{
	quizData.open(Test, std::ios::in);
	if (quizData.fail())
		return false;
	else
		return true;

}


void Quiz::closeQuiz()
{
	quizData.close();
}

void Quiz::doQuestions()
{
	std::string numberOfQuestionsData;
	std::string questionValueData;
	std::string numberOfAnswersData;
	std::string mcData;



	getline(quizData, numberOfQuestionsData);
	numberOfQuestions = stoi(numberOfQuestionsData);
	for (int i = 0; i < numberOfQuestions; i++) {
		getline(quizData, questionType, ' ');
		if (questionType == "TF") {
			getline(quizData, questionValueData);
			questionValue = stoi(questionValueData);
			getline(quizData, question);
			std::cout << "Question #" << i + 1 << ": True of False: " << question << " Points value = " << questionValue << std::endl;
			getline(quizData, tfAnswer);
			std::cout << tfAnswer << std::endl;
		}
		if (questionType == "MC") {
			getline(quizData, questionValueData);
			questionValue = stoi(questionValueData);
			getline(quizData, question);
			std::cout << "Question #" << i + 1 << ": Multiple Choice: " << question << " Points value = " << questionValue << std::endl;
			getline(quizData, numberOfAnswersData);
			numberOfAnswers = stoi(numberOfAnswersData);
			for (int k = 0; k < numberOfAnswers; k++) {
				getline(quizData, mcAnswers[k]);
				std::cout << mcAnswers[k] << std::endl;
			}
			getline(quizData, tfAnswer);
			std::cout << tfAnswer << std::endl;
		}

	}
	if (questionType != "MC", "TF") {
		std::cerr << "Either there has been an error! Or this is the end of the file. Check the file to be sure." << std::endl;
	}
	

}

bool Quiz::Run(std::string Test)
{
	if (openQuiz(Test)) {
		doQuestions();
		closeQuiz();
	}
	else
		return false;
	return true;
}
void menu()
{
char selection = 'a';
do
{

std::cout << "Please select an option from the list below." << std::endl;
//display the menu
std::cout << "Menu";
std::cout << "======" << std::endl;
std::cout << "1 - Create new file" << std::endl;
std::cout << "2 - Open existing file" << std::endl;
std::cout << "E - Press E to exit" << std::endl << std::endl;
std::cout << "Enter selection: ";
// read user selection
std::cin >> selection;


switch (selection)
{
case '1':
std::cout << "You selected - Create new file" << std::endl;
//something such as call to another function that creates a new file?
//this is where i am lost
break;

case '2':
std::cout << "You selected - Open existing file" << std::endl;
//something such as call to another function that opens an existing file?
//this is where i am lost
break;

case 'E':
case 'e':
std::cout << "Thank you." << std::endl;
break;

//if  1, 2, 3 or E is not selected then go to the default case
default: std::cout << "Invalid selection. Please try again";
// no break in the default case
}
std::cout << std::endl << std::endl;
}

while (selection != 'E' && selection != 'e');

std::cout << "Thank you" << std::endl;


}


int main(int argc, char* argv[])
{
	menu();
	Quiz Game;

	if (!Game.Run("Test.txt"))
		std::cerr << "Failed to run due to missing quiz data.";

	std::cin.ignore();
	std::cin.get();
	return 0;
}
Could any one show me a simple example of how to create a .txt file that doesn't already exist and then how to save it after the user has entered in some text so that it doesn't get deleted at the end of the function?
Could any one show me a simple example of how to create a .txt file that doesn't already exist and then how to save it after the user has entered in some text so that it doesn't get deleted at the end of the function?
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <fstream>
#include <string>

int main()
{
    std::string a = "Hello World \n";
    std::ofstream fout {"F:\\test12.txt"};//new file, doesn't yet exist
    fout << a;//compile and run program, then check F directory!!!
}
Last edited on
Thanks that helped me figure it out.
Topic archived. No new replies allowed.