Hi I am having trouble figuring out where to start with my project. What I need to do is instead of me creating the Test.txt file and inputting the needed information in a specific format(below), I need to allow the user to create the file, input the information in the specified format, then save it and print it out to the screen. In the end the output on the screen should look the same as it does now. I have posted my Test.txt file I have created (which I need to change to allow the user to create) and an example of the format it needs to be in below along with my code I have now. I am taking online classes and the teacher has been supposedly sick and nowhere to be found the whole first three weeks of the class (Which I have already filed a claim with the school) so I am basically trying to teach myself and it is not working very well. Any help will be GREATLY appreciated. Thank you for any help in advance.
FORMAT EXAMPLE OF THE TEST.TXT FILE:
3 - How many questions
TF 5 - What type of question / value of question
There exist birds that cannot fly? - Question
true - answer
MC 10 - What type of question / value of question
Who was the President of the USA in 1991? - Question
6 - how many choices
Richard Nixon - choice 1 (a)
Gerald Ford - choice 2 (b)
Jimmy Carter - choice 3 (c)
Ronald Reagan - choice 4 (d)
George Bush Sr. - choice 5(e)
Bill Clinton - choice 6 (f)
E - answer
TF 10 - What type of question / value of question
The city of Boston hosted the 2004 Summer Olympics? - Question
false - answer
END OF TEST.TXT FILE
MY TEST.TXT FILE I HAVE NOW:
6
TF 5
5 + 5 = 55?
false
MC 10
What is 9 - 6?
6
Two (a)
Three (b)
Six (c)
Seven (d)
Nine (e)
Ten (f)
B
TF 5
10 * 500 = 5000?
true
TF 5
9,625 / 35 = 275?
true
MC 10
What is 100 * 5?
6
Two hundred (a)
Three hundred (b)
Six hundred (c)
Seven hundred (d)
Nine hundred (e)
Five hundred (f)
F
MC 10
What is 1000 - 956?
6
Nineteen (a)
Fifty four (b)
Fourty six (c)
Fourty four (d)
Seventy four (e)
Fifty six (f)
D
END OF MY TEST.TXT FILE
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
|
#include <iostream>
#include <string>
#include <fstream>
class Quiz
{
private:
int score;
int numberOfQuestions;
int numberOfAnswers;
std::string questionType;
int questionValue;
std::string question;
std::string mcAnswers[10];
std::string tfAnswer;
std::fstream quizData;
bool openQuiz(std::string);
void closeQuiz();
void doQuestions();
public:
Quiz();
bool Run(std::string);
};
Quiz::Quiz()
{
score = 0;
}
bool Quiz::openQuiz(std::string Test)
{
quizData.open(Test, std::ios::in);
if (quizData.fail())
return false;
else
return true;
}
void Quiz::closeQuiz()
{
quizData.close();
}
void Quiz::doQuestions()
{
std::string numberOfQuestionsData;
std::string questionValueData;
std::string numberOfAnswersData;
std::string mcData;
getline(quizData, numberOfQuestionsData);
numberOfQuestions = stoi(numberOfQuestionsData);
for (int i = 0; i < numberOfQuestions; i++) {
getline(quizData, questionType, ' ');
if (questionType == "TF") {
getline(quizData, questionValueData);
questionValue = stoi(questionValueData);
getline(quizData, question);
std::cout << "Question #" << i + 1 << ": True of False: " << question << " Points value = " << questionValue << std::endl;
getline(quizData, tfAnswer);
std::cout << tfAnswer << std::endl;
}
if (questionType == "MC") {
getline(quizData, questionValueData);
questionValue = stoi(questionValueData);
getline(quizData, question);
std::cout << "Question #" << i + 1 << ": Multiple Choice: " << question << " Points value = " << questionValue << std::endl;
getline(quizData, numberOfAnswersData);
numberOfAnswers = stoi(numberOfAnswersData);
for (int k = 0; k < numberOfAnswers; k++) {
getline(quizData, mcAnswers[k]);
std::cout << mcAnswers[k] << std::endl;
}
getline(quizData, tfAnswer);
std::cout << tfAnswer << std::endl;
}
}
if (questionType != "MC", "TF") {
std::cerr << "Either there has been an error! Or this is the end of the file. Check the file to be sure." << std::endl;
}
}
bool Quiz::Run(std::string Test)
{
if (openQuiz(Test)) {
doQuestions();
closeQuiz();
}
else
return false;
return true;
}
int main(int argc, char* argv[])
{
Quiz Game;
if (!Game.Run("Test.txt"))
std::cerr << "Failed to run due to missing quiz data.";
std::cin.ignore();
return 0;
}
|