One of the main points of the program is the use of file reading and writing. We used four different .txt files, which were: "cpr.txt", "Questions.txt", "CorrectAnswers.txt", and "UserAnswers.txt".
The "cpr.txt" file contains the steps for performing CPR and uses file reading.
The "Questions.txt" file contains the questions themselves. This is where we used dynamic memory as well - we created a string vector called "filelines" to hold all of the lines in the Questions file. The array questionNum acted as an index for filelines and allowed us to randomly shuffle the order of the questions.
vector <string> filelines; //vector of strings for file lines
ifstream questions("Questions.txt", ios::in | ios::out); //open file
if(questions.is_open() ) { //if file is opened
while(getline(questions, line)) { //gets total number of lines
filelines.push_back(line);
}
// Make an array (questionNum) of question numbers
int questionNum[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
// Shuffle the index vector
for(int i = 0; i < 20; i++){
int f = rand()%filelines.size();
int s = rand()%filelines.size();
std::swap(questionNum[f], questionNum[s]);
}
The "UserAnswers.txt" file is initially empty. The current question from the Questions file and the user's answer to that question, as well as the correct answer from "CorrectAnswers.txt" is written into the file as the program loops through questions. The writing to UserAnswers is seen below. answer << filelines[curQ] << endl; // add Q to the file
answer << "\tYour answer: " << userAns << endl; // add user answer to file
string corrAns = uScore.getCorrectAnswer(curQ);
answer << "\tCorrect Answer: " << corrAns << endl; // add correct answer
"CorrectAnswers.txt" is used in the case previously mentioned, but also mainly where object oriented programming was utilized. The object oriented programming we used was a class called Score, which helped to track the user's score as they answered questions. There was one private data member, which was "int userscore." The default constructor set userscore to zero, and there were three public methods: one was to adjust the score depending on if the user was correct or not, another was to get the correct answer to a question, and the final was to get the userscore. Below is the second method, as an example of how "CorrectAnswers.txt" was used in the Score class. It is similar to the code written for "Questions.txt" and filelines (the first lines of code seen, above).
void userCorrect(string uAns, int lineNum){
ifstream corrAns;
corrAns.open("CorrectAnswers.txt");
if(corrAns.fail()){
cout << "Cannot open CorrectAnswers.txt" << endl;
}
string line;
vector <string> corrAnslines; //vector of strings for corrAns file lines
while(getline(corrAns, line)) { //gets total number of lines
corrAnslines.push_back(line);
}
if(uAns == corrAnslines[lineNum]){
// user has correct answer! + 10 points
userscore += 10;
}
corrAns.close();
}
Reflection: Lessons Learned
Specifically, we learned how useful file input and output can be to a program. Our program relied heavily on this aspect, since we wanted to see the functionality of fstream and how it could be applied in different ways and in a practical sense. It was also interesting to combine file input/output and object oriented programming and see those results.
When developing our ideas for the project and beginning to implement them, we each had different ideas that would result in the same outcome. There are multiple ways to approach a problem, and it can be difficult to see which one would fit the best before actually implementing it.
A difficult part about this project was pair programming while taking the class online. It is nearly impossible to do in the conventional way with everything happening in the world currently, but we managed to message each other back and forth to provide feedback and ideas.
Future Potential Work
This project could be extended in many different ways. Here are a few ideas.
- Expand the program to include questions for first aid as well.
- Make another .txt file called IncorrectAnswers. Using fstream, write to the file which questions that the user answered incorrectly, their answer, and the correct answer. Explain why the user is incorrect.
- Expand the program to include CPR scenarios - prompt the user with a specific scenario and walk them through it, so they receive more practice before beginning their quiz.
No comments:
Post a Comment