COMP2004 Programming Practice
2002 Summer School

Assignment 2 - Calendar System


Summary

DUE DATE: 5:00pm Tuesday 29 January 2002 (extension announcement)
WEIGHT: 10%
SUBMISSION NAME: comp2004s.a2
MARKING TYPE: automated testing and hand marking
TASK: Write a Calendar system

Detailed Description

Common Questions

  1. When I run my program it does nothing...
    There is a unix command called calendar which does something very similar to the assignment, you are probably running it instead, try running your program as ./calendar

  2. I'm using C++, how do I access the shell variables CALENDAR and CALENDAR_DATE?
    There is a C library function called getenv that you can call. See the man page (run the command man getenv) for details. Basically you will use getenv("CALENDAR") to get the name of the file to use, and the obvious for the date.

  3. I'm using C++, how do I read from and write to a file?
    You use the fstream library which is just like the iostream library. information is available here. They work just like cin and cout so once you see the examples of how to create them it should be easy.

  4. I'm using C++, how can I read the rest of a line?
    You can use either of the getline() or get() methods in the istream class. getline() is a pain because it reads into C-style strings of a fixed size (ie. you have to define char s[256];). The easier way is to use get(), which returns a char as an int (ie. you do something like int ch = std::cin.get();). This is so that it can signal end-of-file by returning the 'character' EOF. Thus you can have a while loop which keeps reading a single character and appending it to a C++-style string, until you read a character which is either EOF or '\n'. Reference information on getline() and get().

  5. Does my program need to set the environment variables CALENDAR and CALENDAR_DATE?
    No. They will be set by the machine marking before it runs your program. So to test your program, you should set them by hand in an xterm before running your program. You do this using the export command, as shown in the earlier example.

  6. What's the point of the CALENDAR environment variable?
    Your calendar program will be run once each time an event is added, some events are shown, or some events are purged. This means that if an event is added, and then a show command is given, your program has been run twice. This means that you can't just store events in memory when you get an add instruction, because if you did, that event would be lost when your program finishes (which is before you get a show instruction). So you need to store events in a file somewhere, so that they can be read back in later. The name of the file into which you should store the events is the value the CALENDAR environment variable.

  7. I'm confused. Does the CALENDAR environment variable store a file or what?
    An environment variable simple stores a string. So the CALENDAR environment variable stores a string. It's up to you as a programmer to treat that particular string as the name of a file. The way you would typically do that (in C++) is to pass that string to the constructor of an ofstream object.

  8. Does my program need to create the file with filename given by the CALENDAR environment variable?
    If the file with filename given by the CALENDAR environment variable doesn't exist, your program should create the file (since a file must exist before it can have information written into it). Otherwise you should assume that the file contains events which were stored by previous runs of your calendar program. The machine marking will remove all data from the calendar file before running your program, so if the calendar file exists when your program is run for the first time, then the file will be empty (ie. 0 bytes).

  9. What's the point of the CALENDAR_DATE environment variable?
    If you were writing a proper calendar program, then your program would need to figure out what day today is. It would do that by asking the computer for the current date and time. However, if you were to write your code in that way it would make machine marking very hard, because I would have to change the date of my computer to test your code with different values of "today". To avoid this problem, your program should figure out what day today is by looking at the value of the CALENDAR_DATE environment variable. This is a string, and so you'll need to figure out a way to convert it into a date of some sort so that you can actually use it.

  10. What do I do if I get an invalid date?
    You can assume that every date you'll be given in an add instruction will be a valid date (ie. you won't be asked to add an event for 36/1/2002). However, you may be given a date which is in the wrong format. For example, you might be given a date which looks like "22//2002". In this case, you should ignore the add request, and not add the event description which is specified on stdin. You can assume that the contents of the CALENDAR_DATE environment variable will always be in the same format as shown in the example earlier, which is the format used by the date command. If you do man date to get the manual page for date, it tells you that this format corresponds to using the format string "%a %b %e %T %Z %Y" with the strftime function. If you want, you can do man strftime to get a detailed description of each of the "%" codes used by date.

  11. Which days should I show if today is a Saturday or Sunday?
    Well, neither Saturday nor Sunday is Friday, so the normal rule of today and tomorrow applies (ie. for Saturday, show Saturday and Sunday, for Sunday, show Sunday and Monday). Friday is the only day which needs to show more than just today and tomorrow.