Contents

Overview

Your job in this assignment is to create the fourth iteration of the Notes program. You are going to update your program so that it saves and loads notes to/from disk. This will keep the notes persistent between different executions of your program. So if you run your program, make some notes and quit, then run your program again, it should display all the notes from your previous run.

To make this happen, you should add two function to your PA3: one to load from a the storage file when your program starts up, and another that writes the notes in memory to the storage file when the user says to save or quit. The storage file name should be stored in a constant: const string STORAGE_FILE_NAME = "storage.txt".

Step 1: load function

You need a function that will take care of reading in previous notes, if any exist. Here are the things to be decided:

  1. what's the function going to be called?
  2. how are we going to get the notes array and the number of notes read in back from the function?
  3. how do we know if there are any notes to read?
  4. how is the storage data file formatted?

Some of these you'll need to think through (namely, 1 and 2). For question 3, consider what we did in Lab10—we worked on a very similar problem. For question 4, assume the file is formatted as follows (whether this works or not depends on you implementing the save function correctly):

number of notes
contents for note 1
priorityLevel for note 1
isDeleted for note 1
contents for note 2
priorityLevel for note 2
isDeleted for note 2
...

E.g., a storage file with two notes it may look like this:

2
This is my first note!
1
0
this is a deleted note
3
1

Note that the isDeleted line is either 0 or 1—this is because C++ represents booleans as 1 (true) and 0 (false). If you did cout << false;, the number 0 would be displayed. To read in the data, you're going to need to use getline to read the contents, and then the usual fileIn >> ...; to read directly into struct fields for priorityLevel and isDeleted, as well as to read in the number of notes.

Step 2: save function

Reading in data is half the problem. In order to read the previous state of your notes, you need to have saved them. So, whenever the user quits, you should invoke a save function. This function should be responsible for writing out every note in the array in the format described above: each note has three lines—one line for the contents, one for the priority level, and one indicating if it's deleted or not. The code to do this will look very similar to how you print data out to the user, but using a different format, and you'll be writing to a file.

Here's an example of my version of PA4 running (my input is in orange just to make it obvious):

$ ./pa4 

Current notes:
Note 1: 
Note 2: 

Would you like to [d]elete|[m]odify a note or [q]uit? m
Which note would you like to modify? 1
Please enter the new note: Finish homework
Please enter the new note's priority level: 1

Current notes:
Note 1: Finish homework (Priority 1)
Note 2: 

Would you like to [d]elete|[m]odify a note or [q]uit? q
Okay, goodbye!

Then when I run it again, I see:

$ ./pa4 

Current notes:
Note 1: Finish homework (Priority 1)
Note 2: 

Would you like to [d]elete|[m]odify a note or [q]uit? 

See how my Note 1 is just like I left it the first time I ran the program?

(Back to top)

Requirements

Start by copying your PA3 and work from there.

(Back to top)

Extra credit

Extra credit up to 5 points. Make sure you indicate in your program header (in the description) whether you've attempted the extra credit, otherwise I won't count it!!!

EC-1 (2 points)

Submit your assignment 24 hours early—by Sunday night at 11:59.

EC-2 (2 points)

Add a '[S]ave' option, where users can decide to save right then rather than waiting to quit.

EC-3 (5 points)

Allow users to add new notes, or completely remove old ones. This is challenging because you will have to grow and shrink your array (which means copying data from an old array to a new one that is larger or smaller).

(Back to top)

Submissions

Everything should be uploaded to Canvas by the deadline posted.

(Back to top)