CSCI 261 - Programming Concepts

Fall 2018 - Lab 7A - RPS: Game Log

Quick Links: Canvas | Mines | Piazza | zyBooks

|   Home |  Contact |  Syllabus |  Assignments |  Schedule |  Resources   |

This lab is due by Thursday, November 15, 2018, 11:59 PM.


Make a copy of Lab3A's main.cpp and place it as the starting point for Lab7A.


Rock Paper Scissors Part V



At the end of Lab3A, we had a fully functioning rock-paper-scissors game that allowed the user to play multiple games. Now we want to modify our program to create a log of every game played.


Concepts



The focus of this lab is on one concept: how to write data to an "output file stream" or ofstream object.


Working with Data



Today's class discussed how data is often treated as "streams" of information that can be written a piece at a time. The files we will write in CSCI 261 are simple text files; for lab today, the simple text file will contain a series of characters. Remember that whenever you work with a file stream as output, we call them ofstream objects.

There will always be four things you will do whenever working with an ofstream . Open the file, check for an error, write its data, and close the file. The typical pattern for this is as follows:

#include <iostream>
#include <fstream>
using namespace std;

int main() {
        ofstream myCatsAges("filename"); // declare ofstream object and open the file

        // check for an error
        if ( myCatsAges.fail() ) {
           cerr << "Error opening output file";
           exit(1);
        }

        // write the data
        myCatsAges << 5;

        myCatsAges.close(); // close the file

        return 0;
}

Remember, once you have an ofstream object (like myCatsAges shown above) you use it in a manner similar to using cout .


Instructions



For this lab, start with your solution to Lab3A. We want to write out a log of all of the user's input. What choice they entered and what time this occurred.

This log will be created by writing to a file in the following format:

Time: 1489549042 Human: Rock. Computer: Paper. = L
Time: 1489549155 Human: Rock. Computer: Rock. = T
Time: 1489549200 Human: Rock. Computer: Scissors. = W
Time: 1489549251 Human: Rock. Computer: Paper. = L
Time: 1489549277 Human: Rock. Computer: Paper. = L

From this log, we could go back and study a user's behavior. How long it takes them to complete steps, what order they make decisions.

You should modify your Lab3A program by first opening a file to write out to. Then everytime the user enters a choice, write to the file the current time and the game result following the format above. Do not worry about formatting the time to appear in a pretty format like hh:mm. The output of the time() function is sufficient.


CLion Setup



Where is the output file

After you run your program, you may wonder "hmm, where did the file I just wrote to go?". Well, if you expand the cmake-build-debug folder, then you will see the file you created. This is ok, but not our preferred location. We would much rather have the resulting file be placed at the same level as our main.cpp.

To do so, we need to tell CLion where we would like files placed. This can be found from going to Run > Edit Configurations. In the pop up window that appears, we want to set the "Working Directory" to be the path to the folder that our main.cpp file is located in. Hit Apply and OK, then rerun the program. You will now see the file created next to main.cpp.




Lab Submission



You will submit your solution to this lab with the rest of Set7. Detailed instructions for doing this are posted in Assignment 7.


This lab is due by Thursday, November 15, 2018, 11:59 PM.

Last Updated: 10/29/18 18:19


Valid HTML 4.01 Strict Valid CSS! Level Triple-A conformance, W3C WAI Web Content Accessibility Guidelines 2.0