2 Replies - 4710 Views - Last Post: 19 October 2012 - 06:03 PM Rate Topic: -----

#1 ZenithMkII  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 49
  • Joined: 06-September 12

Multiple Choice Program Random Questions and Answers

Posted 19 October 2012 - 04:41 PM

So I've been working on this program for about 3 hours for my little brother to use for memorizing his keywords. I'm having an error where it says I am going out of range with a vector. I know where the error is but can't seem to find how to solve it. I can also build it correctly. It must be a runtime error, don't know.

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
//etc

The 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


Is This A Good Question/Topic? 0
  • +

Replies To: Multiple Choice Program Random Questions and Answers

#2 ZenithMkII  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 49
  • Joined: 06-September 12

Re: Multiple Choice Program Random Questions and Answers

Posted 19 October 2012 - 05:40 PM

Well I found out why it didn't work. The 3 random generated answers did not have a limit and went out of bounds.
//Then randomly gets 3 other answers from questionList
while(randomAnswer.size() < 4){
	int random = rand();
	if(random != i){
		randomAnswer.push_back(questionList[rand() % (questionList.size() - 1)]);
	}
}

Note the "% (questionList.size() - 1)"
Was This Post Helpful? 1
  • +
  • -

#3 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1432
  • View blog
  • Posts: 4,968
  • Joined: 19-February 09

Re: Multiple Choice Program Random Questions and Answers

Posted 19 October 2012 - 06:03 PM

Very good, but you'll want to check it is not the current question.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1