The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | Jul 2017 |
| Last Sign in: | 305 Weeks Ago, 1 Day Ago |
| Questions Answered: | 15833 |
| Tutorials Posted: | 15827 |
MBA,PHD, Juris Doctor
Strayer,Devery,Harvard University
Mar-1995 - Mar-2002
Manager Planning
WalMart
Mar-2001 - Feb-2009
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
using namespace std;
Â
class Question
{
public:
 string getQuestion()// Get the question
 {
  return question;
 }
 virtual int getValue() // Get point value of question
 {
  return value;
 }
 virtual string getQuestionType()// gets the type of question
 {
  return questiontype;
 }
 virtual void setQuestion(string answer, int value)
 {
 }
 virtual void setNewQuestion(string answer, int value)
 {
 }
 virtual void printOptions()
 {
 }
 virtual string getAnswer()
 {
  return answer;
 }
private:
 string question, answer;
 int value;
 string questiontype;
};
Â
// True/False Questions
class QuestionTF : public Question
{
public:
 void setQuestion(string theQuestion, int pointValue)
 {
  string theAnswer;
  questiontype = "TF";
  question = theQuestion;
  value = pointValue;
  options = "true/false";
  getline(cin, theAnswer);
  answer = theAnswer;
 }
 void setNewQuestion(string theQuestion, int pointValue)
 {
  string theAnswer;
  questiontype = "TF";
  question = theQuestion;
  value = pointValue;
  options = "true/false";
  cout << "Enter answer true/falsen";
  getline(cin, theAnswer);
  answer = theAnswer;
 }
 int getValue() // Value for test question
 {
  return value;
 }
 string getQuestionType()// Type of question
 {
  return questiontype;
 }
 void printOptions()
 {
  cout << question << endl;
  cout << answer << endl;
 }
 string getAnswer()
 {
  return answer;
 }
private:
 string question, questiontype;
 string answer;
 string options;
 int value;
};
// Multiple Choice Questions
class QuestionMC : public Question
{
public:
 void setQuestion(string theQuestion, int pointValue)
 {
  string line;
  questiontype = "MC";
  getline(cin, line);
  numberOfOptions = atoi(line.c_str());
  question = theQuestion;
  value = pointValue;
  for (int count = 0; count<numberOfOptions; count++) {
   getline(cin, line);
   options[count] = line;
  }
  getline(cin, line);
  answer = line;
 }
 void setNewQuestion(string theQuestion, int pointValue)
 {
  string line;
  questiontype = "MC";
  cout << "Enter the number of choices: ";
  getline(cin, line);
  numberOfOptions = atoi(line.c_str());
  question = theQuestion;
  value = pointValue;
  for (int count = 0; count<numberOfOptions; count++) {
   cout << "nEnter next option: ";
   getline(cin, line);
   options[count] = line;
  }
  cout << "nEnter Answer: ";
  getline(cin, line);
  answer = line;
 }
 void printOptions()
 {
  char first = 'A';
  cout << question << endl;
  for (int count = 0; count<numberOfOptions; count++) {
   cout << first++ << ". " << options[count] << "n";
  }
  cout << answer << "n";
 }
 int getValue() // Get the point value of the question
 {
  return value;
 }
 string getQuestionType()// Get the type of question
 {
  return questiontype;
 }
 string getAnswer()
 {
  return answer;
 }
private:
 int numberOfOptions;
 string question, answer;
 string options[6];
 string questiontype;
 int value;
};
Â
//Â Function prototypes for the functions supporting the main program functionality
int loadArray(Question *myQuestions[]);
int addQuestion(Question *myQuestions[], int numquestions);
void printQuizQuestions(Question *myQuestions[], int numquestions);
void writeExamQuestionFile(Question *myQuestions[], int numquestions);
Â
int main() {
 Question *myQuestions[10];
 int numquestions;
Â
 // Opening the testbank file and processing as a question of each type
 try {
  ifstream infile("testbank2.txt");
  streambuf *cinbuf = cin.rdbuf();Â
  cin.rdbuf(infile.rdbuf());     Â
  string line, theQuestion, theAnswer;
  numquestions = loadArray(myQuestions);
  cin.rdbuf(cinbuf);
 }
 catch (exception& e) {
  cout << "Exception occured";
  cout << "Something went wrong with reading the infile like: " << e.what() << ". Check the contents of testbank2.txt";
 }
 numquestions = addQuestion(myQuestions, numquestions);
 printQuizQuestions(myQuestions, numquestions);
 cout << "Write to textbank File";
 try {
  ofstream outfile("textbank.txt");
  streambuf *coutbuf = std::cout.rdbuf();
  cout.rdbuf(outfile.rdbuf());          Â
  writeExamQuestionFile(myQuestions, numquestions);
  cout.rdbuf(coutbuf);
  cout << "Text Bank File Written.n" << "n";
Â
 }
 catch (exception& e) {
  cout << "Exception occurred";
 }
 getchar();
 return 0;
}
Â
// Function to load the array of Questions from the input file
int loadArray(Question *myQuestions[])
{
 string line;
 string questiontype, theQuestion;
 int numquestions, questionvalue;
 // Get the number of questions from the first line in the file
 getline(cin, line);
 numquestions = atoi(line.c_str());
 for (int count = 0; count<numquestions; count++) {
  getline(cin, line);
Â
  int npos = line.size();
  int prev_pos = 0;
  int pos = 0;
  while (line[pos] != ' ')
   pos++;
  questiontype = line.substr(prev_pos, pos - prev_pos);
  prev_pos = ++pos;
  questionvalue = atoi(line.substr(prev_pos, npos - prev_pos).c_str());
Â
  if (questiontype == "TF")
   {
    myQuestions[count] = new QuestionTF;
    cout << " nEnter the Question: ";
    getline(cin, theQuestion);
    myQuestions[count]->setNewQuestion(theQuestion, questionvalue);
   }
  if (questiontype == "MC")
   {
    myQuestions[count] = new QuestionMC;
    cout << " nEnter the Question: ";
    getline(cin, theQuestion);
    myQuestions[count]->setNewQuestion(theQuestion, questionvalue);
   }
Â
 }
 return numquestions;
}
Â
// Function to add questions by getting from user and using the interactive methods added to the classes.
int addQuestion(Question *myQuestions[], int numquestions)
{
 int questionvalue;
 int count = numquestions;
 string theQuestion, line, questiontype;
 cout << "Enter the question type and valuen";
 cout << "The question types are MC and TFn";
 getline(cin, line);
Â
 // Get the next line with the question type and the value of the question
 int npos = line.size();
 int prev_pos = 0;
 int pos = 0;
 while (line[pos] != ' ')
  pos++;
 questiontype = line.substr(prev_pos, pos - prev_pos);
 prev_pos = ++pos;
 questionvalue = atoi(line.substr(prev_pos, npos - prev_pos).c_str()); // Last word
 if (questiontype == "TF")
 {
   myQuestions[count] = new QuestionTF;
   cout << " nEnter the Question: ";
   getline(cin, theQuestion);
   myQuestions[count]->setNewQuestion(theQuestion, questionvalue);
  }
 if (questiontype == "MC")
  {
   myQuestions[count] = new QuestionMC;
   cout << " nEnter the Question: ";
   getline(cin, theQuestion);
   myQuestions[count]->setNewQuestion(theQuestion, questionvalue);
  }Â
Â
 return ++numquestions;
}
void printQuizQuestions(Question *myQuestions[], int numquestions)
{
 for (int count = 0; count<numquestions; count++)
 {
  myQuestions[count]->printOptions();
  cout << "n";
 }
}
Â
// Function to write to the file in the format needed to read later when loading if needed.
void writeExamQuestionFile(Question *myQuestions[], int numquestions)
{
 int count;
 string qtype;
 cout << numquestions << "n";
 for (count = 0; count<numquestions; count++) {
  qtype = myQuestions[count]->getQuestionType();
  cout << qtype << " " << myQuestions[count]->getValue() << "n";
  myQuestions[count]->printOptions();
 }
}Â Â
HI! I am getting an error when I type the answer in my C++ Program. Need help figuring out what the problem is.
----------- Â ----------- H-----------ell-----------o S-----------ir/-----------Mad-----------am ----------- Th-----------ank----------- yo-----------u f-----------or -----------you-----------r i-----------nte-----------res-----------t a-----------nd -----------buy-----------ing----------- my----------- po-----------ste-----------d s-----------olu-----------tio-----------n. -----------Ple-----------ase----------- pi-----------ng -----------me -----------on -----------cha-----------t I----------- am----------- on-----------lin-----------e o-----------r i-----------nbo-----------x m-----------e a----------- me-----------ssa-----------ge -----------I w-----------ill----------- be----------- qu-----------ick-----------ly