Description: This program reads a set of questions and answers from a text file. Then, it shuffles them and prints out the question, one right answer, and three random answers. It puts the question and answers in multiple choice form.
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
#include <cstdlib>
#include <time.h>
#include "test.h"
using namespace std;
int main(){
string question;
string answer;
char response;
vector <Test> questionList;
//Seed time
srand(time(0));
//Open file
fstream file("test.txt", ios::in);
//Error checking to see if file exists
if(!file){
cout << "Could not read file.";
exit(1);
}
//Read from file until the end of file
//Also, places the information into questionList vector
while(!file.eof()){
getline(file, question);
getline(file, answer);
Test temp(question, answer);
questionList.push_back(temp);
}
//Randomizes the questionList vector
random_shuffle(questionList.begin(), questionList.end());
//Goes through every Test question
for(int i = 0; i < questionList.size(); i++){
vector <Test> randomAnswer;
//Puts the correct Answer into it first
randomAnswer.push_back(questionList[i]);
//Then randomly gets 3 other answers from questionList
/*---------------------Problem Here--------------------------
while(randomAnswer.size() < 4){
int random = rand();
if(random != i){
randomAnswer.push_back(questionList[rand()]);
}
}
-----------------------Problem Here------------------------*/
//Shuffle the answers
random_shuffle(randomAnswer.begin(), randomAnswer.end());
//Print the question
cout << "Definition of " << questionList[i].getQuestion() << ":" << endl;
//Initialize the first choice character to 'A'
char ch = 'A';
//Prints the shuffled answers
for(int j = 0; j < randomAnswer.size(); j++){
cout << ch << ") " << randomAnswer[j].getAnswer() << endl;
//Increment 'A' so it can print 'B' and so forth
++ch;
}
//Get users response
cout << "\nYour answer: ";
cin >> response;
//Bool data type to determine if the correct answer was found
bool isCorrect = false;
switch(toupper(response)){
case 'A':
if(randomAnswer[0].getAnswer() == questionList[i].getAnswer())
isCorrect = true;
break;
case 'B':
if(randomAnswer[1].getAnswer() == questionList[i].getAnswer())
isCorrect = true;
break;
case 'C':
if(randomAnswer[2].getAnswer() == questionList[i].getAnswer())
isCorrect = true;
break;
case 'D':
if(randomAnswer[3].getAnswer() == questionList[i].getAnswer())
isCorrect = true;
break;
default:
cout << "\nIncorrect input.\n";
}
//If the answer was found print "Correct" else "Wrong"
if(isCorrect){
cout << "\nYou got the answer correct!\n";
}
else{
cout << "\nYou got the answer WRONG!\n"
<< "Correct answer was " << questionList[i].getAnswer() << endl;
}
}
}
test.h
#ifndef TEST_H
#define TEST_H
#include <iostream>
using namespace std;
//Test class definition
class Test{
private:
string question;
string answer;
public:
Test(){
question = "";
answer = "";
}
Test(string q, string a){
question = q;
answer = a;
}
string getQuestion() const;
string getAnswer()const;
void setQuestion(string);
void setAnswer(string);
};
#endif
test.cpp
#include "test.h"
string Test::getQuestion() const{
return question;
}
string Test::getAnswer() const{
return answer;
}
void Test::setQuestion(string q){
question = q;
}
void Test::setAnswer(string a){
answer = a;
}
The test.txt should look like this:Q1 A1 Q2 A2 Q3 A3 Q4 A4 //etcThe rest of the code is just a class called Test. It contains set and get methods for questions and answers.
This post has been edited by ZenithMkII: 19 October 2012 - 04:47 PM

New Topic/Question
Reply



MultiQuote




|