Мой вопрос касается школьного задания, и я хочу подчеркнуть, что я не ищу кого-то, кто просто дал бы мне ответ. Тем не менее, я ищу направление, чтобы я мог решить эту проблему.
Я сделал тестовую программу, состоящую из вопросов с несколькими вариантами ответов и вопросов типа «истина / ложь». Я разместил эту информацию в 2 .txt файлах. Я организую свой код по классам. Мне нужно извлечь информацию из файла .txt, вывести ее на экран, сравнить результаты пользовательского ввода с ответами на тест и вывести результат теста.
Я знаю, как извлечь из файла .txt, но у него есть вопросы с множественным выбором и истинно-ложными, которые дают данные о строках и символах. Я подумал о создании массивов для хранения вопросов и ответов; Однако я начал сомневаться, существует ли более эффективный способ организации этих данных. Кажется, мне нужно слишком много массивов, и, возможно, одного или двух методов может быть достаточно. Я знаю, как рассчитать результаты тестов, и мне не нужно указывать направление в этой части.
Я разрабатываю этот код C ++ с использованием Microsoft Visual Studio IDE.
Я исследовал массивы, векторы, объекты, классы и многие другие темы, которые, по моему мнению, могут быть связаны с этой проблемой. Я прочитал учебник для колледжа, слушал лекции, смотрел видео на YouTube и консультировался с некоторыми программистами через Facebook. Я гуглил вопросы и читал ответы на различные темы C ++ и примеры программирования. Тот факт, что я все еще в замешательстве, заставляет меня поверить, что, возможно, я просто не понимаю этих понятий, какими я должен быть.
Может ли кто-нибудь с большим опытом направить меня в правильном направлении?
******** \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ **
Вот код, который я создал до сих пор:
// 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";
}