Classes and object arrays organizing a test questionnaire

My question pertains to a school assignment, and I want to emphasize that I am not looking for someone to just give me the answer. However, I am looking for direction so I can solve this problem.

I made a test program consisting of multiple choice and true/false questions. I placed this information in 2 .txt files. I'm organizing my code by class. I need to pull the information from the .txt file, output it to the screen, compare the user-input results with the test answers, and output a test score.

I know how to pull from a .txt file, but this has multiple choice and true-false questions that give string and char data. I considered creating arrays to store the questions and answers; however, I began to question whether there is a more efficient way to organize this data. It seems like I need way too many arrays, and perhaps 1 or 2 methods might suffice. I know how to calculate the test scores and do not need direction on that portion.

I'm developing this C++ code using the Microsoft Visual Studio IDE.

I've researched arrays, vectors, objects, classes, and many more topics that I thought may be related to this problem. I've read through my college textbook, listened to the lectures, watched YouTube videos, and consulted some programmers via Facebook. I've googled questions and read answers on various C++ topics and programming examples. The fact that I'm still confused leads me to believe perhaps I'm just not grasping these concepts like I should be.

Can anyone with more expertise point me in the right direction?



\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

Here is the code I've created thus far:

// Test.cpp : This file contains the 'main' function. Program execution begins and ends there

#include "pch.h"
#include "QuestionMC.h"
#include "QuestionTF.h"

#include "QuestionMC.cpp"
#include "QuestionTF.cpp"

#include "TestBankMC.txt"
#include "TestBankTF.txt"

#include <iostream>
#include <fstream>
#include <string>
#include <ctime>
#include <cmath>

using namespace std;

