Lab 12

In this lab, you will be writing the readAlbums and mainMenu functions of your MP2. Since this is part of your MP, you should work this lab individually.

Follow the instructions for MP2 under the heading "Preparation" to get ready to do the MP by loading the project into Eclipse.

Also, read the brief introduction and familiarize yourself with the structure definition of an Album.

mainMenu function

The first thing we'll do is write the mainMenu function. In Eclipse if you open the file mp2.h you will find the prototype:
int mainMenu( void );
Copy and paste this prototype into the input.c file. DO NOT modify the mp2.h file. In input.c modify this prototype by replacing the semicolon with opening and closing curly braces. Then your input.c file should look like the following:
/* input.c */

#include "mp2.h"
#include "user.h"

int readAlbums( char *filename, Album albums[], int num_albums, int max_size )
{


}


int mainMenu( void )
{


}
Don't forget to remove the semicolon from the end of the line after you copy it over, and to put the code for your function in curly braces ( "{" and "}" ).
You've done menu functions in previous labs; this one will be very similar. You want to display the list of options to the user, and return whatever number the user enters. Assume that the user always enters an integer. We won't worry about checking for invalid integer values in the mainMenu function. You will need to declare an integer variable.

Your menu function should display the following menu:

    Main Menu
1. Display
2. Sort
3. Search
4. Edit
5. Statistics
6. Save to disk
0. Exit

Please enter the choice:
Next use the scanf function to read the value the user entered at the keyboard. Finally, return the choice to the function that called mainMenu.

Every function you write for this MP2 will be done in this way. That is, first copy and paste the prototype found in mp2.h See the Implementation section for a step by step list of functions. You will only be writing the code for mainMenu and readAlbums for this lab 12.

When you finish writing your code for the menu function don't forget to save your changes and make your project as described in the MP2 instructions.

readAlbums Function

Of course, your CD album program isn't going to be much use unless it can read a file containing information concerning CD albums. That's what the readAlbums function does.

The function header for readAlbums has already been typed into the input.c file for you:

int readAlbums( char *filename, Album albums[], int num_albums, int max_size)
{
}

The function readAlbums is called by main. If you look at the main function in mp2.c, a line of code that calls readAlbums with the correct parameters (the filename, the array of Albums and 0 is the current number of albums and MAX_ALBUMS will be assigned to max_size) has been included.

Since you're going to get input from the user (keyboard by using mainMenu) as well as from the file (album.dat by readAlbums), we'll have to use real file I/O (input/output) here, rather than just reading from "standard in" (the keyboard) and letting the operating system make us think the file was the keyboard (that's what you've been doing when you run something like "program < file"). Since we haven't seen that yet, here's how it should be done:

 /* begin of readAlbums */

int track, i;
FILE * fileptr; /* flieptr is a pointer variable, FILE is a new datatype */

fileptr = fopen( filename, "r"); /* filename is a string, "r" means open file for reading */
if (fileptr == NULL)
return -1;

/* read the data from ALBUM_FILE */
/* first record looks like ...
1001; One Heart; Celine Dion; 2003; 14; 2; 13.99
211.10 96.15 67.93 340.67 138.01 250.97 259.76 74.66 114.92 268.26 126.47 79.63
*/

/* The datatype Album (from mp2.h) looks like...
typedef struct {
int cdno;
char title[30];
char artist[20];
int year;
int num_tracks;
int quantity;
float price;
float sales[MONTHS]; cd sales ($US) over the 12 months in 2005

Track tracks[20];
} Album;
*/

/* check to make sure that we haven't exceeded the maximum size for the array albums */
if ( num_albums >= max_size) {
fclose(fileptr);
return num_albums;
}

/* you must fill in the blank on the line below */
/* num_albums starts at 0 */

while( EOF != fscanf(fileptr, "__________________________________",
&albums[num_albums].cdno,
albums[num_albums].title,
albums[num_albums].artist,
&albums[num_albums].year,
&albums[num_albums].num_tracks,
&albums[num_albums].quantity,
&albums[num_albums].price) ) {

/* read the sales over 12 months */
for ( i = 0; i < MONTHS; i ++ ) {
fscanf( fileptr, " %f", &albums[num_albums].sales[i]);
}

/* We need to read the track data into the array named tracks.
* The first record has the following form ...
* I Drove All Night; 240
*/
/* The datatype Track (from mp2.h) looks like...
typedef struct {
char name[40]; track name or song name
int length; in seconds
} Track;
*/
for( track = 0; track < albums[num_albums].num_tracks; ++track ) {
/* fill in the following blanks to read in all the track data */

fscanf( __________ ," %[^;]; %i ", albums[num_albums].tracks[track].name, ___________________________________________ );
}

/* we succeeded in reading in one more album so increase num_albums by one */
num_albums++;

/* we don't want to read more CDs than the size of the albums array so check now */

if ( num_albums >= max_size) {
fclose(fileptr);
return num_albums;
}
} /* end of while */

fclose(fileptr);
return num_albums;
/* end of readAlbums */
Notice that there is a "space" before %[^;] in the format string of fscanf. This is critical to ignore any blank space in the input file before your program sees the track name. When you test your program, it's going to be a little hard to tell if readAlbums is working if you can't see what you read in, right? Fortunately, the mp2 checker provided will allow you to test your readAlbums function without having to write the main function. (See below.)

Prepare for you MP2

In this lab, you have written the function mainMenu and readAlbums. In MP2, you are asked to write a bunch of other functions. To get prepared, please read instructions for MP2, "Before you start..". As the last part of this lab, please copy and paste each function prototype into their corresponding .c file. DO NOT modify the mp2.h file. Modify each function so that they have an empty function body, just like what you did for mainMenu and readAlbums at the very beginning of this lab.

If you do it right, you should be able to compile every .c files by typing:

make mp2checker
from the ~/workspace/mp2 directory.

If you see any errors, correct any mistakes (e.g., missing a pair of curly brackets, forgetting remove the semicolon after the parenthesis).

Now, you can play with the mp2checker. Please refer to the section "Checking" in instructions for MP2.

Fill in the blanks on the answer sheet and drag your TA across the room to show him/her that your program works and it gets full points from the mp2checker for test case 3 (readAlbums) and 4 (mainMenu). Beg him for that autograph that you know is going to be a collectible in 20 years, and you're done!