CSCI 159 Structs and File I/O Lab

CSCI 159: Fall 2024 Lab 5

(Part I Soft due date Nov 13. Part III hard due date Nov 19 or 20 at the beginning of lab)

    Part I



    Part 1: In your dev directory, create a new directory called lab5 ("mkdir lab5") and cd into it. Edit a new file called lab5.cpp. Into this file, ensure you have the elements as they exist in the following file. Short explanations appear below. lab5 starter file The first part of this lab, for you to do in the first week, is to create structs that can keep data for the following: Your program will use file i/o functions:
    	ifstream f declares the filestream (input)
    	f.open("game.dat") // looks for a file in current directory 
    			    //	with this name
    	f.fail() // checks if the file was there and openable
    	
    	use f>> to get stuff from the file, just like cin
    
    	f.close() // closes the file
    
    	
    You program should take a date file that looks like game.dat and reads in the data. Read the data into the array players. A line is data for a player if it starts with P. If the line starts with Z, that is the end of the file. Print out the players with their scores. Output should look like this.

    PART II

  1. While you are in your top directory: You should have this in your home directory:
    	make159      
    	

  2. While in the top directory, you will type the following command onto the command line and hit return:
    	make -f make159 csci159/lab5 
    	
    This makefile will create a new directory csci159 with a subdirectory called lab5, and it will populate that directory with some files that your instructor has provided. cd into that lab5 directory. Do an "ls". You are provided with a README. Read the README. You are also provided, in directory lab5, with files called makefile, and a data file "game.dat". cat these files; you can see that they have some data ready to be read in.


  3. The new makefile is ready for you to use.

    To get your files from your dev directory to your new, git-connected csci159/lab5 directory:
    While in your top directory (entery 'cd' on the command line, and that will always take you to your top directory), do the following commands:
    		cp csci159dev/lab5/lab5.cpp csci159/lab5/.
    		
    Now you can "cd csci159/lab5", and your lab5.cpp from your dev directory is now copied to your git-connected directory.

  4. Amend lab5.cpp so that it prompts the user for new data. The query and response should be as indicated below.
    		Enter 'P' new player, 'S' new or existing score for a player, or 'X' exit?
    
    		<user enters 'w'>
    
    		Enter 'P' or 'S' or 'X':
    
    		<user enters either 'P' or 'p'>
    
    		Enter new player's username:
    
    		<user enters "funnygrl">
    
    		funnygrl is a new player, all scores are 0. 
    
    		Enter 'P' new player, 'S' new or existing score for a player, or 'X' exit?
    
    		<user enters 'S' or 's'>
    
    		Enter player's username:
     
    		<user enters "ObiWan">
    
    		Player Obiwan does not exist. 
    
    		Enter 'P' new player, 'S' new or existing score for a player, or 'X' exit?
    
    		<user enters 'S' or 's'>
    
    		Enter player's username:
     
    		<user enters "funnygrl">
    
    		Enter level:
    
    		<user enters an integer between 1 and 4, say it is 2>
    
    		Enter new Level 2 score for funnygrl (0 for score report): 
    
    		<user enters an integer between 0 and INT_MAX, say it is 300>
    
    		300 is a new high score for funnygrl at level 2!  Recorded.
    
    		Enter 'P' new player, 'S' new or existing score for a player, or 'X' exit?
    
    		<user enters 'S' or 's'>
    
    		Enter player's username:
     
    		<user enters "funnygrl">
    
    		Enter level:
    
    		<user enters an integer between 1 and 4, say it is 2>
    
    		Enter new Level 2 score for funnygrl (0 for score report): 
    
    		<user enters an integer between 0 and INT_MAX, say it is 200>
    
    		funnygrl has score of 300 at level 2.
    
    		Enter 'P' new player, 'S' for new or existing score for a player, or 'X' exit?
    
    
    		<user enters either 'P' or 'p'>
    
    		Enter new player's username:
    
    		<user enters "funnygrl">
    
    		funnygrl already exists. 
    
    		Enter 'P' new player, 'S' for new or existing score for a player, or 'X' exit?
    
    		<user enters 'X' or 'x'>
    
    		Thanks for playing!
    
    		
    As usual, however, do not put a newline in between the program's prompt and the user's input. Look here for what that same interaction would look like on the screen.

    At the point of 'X' or exit, the program will write the current set of data to the file game.dat. It should overwrite the existing file. All player data records should be lines that start with the letter 'P'. The final line should start with 'Z' to indicate end of data.

    When the program starts up again, any new data (new players or new scores) should be reflected in the new outputs, when queried. (I.e., funnygrls score at level 2 should be 300.)

    Also, if there is no game.dat file in the current directory, at the beginning of the program's execution the program should say, "No game.dat file found, creating one..." and should proceed to the input sequence as given above.

    Note that if any score is entered that is less than the player's current score at that level, then the program gives a report of the player's current score at that level.


  5. Notes about the design: Observe that the above-described interaction reflects a kind of loop. At each iteration, the program is asking, "Enter 'P' new player, 'S' for new or existing score for a player, or 'X' exit?" Then the program does what is indicated.
    The array of players is not required to be in any particular order, so the insertion of a new player can be at the next empty slot. When searching for an existing player, you can make it do linear search. For your design, do not dump all of this code into main()! Look at the following code for ideas. example.cpp Note: you do not have to follow it slavishly -- it is not an implementation of lab5 but a mishmash of typical code fragments. Nevertheless, it has good design! Also, the example file has too few comments. You should write your own comments, and remove any from the example that don't make sense in the context of lab5.

    You will have marks deducted if you do not have a "int find(string usr)" function that returns the index of the user with that username, or some value like -1 if it is not found! Such functionality should be hived off into a separate function.

  6. When you have completed parts I and III, execute the following command:
    	make lab5x
    	
    Make sure it gives you no errors or warnings. Warnings can cost you marks.


  7. Now do the following:
    	make clean
    	
    This cleans up your directory of old executables. "ls" to see what remains.


  8. Execute the following command:
    	make submit
    	
    If successful, you will see a variety of updates. The git version control program is copying your updated directory contents to a location where the instructor can view and mark it. There is a deadline programmed into git, so if you try to submit after the deadline, it won't let you. Up until that deadline, you can submit and it will keep track of the most recent version, which is the one your instructor will look at.

  9. PART III


  10. Copy your file from Part I (which probably resides in your csci159dev directory) into a file in csci159/lab5 called lab5.cpp Edit it to satisfy the requirements as per the README. You can resubmit the files on Oct 22/23 using the usual 'make clean' and 'make submit' commands.

    One way to accomplish the transfer from your dev directory to your git-created directory is the following sequence of commands:


Useful Links: Gara Pruesse's Homepage
Computing Science Homepage
Vancouver Island University Homepage
X2Go