int main() {

string question, answer;
string q, a;
int v;
int value;

cout << "====================================================\n";
cout << "== ==\n";
cout << "== WELCOME TO THE FBI ENTRANCE EXAM! ==\n";
cout << "== ==\n";
cout << "====================================================\n";
cout << endl;
cout << endl;
cout << "You will be asked a series of 11 questions consisting of both
True/False questions and multiple choice questions.\n";

cout << "Please answer each question to the best of your ability\n\n\n" <<
"Good luck!\n\n\n" << endl;


//open file TestBankTF.txt
ifstream file("TestBankTF.txt");

// check for errors
if (infile.fail()) {
cerr << "Error Opening File" << endl;
exit(1);
}

if (file.is_open())
{
string myTestBankTF[12];
for (size_t i = 0; i < 12; i++){
{
file >> myTestBankTF[i];
}
}

// close TestBank.txt file
infile.close();

cout << "Please answer the following true or false questions with either
'true' or 'false': \n\n";
cin >> question;

string answersTF[3];

for (size_t i = 0; i < 12; i++)
{
if (myTestBankTF[12 == question)
{
cout << question << "\n\n";
cout << "Answer: " << endl;
cin >> answersTF[3];
}
else
{
cerr << question << "Error loading in myTestBankTF[].";
}
}

//open file TestBankMC.txt
ifstream file("TestBankMC.txt");

//check for errors
if (infile.fail())
{
cerr << "Error opening file" << endl;
exit(1);
}

if (file.is_open())
{
string myTestBankMC[71];

for (size_t i = 0; i < 71; i++)
{
string myTestBankMC[i];
}
}

// close TestBankMC.txt file
infile.close();

cout << "Please answer the following multiple choice questions with the
appropriate letter answer (A, B, C, D, or E): \n\n";
cin >> question;

string answersMC[11];
string TestBankMC[71];

for (size_t i = 0; i < 71; i++)
{
if (myTestBankMC[71] == question)
{
cout << question << "\n\n";
cout << "Answer: " << endl;
cin >> answersMC[11];
cin.ignore();
}
else
{
cerr << question << "Error loading in myTestBankTF[].";
}
}

return 0;

} // end of main function


\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
// header file is class Questions' class and function declarations

class Question
{
public:
Question();

~Question();

Question(string, int, string)

const std::string& getQuestion() const;

const std::string& getAnswer() const;

int getValue() const;

virtual std::string printOptions() const;


protected:
std::string question, answer;
int value;
};

#endif

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
// class Question's class and function definitions

Question::Question() {}

Question::~Question() {}

Question :: Question (std::string q, int v, std::string a)
: question(std::move(q)), answer(std::move(a)), value(v) {}

Question :: const std::string& getQuestion() const
{
return question;
}

Question :: const string& getAnswer() const
{
return answer;
}

Question :: int getValue() const
{
return value;
}


\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
// header file is class QuestionMC's class and function declarations

class QuestionMC:: public Question
{
public:
QuestionMC();

~QuestionMC();

QuestionMC(const std::string&, int, const std::string&)

void addOption(const std::string& option);

string printOptions() const override {};


private:
vector options;
static const size_t MaxOptions = 6;
};

#endif

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
// subclass QuestionMC's class and member functions definitions

QuestionMC::QuestionMC(){}

QuestionMC::~QuestionMC(){}

QuestionMC(const string& q, int v, const string& a)
: Question(q, v, a) {}

void QuestionMC::addOption(const string& option)
{
if (options.size() < MaxOptions)
options.push_back(option);
}

string QuestionMC::printOptions() const override
{
ostringstream oss;
char first = 'A';
for (auto& option : options)
oss << (first > 'A' ? "\n" : " ") << first++ << "." << option;
return oss.str();
}

\
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
// class and function declarations for subclass QuestionTF.h

class QuestionTF:public Question
{
{
public:
QuestionTF();

~QuestionTF();

QuestionTF(const std::string&, int, const std::string&)

std::string printOptions() const override {};
};

#endif

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
// Class and functions definitions for subclass QuestionTF of class Question

QuestionTF::QuestionTF(){}

QuestionTF::~QuestionTF(){}


QuestionTF::QuestionTF(const std::string& q, int v, const std::string& a)
: Question(q, v, a) {}

QuestionTF:: std::string printOptions() const override
{
return "True/False";
}
#include of a cpp file is generally a sign that you've misunderstood what #include does and how the compiler and linker work.


You have misunderstood the difference between creating an array, and then using it.
string TestBankMC[71]; This CREATES an array of 71 strings.

string TestBankMC[0] = "some value"; This sets the value of ONE of those string
cin >> answersMC[11];This is an attempt to store something in the 12th element of an array. Why do you try to store into an element that doesn't exist? Why do you try to do it over and over?

You need to stop, and go back and practice just creating an array, and then writing into the array. When you can do that, you are ready for more. You're trying to things without understanding the basics. Learn how to use an array first.



I removed the [#include .cpp] files. When I created my classes, I included the class declarations in a .h file, and the definitions in the .cpp file. Will my program still pull the class definitions?

Thank you for explaining the array. If I were to replace the index with i, would that sequentially store the user's answers in the array?

[// Test.cpp : This file contains the 'main' function. Program execution begins and ends there

#include "pch.h"
#include "QuestionMC.h"
#include "QuestionTF.h"

#include "TestBankMC.txt"
#include "TestBankTF.txt"

#include <iostream>
#include <fstream>
#include <string>
#include <ctime>
#include <cmath>

using namespace std;

int main() {

string question, answer;

cout << "====================================================\n";
cout << "== ==\n";
cout << "== WELCOME TO THE FBI ENTRANCE EXAM! ==\n";
cout << "== ==\n";
cout << "====================================================\n";
cout << endl;
cout << endl;
cout << "You will be asked a series of 11 questions consisting of both True/False questions and multiple choice questions.\n";
cout << "Please answer each question to the best of your ability\n\n\n" << "Good luck!\n\n\n" << endl;


//open file TestBankTF.txt
ifstream file("TestBankTF.txt");

// check for errors
if (infile.fail()) {
cerr << "Error Opening File" << endl;
exit(1);
}

if (file.is_open())
{
string myTestBankTF[12];
for (size_t i = 0; i < 12; i++)
{

}
{
file >> myTestBankTF[i];
}
}

// close TestBank.txt file
infile.close();

cout << "Please answer the following true or false questions with either 'true' or 'false': \n\n";
cin >> question;

string answersTF[3];

for (size_t i = 0; i < 12; i++)
{
if (myTestBankTF[i] == question)
{
cout << question << "\n\n";
cout << "Answer: " << endl;
cin >> answersTF[i];
}
else
{
cerr << question << "Error loading in myTestBankTF[].";

}

}


//open file TestBankMC.txt
ifstream file("TestBankMC.txt");

//check for errors
if (infile.fail())
{
cerr << "Error opening file" << endl;
exit(1);
}

if (file.is_open())
{
string myTestBankMC[71];

for (size_t i = 0; i < 71; i++)
{
string myTestBankMC[i];
}
else
{
cerr << question << "Error loading in myTestBankMC[]";
}
}

// close TestBankMC.txt file
infile.close();

cout << "Please answer the following multiple choice questions with the qppropriate letter answer (A, B, C, D, or E): \n\n";

string answersMC[11];

for (size_t i = 0; i < 71; i++)
{
if (myTestBankMC[i] == question)
{
cout << question << "\n\n";
cout << "Answer: " << endl;
cin >> answersMC[i];
cin.ignore();

}
else
{
cerr << question << "Error loading in myTestBankTF[].";

}
}
return 0;

} // end of main function]
1
2
#include "TestBankMC.txt"
#include "TestBankTF.txt" 

This looks suspicious. Unless those files contain C++ code, this will be an error.

#include means "copy and paste the entire contents of this file right here and then try to compile it as C++ code"
Topic archived. No new replies allowed.