program to read in various types of test questions

Assignment instructions: Write a C++ program to read in various types of test questions (multiple choice and True/False) from a test bank (text file), and load the questions into an array of questions.

The test bank (text file) will have the following format:

Line 1 will be an integer value, indicating how many questions in the file
Each question will start with a line that starts with either "MC" or "TF" followed by a space and then the point value of the question.
The next line will be the actual question.
If the question was True/False, the following line will be the answer, either "True" or "False"
If the question was multiple choice, the following line will be an integer value indicating how many choices will be provided for that question. The following lines will be the choices. There will never be more than 6 choices. The final line following the choices will be the answer: "A" or "B" or "C" or "D" or "E" or "F".




BELOW IS THE CODE I CURRENTLY HAVE

#include <iostream>
#include <string>
#include <fstream>

class CQuiz
{

private:
int score;

int numQuestions;
int numAnswers;
std::string questionType;
int questionValue;
std::string question;
std::string mcAnswers[10];
std::string answer;

// file stream
std::fstream quizData;

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

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

};

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


bool CQuiz::openQuiz(std::string filename)
{
quizData.open(filename, std::ios::in);
if (quizData.fail())
return false;
return true;
}

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

void CQuiz::doQuestions()
{


Question* myQuestions[10];
string questiontype, questiontxt;
string answertxt, optiontxt;
int numquestions, questionvalue;

quizData.open("Test.txt");
if (quizData.is_open())
{
outfile << "3\n";
outfile << "TF 5\n";
outfile << "There exists birds that cannot fly?\n";
outfile << "true\n";
outfile << "MC 10\n";
outfile << "Who was the President of the USA in 1991?\n";
outfile << "6\n";
outfile << "Richard Nixon\n";
outfile << "Gerald Ford\n";
outfile << "Jimmy Carter\n";
outfile << "Ronald Reagan\n";
outfile << "George Bush Sr.\n";
outfile << "Bill Clinton\n";
outfile << "E\n";
outfile << "TF 10\n";
outfile << "The city of Boston hosted the 2004 Summer Olympics?\n";
outfile << "false\n";
outfile.close();


std::string data; // temp storage.


getline(quizData, data);
numQuestions = stoi(data);



for (int i = 1; i <= numQuestions; i++) {

getline(quizData, questionType, ' ');
getline(quizData, data);
questionValue = stoi(data);
getline(quizData, question);

if (questionType == "TF") {

getline(quizData, answer);
}
else
if (questionType == "MC")
{

getline(quizData, data);
numAnswers = stoi(data);
for (int i = 0; i < numAnswers; i++)
getline(quizData, mcAnswers[i]);
getline(quizData, answer);
}


}



}

bool CQuiz::Run(std::string filename);
{
if (openQuiz(filename)) {
doQuestions();
closeQuiz();
}
else
return false; // cant open file.
return true;
}


int main()
{
CQuiz Game;

if (!Game.Run("Test.txt"))
std::cout << "Thanks for playing";

return 0;
}
And so you posted your assignment here hoping someone could do your homework for you instead of asking a concrete question?
I would like to know if i am on the right track. I wanted the to be created, i could have paid someone to create this. I am trying to do it in a simple way that is not complex.
Last edited on
Yes, you are on the right track. Has your question been answered?
I'm a beginer too, but i've been working with a similar problem, just for funn.
Of course not all the homework is done, but it could give you an idea.
You have some work to do!.

The routine Dsp is a bonus. You can keep the responses as in a log.

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
// * Asking for responses
#include    <sstream>        // ios; is
#include    <iostream>	    // cin, cout
#include    <fstream>	    // basic_ifstream  ifstream, ofstream,fstream,filebuf
using namespace std;

int			dst= 1;		// destination for the log 
ofstream 	DsF;	 	//   ofstream addFile(outName); 
string 		dspName="E:/Displays.txt";	
ostringstream  	OuB;

	// Creates a file with log of activity *******************************************************
void Dsp(	int dest,     //  0-none, 1-cout, 2-cout and file, 3-just file 
		ofstream & DsF, 
		ostringstream & bufi, 
		char bott='\n') {
		string 	me ("Dsp: ");
	if (dest != 0) {
		if(dest <  3)    cout <<  me << bufi.str() ; // << bott;  1,2 van a cout
		if(dest != 1)     DsF <<  me << bufi.str() ; // << bott;  2,3 van a Archivo
	bufi.str(""); bufi.clear();
	}
}
	
	// Receive a group of strings with "n" posible responces i.e 
	// first of all valiates those.  you can skip the validation
	// the procedure returns a number, wich is the position of the response (0 to n-1, but also the response)
int	getOptInt	  (int numRspons, string strRspons[], string& str_rE) {
	string  	me="getOptInt: ";  								// identifier for the routine
	int 		j, large=0, fin=0, icnt, irsp = numRspons;
	string		guess, str_ax;
	unsigned char   chr_rE;
	
	// validating the responses	in the strRspons array. 
	if ( irsp <= 0 ) { 				// at lease one response
		OuB<< me << "Parameter (num of responses) invalid-"<< irsp <<"<-"<<endl; 	 Dsp(dst, DsF, OuB);
		exit (21);
	}
	
	for( j=0; j < irsp; j++ ) {  // rescue responses
		if( ( icnt = strRspons[j].size() ) == 0 ) {		// look for the real number of responses
			irsp = j;
			break;			
		}
		else {
			if( large < (icnt = (strRspons[j].size())-1) ) large = strRspons[j].size() ;  // maxsize of response
		//			each character in strRspons, should by aplphanum 
			for ( ; icnt >= 0; icnt--)  { 
			    str_ax = strRspons[j].substr(icnt,1);
				chr_rE = (unsigned int)   str_ax[0];
				if( (chr_rE < 48 || chr_rE > 122 ) ||   // non printable   verify the ascii chart
				    (chr_rE < 65 && chr_rE >  57 ) ||   // is not a number 
					(chr_rE < 97 && chr_rE >  90 ) )    // is not a letter
				{
					OuB<< me << "Invalid content - Position:"<< j<<", cont->"<< str_ax<<"<-"<<endl;  Dsp(dst, DsF, OuB);
					exit (21);
				}
			}
		//			Maybe its necesary to validate no repetead answers.  ...			
		}
	}
	// Any way you make simplier
	
	
	OuB<< me <<" Please, select you response from the list: \n";
	for( j = 0; j < irsp ; j++) {
		OuB<< " "<<strRspons[j] <<",";      // << strRspons[j].size() ;
	}
	OuB<< endl;					Dsp(dst, DsF, OuB);
	OuB<< me <<  "     R_";		Dsp(dst, DsF, OuB);
	
	icnt = 0;
	while (fin != 1) {						// just trying a way of response
		guess.clear();
		do {	// chr by char
		// ***********************************************		
			chr_rE = cin.get();
			if (chr_rE != '\n' ) guess += chr_rE;
		}	while ( chr_rE != '\n');
		// ***********************************************
		if( guess.size() == 0) {
			OuB<< me <<"Error: min length is 1 char"<<endl;  Dsp(dst, DsF, OuB);
			OuB<<"Response     R_";		      Dsp(dst, DsF, OuB);
			guess.clear();			
		} 
		else
		if( guess.size() > large) 
		{
			OuB<< me <<"Error: max lenth response is "<<large<<endl;  Dsp(dst, DsF, OuB);
			OuB<<"Response     R_";		      Dsp(dst, DsF, OuB);
			guess.clear();
		}
		else
		{
			for( j=0; j<irsp; j++ ) {
				if( strRspons[j] == guess ) {
					icnt = j + fin++;
					str_rE = strRspons[j];
					break;			
				}
			}
			if ( fin == 0) {
				OuB<< me <<"Option is not a valid selection: "<<guess<<endl;   	Dsp(dst, DsF, OuB);
				OuB<<"Response     R_";			Dsp(dst, DsF, OuB);
				guess.clear();
			}
		}
	}	

	OuB <<endl;					Dsp(dst, DsF, OuB);
	return icnt;
}
	
int main() {
	string  me="main: ";
	string	str_rE;
	int 	numRspons = 15, iSel;
	
	string	strRspons[10] {"A","B","C","D","F","G","None","All","DontKnow"};
	OuB<< me <<"Selection: "<<(iSel= getOptInt (numRspons, strRspons, str_rE))<<", Option: "<<str_rE<<endl;
	Dsp(dst, DsF, OuB);
	
	
	system("pause");
	return 0;
}


Good look.
load the questions into an array of questions.

You need a class for an individual Question. Your CQuiz class seems to be unsure if it's a quiz or an individual question.

Also, I have to believe that there's more to the assignment than simply storing questions in an array. Do you need to give the quiz to a user? Display the score? Display the correct answers? Store student scores in a file? Depending on what else you mus do, there may be more.

It's possible that you need 3 classes related to questions, a base Question class, and derived classes for true/false and multiple choice questions (TFQuestion and MCQuestion?) The classes might have virtual methods to read the question, display the question, and get an answer from the user. Or you could just create one class a true/false question as a degenerate multiple choice question, where T/F questions always have two choices: "True" and "False"
Topic archived. No new replies allowed.