Thursday, May 14, 2020

Main Points, Reflection, and Future Potential

Main Points of the Program
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.

CPR Tutor Demonstration

Here is a demonstration of what our code looks like on the terminal!

The program first displays a welcome message to the user and explains what they will be doing, which is taking a quiz about the steps to CPR. 


The program displays the steps for CPR next, and then explains to the user how to answer fill in the blank questions. 
The first question is displayed too. The user should answer "A" to be correct - it is true that checking if the person is breathing is a very important step in performing CPR. 

A few questions have gone by, and the user has answered. The user is going to answer incorrectly for this multiple choice question (just for fun): How many times should you compress and breathe into the person who is choking? 
A) 4 times. B) 9 times. C) As many needed until help arrives. D) Maybe 11 times.
They pick D, which is incorrect. 

The user continues to answer questions, until they are finished with the quiz, and the program displays this message below. By answering some of the questions incorrectly, the user received lower than a 100 percent. 

Not displayed to the terminal is the "UserAnswers.txt" file, which begins blank...

and then while the program is running, fills with questions and answers. This is to let the user reference to which questions they answered correctly and incorrectly. 

Wednesday, May 13, 2020

Project Outline

For our final project, we wanted to focus on file reading and writing, object oriented programming, and dynamic memory. To do this, we thought that some sort of tutorial program would best fit what we wanted to accomplish. After some brainstorming, we decided that creating a program that is focused on CPR would be a good start. Outputting the appropriate steps to performing CPR and then asking questions in a "quiz" format afterwards would be the direction of our program. The purpose of this is to educate the user about the proper way of performing CPR, something that not many people know how to properly do, or what to do if such a scenario occurred. It is a simple program that anyone can use. 
Our code utilizes user input and file reading to create a quiz that revolves around the steps to perform CPR. The program outputs the steps to the user, and tells them that they will be asked questions related to the steps. Each time the user answers a question, their answer is stored on a new file, and their answers are compared to the correct answer stored in a separate file. For each correct answer, they get 10 points. In total, the user can get 100 points possible. After the user answers all questions, their total score will be displayed on screen. 
The main part of our program is the ability to read and display the files onto the screen. It is the main focus of our project, since it is the most efficient way to run our program. Storing the user answer and each line from each file is another large part of the project. We were able to do each successfully and revolve it around how we wanted to execute our code.