LAB 4 CD Catalogue Assignment For this assessment you are as

LAB 4
CD Catalogue Assignment

For this assessment you are asked to implement and test a program for cataloguing a collection of compact
disks.
Each compact disk has the following details:

Name of Band/Singer String of 48 characters
Name of CD String of 48 characters
Reference String of 16 characters
Cost Floating point number
Year of Release Integer

The program should carry out the following tasks:

• Read in data from the standard input (keyboard) and save it appropriately organised in a file.

• Retrieve from the file and print out data to the screen appropriately annotated (e.g. Name of Band:
XXXX, etc).

The User Interface of the program should be based on the following text based menu:

The following options are available:
Read in data [1]
Print out catalogue to screen [2]
Save data to file [3]
Exit Program [4]
Enter your choice now:
After a task is performed the user should be probed for a new choice until Exit is requested.

Programming hints:
You could create a custom struct to hold the above details.
You can use unformatted input to read in one line at a time into a string. This will allow you to read in
names and titles with spaces, e.g.
gets(str);
to read characters from the standard input into a character array called str.

Keep in mind that gets() does not
let you specify a limit on how many characters are to be read so you must be careful with the size of the
array pointed by str to avoid buffer overflows. You may also use fgets() although the usage differs from gets() slightly.

Solution

The below code is usually mean to take take 10 inputs to file for simplification. The inputs are singer name, title, id, cost, year of release.

#include <stdio.h>
#include <stdlib.h>

#define SING_BAND   40
#define CD_NAME   40
#define REF_ID   10           
#define cost 10
#define Year_rel 4


struct Music {                 

    char singer[SING_BAND];
    char title[cd_name];
    char id[ref_id];
    int rate[cost];
    int year[year_rel];

};


int main(void)

{

    struct music catalogue[10]; /* array of structures     */
    int count = 0;
    int index, filecount;
    FILE * pmusic;

    int size = sizeof (struct music);

    if ((pmusic = fopen(\"music.dat\", \"a+b\")) == NULL)

    {

        fputs(\"Can\'t open book.dat file\ \",stderr);

        exit(1);

    }

    rewind(pmusic);            /* go to start of file     */

    while (count < 10 && fread(&catalogue[count], size,

                1, pmusic) == 1)

    {

        if (count == 0)

            puts(\"Current contents of music.dat:\");

        printf(\"%s by %s: $%.2f\ \",catalogue[count].title,

            library[count].singer, catalogue[count].id);

        count++;
    }

    filecount = count;

    if (count == 10)

    {

        fputs(\"The book.dat file is full.\", stderr);

        exit(2);

    }

    puts(\"Please add new album.\");

    puts(\"Press [enter] at the start of a line to stop.\");

    while (count < 10 && gets(catalogue[count].title) != NULL

                          && library[count].title[0] != \'\\0\')

    {

        puts(\"Now enter the author.\");

        gets(catalogue[count].singer);

        puts(\"Now enter the value.\");

        scanf(\"%f\", &catalogue[count++].id);

        while (getchar() != \'\ \')

            continue;                /* clear input line */

         if (count < 10)

             puts(\"Enter the next title.\");

    }
   if (count > 0)
    {
        puts(\"Here is the list of albums:\");
        for (index = 0; index < count; index++)
            printf(\"%s by %s: $%.2f\ \",catalogue[index].title,
                   library[index].singer, library[index].id);
        fwrite(&catalogue[filecount], size, count - filecount,
                pmusic);
    }
    else
          puts(\"nil music\ \");
puts(\"Bye.\ \");
    fclose(pmusic);
    return 0;
}

LAB 4 CD Catalogue Assignment For this assessment you are asked to implement and test a program for cataloguing a collection of compact disks. Each compact disk
LAB 4 CD Catalogue Assignment For this assessment you are asked to implement and test a program for cataloguing a collection of compact disks. Each compact disk
LAB 4 CD Catalogue Assignment For this assessment you are asked to implement and test a program for cataloguing a collection of compact disks. Each compact disk

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site