I am trying to load an exam and have it read from a separate .txt file. I don't know how to make it work. I am using Cygwin and have code and have a .txt file. I made it in wordpad. It wont talk to the source code. Do I not use wordpad. I see it using excel a lot but I don't know how that works either. I want the code to load an exam and ask the user questions. The user can tell the code which question to ask then it will make the question based on the .txt file. Here is the code I am using.
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
//question class
class Question
{
protected:
string question;
int value;
public:
Question(string theQuestion, int pointValue)
{
question = theQuestion;
value = pointValue;
}
string getQuestion(string qu)
{
question = qu;
return question;
cout << "\t" << question << endl;
}
int getValue(int val)
{
value = val;
return value;
cout << "Worth " << value << " points" << endl;
}
virtual ~Question(){}
virtual string printOptions()
{
return NULL;
}
virtual string getAnswer()
{
return NULL;
}
}; //end question class
//multiple choice class
class QuestionMC : public Question
{
private:
string answer, options[];
public:
QuestionMC(string theQuestion, int pointValue, string theAnswer) : Question(theQuestion, pointValue)
{
theAnswer = answer;
}
string addOption(string anOption)
{
for (int i = 0; i < 4; i++)
{
anOption = options[i];
}
return 0;
}
string printOptions()
{
return 0;
}
string getAnswer(string ans)
{
ans = answer;
return answer;
cout << "\tAnswer - " << answer << endl;
}
}; //end multiple choice class
//function to read the bank in
void testBank()
{
//declarations
int numQuestions, numOptions, points;
string questionType, questionText, answerText, mcOption;
//multiple choice
else if (questionType == "MC")
{
getline(infile, questionText);
infile >> numOptions;
for (int j = 1; j <= numOptions; j++)
{
getline(infile, mcOption);
mc.addOption(mcOption);
}
getline(infile, answerText);
mc.getAnswer(answerText);
}//end multiple choice
}//end loop
};//end test bank function
int main()
{
//open test bank file
ifstream infile;
infile.open ("testbank.txt");
//error if file can't open
if (!infile)
{
cout << "File could not be opened" << endl;
}
testBank();
infile.close();
return 0;
}//end main
here is the .txt file.
3
TF 5
There exist birds that cannot fly?
true
MC 10
Who was the President of the USA in 1991?
6
Richard Nixon
Gerald Ford
Jimmy Carter
Ronald Reagan
George Bush Sr.
Bill Clinton
E
TF 10
The city of Boston hosted the 2004 Summer Olympics?
false
I just have no idea where to go from here. I have tried changing the int main to work differently but when I am changing my code around I just cause more errors.
oh no I haven't. I think that has been the majority of my problem. I don't know what program to use to try and open it. Would it be in word or excel? Would these programs make a workable program through Cygwin?
Programs like Wordpad don't work with plain text. They create a heavily-marked up version of text that you really don't want to spend the time parsing.
You need a program that creates only text. On Windows, the default program to do that is *cough*Notepad*cough*. You also have another program that can do it — your programming IDE! If you are using Code::Blocks or Geany or Visual Studio or Notepad++ or whatever, use that to create your .txt data file.