CSCI 261 - Programming Concepts (C++)Fall 2017 - Lab 6AQuick Links: Canvas | CS @ Mines | Cloud9 | Piazza | zyBooks |
|
| Home | Contact | Syllabus | Assignments | Schedule | Resources | | |
This lab is due by Friday, November 10, 2017 11:59 PM .
Make a copy of Lab3A's main.cpp and place it as the starting point for
Lab6A.
Rock Paper Scissors Part VAt 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.
ConceptsThe 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.
Lab Submission
You will submit your solution to this lab with the rest of A6.
Detailed instructions for doing this are posted in Assignment 6.
This lab is due by Friday, November 10, 2017 11:59 PM . | |
Last Updated: 10/22/17 19:43
|