File I/0 function help

Hello, I am trying to take this step by step I code my True/False function complete but I am getting an error and need help figuring it out. I still need to do the Multiple Choice function as well but for now, need to get the True/False figured out.

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

//#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class TestBank
{
public:
	void TrueFalse(ofstream&);
	void MultipleChoice(ofstream&);
};
int main()
{
	TestBank bankObj;
	ofstream test;
	int testNumber, typeOfQuestion;
	char yesOrNo;
	// Create test bank file.
	test.open("C:\\temp\\testBankFile.txt");
	if (test.is_open())
	{
		cout << "How many questions would you like to create? ";
		cin >> testNumber;
		test << testNumber << endl;
		int nextNum = 1;
		//create the test bank
		for (int i = testNumber; i > 0; --i)
		{
			cout << endl << "What type of question will #" << nextNum++
				<< " be? 1=T/F or 2=multiple choice: ";
			cin >> typeOfQuestion;
			switch (typeOfQuestion)
			{
			case 1: bankObj.TrueFalse(test);
				break;
			case 2: bankObj.MultipleChoice(test);
			}
		}
		test.close();
	}
	else
	{
		cout << "Unable to create test bank";
	}
	system("pause");
	return 0;
}
// function to create true/false question
void TestBank::TrueFalse(ofstream& thefile)
{
	int points;
	string question;
	char answer, yn;
	do
	{
		cout << "\nHow many points does this question worth? Enter: ";
		cin >> points;
		cin.ignore();
		cout << "\nPlease enter the question: ";
		getline(cin, question);
		cout << "\nPlease enter the answer True or False ?";
		cin >> answer;
		cin.ignore();
		cout << "Would like to make any correction <y/n>? ";
		cin >> yn;
	} while (yn == 'y');
	//Write all information into the file
	thefile << points << endl;
	thefile << question << endl;
	thefile << answer << endl;
	//Complete the remaining code here
}
//Now you have to complete function MultipleChoice()
// function to create multiple choice question
void TestBank::MultipleChoice(ofstream& theFile)
{


}	


Error:



Warning C4101 'yesOrNo': unreferenced local variable line 17

Error LNK1104 cannot open file 'C:\Users\ACEis\source\repos\CS215_Joseph_Hoffman_IP3\Debug\CS215_Joseph_Hoffman_IP3.exe' CS215_Joseph_Hoffman_IP3 C:\Users\ACEis\source\repos\CS215_Joseph_Hoffman_IP3\LINK line 1



Please assist me with these issues.
So if I comment out three lines of code in the while loop it works but it still is not right.


These three lines after the while to store the inputs into the 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
void TestBank::TrueFalse(ofstream& thefile)
{
	int points;
	string question;
	char answer, yn;
	do
	{
		cout << "\nHow many points does this question worth? Enter: ";
		cin >> points;
		cin.ignore();
		cout << "\nPlease enter the question: ";
		getline(cin, question);
		cout << "\nPlease enter the answer True or False ?";
		cin >> answer;
		cin.ignore();
		cout << "Would like to make any correction <y/n>? ";
		cin >> yn;
	} while (yn == 'y');
	//Write all information into the file (commented out for testing)

	//thefile << points << endl;
	//thefile << question << endl;
	//thefile << answer << endl;

	//Complete the remaining code here
}


The program asks the following in order for the true/false section:

*How many points does this question worth? Enter:
*Please enter the question:
*Please enter the answer True or False?
*Would like to make any correction <y/n>?



If 'y' is selected it should loop back to the beginning to retry if 'n' the program should write all the inputs to the file.


how do I properly do this?
I fixed my code. The thing I need help with now is when the program asks the user to hit 1 for TF (T/F) or 2 for MC (Multiple choice) when you hit 1 or 2 it stores 1 or 2 in the file I need it to store TF if you hit 1 or MC if you hit 2 in the file. How do I do this please help?

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
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class TestBank
{
public:
	void TrueFalse(ofstream&);
	void MultipleChoice(ofstream&);
};
int main()
{
	TestBank bankObj;
	ofstream test;
	int testNumber, typeOfQuestion;
	char yesOrNo;
	// Create test bank file.
	test.open("C:\\temp\\testBankFile.txt");
	if (test.is_open())
	{
		cout << "How many questions would you like to create? ";
		cin >> testNumber;
		test << testNumber << endl;
		int nextNum = 1;
		//create the test bank
		for (int i = testNumber; i > 0; --i)
		{
			cout << endl << "What type of question will #" << nextNum++
				<< " be? 1=T/F or 2=multiple choice: ";
			cin >> typeOfQuestion;
			test << typeOfQuestion << endl;
			switch (typeOfQuestion)
			{
			case 1: bankObj.TrueFalse(test);
				break;
			case 2: bankObj.MultipleChoice(test);
			}
		}
		test.close();
	}
	else
	{
		cout << "Unable to create test bank";
	}
	system("pause");
	return 0;
}
// function to create true/false question
void TestBank::TrueFalse(ofstream& thefile)
{
	int points;
	string question;
	string answer;
	char yn;
	do
	{
		cout << "\nHow many points does this question worth? Enter: ";
		cin >> points;
		cin.ignore();
		cout << "\nPlease enter the question: ";
		getline(cin, question);
		cout << "\nPlease enter the answer True or False ?";
		cin >> answer;
		cin.ignore();
		cout << "Would like to make any correction <y/n>? ";
		cin >> yn;
	} while (yn == 'y');

	//Write all information into the file
	thefile << points << endl;
	thefile << question << endl;
	thefile << answer << endl;
}
//Now you have to complete function MultipleChoice()
// function to create multiple choice question
void TestBank::MultipleChoice(ofstream& theFile)
{


}


The format stored in the file should look like this:

Test bank file format should look like the following example:

3
TF 5
Do pigs fly?
False
TF 2
Is programming important?
True


Currently, it looks like this:

3
1
5
Do pigs fly?
False
1
2
Is programming important?
True


Can you please explain how to fix this?
Last edited on
After line 31, do a simple check like this:

1
2
3
4
if (typeOfQuestion == 1)
	test << "TF " << "\n";
else
	test << "MC " << "\n";


instead of

 
test << typeOfQuestion << endl;


This'll store TF if the user chose 1 or MC otherwise.
Topic archived. No new replies allowed.