Link to home
Start Free TrialLog in
Avatar of GJMill79
GJMill79

asked on

Create a CD Database in C programming

Need help in writing a program that uses structs to create and manage a simple database of information about CDs.  

The struct should have information about the CD title (max length 20), CD author (max length 20), and CD length (in minutes and seconds, max 74 minutes and 60 seconds).

 An infinite number of CD's should be able to be entered.  I need the option to  view all CDs that have been entered, enter new CDs, rewrite over any CD with new information, and delete a CD from database.  All of these things need to be done with functions (i.e. a function for entering in a new CD, viewing CDs.)  

All structs should be passed by reference, not by value.

In addition to the above I need to implement saving to a file and loading from a file.  The user should not have the option to choose the filename that the data should be saved in-use cd.dat.  The program should automatically load the file when run, but the user should have a menu option to save and another menu option to reload the file.


This is an outline of my program thus far...it has not been compiled yet.  

Please offer any suggestions...

#include <stdio.h>

struct CD
{
     char title[20];
     char author[20];
     int min;
     int sec;
}struct CD *content

void ViewCDs(struct CD list*)
{
     printf("%s", list->title);
     printf("%s", list->author);
     printf("%d", list->min);
     printf("%d", list->sec);
}

void InsertNewCD(struct CD new*)
{
   new->title
   new->author
   new->min
   new->sec
}


RewriteCD(struct CD*)
{

}


DeleteCD(struct CD*)
{

}

int main()

     int entry;
     CD *view;



     while(1)
     {
          printf("1.  VIEW CD LIST\n");
          printf("2.  ENTER NEW CD\n");
          printf("3.  UPDATE CD INFO\n);
          printf("4.  DELETE CD\n");
          printf("5.  EXIT\n");

          printf("Choose a number from the menu above.\n");
          fgets(buffer, sizeof(buffer), stdin);
          sscanf(buffer, "%d", &entry);

          switch(entry)
          {
               entry==0;

               case 1:

                    view=&content
                    ViewCDs(&view)
                    break;

               case 2:

               case 3:

               case 4:
               
               case 5:
                       return(0);
                       break;

               default:
                    printf("Choose an option from the men!\n");

          }
     }
}      
Avatar of ankuratvb
ankuratvb
Flag of United States of America image

Repeating my comment in your other thread on the same topic:

struct CD
{
     char title[20];
     char author[20];
     int min;
     int sec;
//where's the next pointer
//something like
     struct CD *next;
}*content;//this should be like this

void ViewCDs(struct CD list*)
{//this prints only one cd info.
 //to print all of them,you need to loop thru the nodes till you get end of list(NULL in next pointer)
     printf("%s", list->title);
     printf("%s", list->author);
     printf("%d", list->min);
     printf("%d", list->sec);
}

void InsertNewCD(struct CD new*)
{
//you need to allocate memory for the new node and set the next pointer to NULL.
   new->title
   new->author
   new->min
   new->sec
}


RewriteCD(struct CD*)
{

}


DeleteCD(struct CD*)
{

}

int main()

     int entry;
     CD *view;



     while(1)
     {
          printf("1.  VIEW CD LIST\n");
          printf("2.  ENTER NEW CD\n");
          printf("3.  UPDATE CD INFO\n);
          printf("4.  DELETE CD\n");
          printf("5.  EXIT\n");

          printf("Choose a number from the menu above.\n");
          fgets(buffer, sizeof(buffer), stdin);
          sscanf(buffer, "%d", &entry);

          switch(entry)
          {
               entry==0; //what is this for??

               case 1:

                    view=&content;
                    ViewCDs(&view);
                    break;

               case 2:

               case 3:

               case 4:
               
               case 5:
                       return(0);
                       break;

               default:
                    printf("Choose an option from the men!\n");

          }
     }
}      

I'll suggest you brief up on your linked lists concepts again.
and take it one step at a time.First get insertion and display working,delete will follow easily.

Avatar of sunnycoder
Hi GJMill79,

1. Is this homework or a real application that you are trying to build ... If it is a real application, then I believe you have much better alternatives for doing this kind of stuff and you should consider them.

If you still insist on using C, I would suggest a better data structure such as a B tree which would permit multiway search and hence will reduce your search time ... Incase of linked list with "infinite" record, the search time can be painfully high.

2. You did not mention what part you need help with ? I mean, what is the question ?

cheers
sunnycoder
It smells very much like homework. If it weren't it would be a good idea to choose the right tools for the job. Of course one could write it from scratch in C, but using the proper tools like Berkeley DB or some free Database would be  the way I would take. Well nothing in the question suggests that he has thought about persitence. So how would he keep his data?

The from ankurtavb point into the right direction.

Friedrich
If u r in Linux u can exploit the dbm database library to do the job.

Avik.
Avatar of PerryDK
PerryDK

are you restricted to simply using C code?  If you can use C++ I could provide you with the entire program that does what you want in about 30 mins...with the use of classes and the STL containers such as std::list or std::vector or a std::map.

if this isn't homework and you will accept an answer in C++ let me know and i'll write the solution.
If this isn't homework and you truly have an unlimited amount of cd's I wouldn't use a linked list.  I'd use a map or a hash table...at the bare minimum a a tree structure to hold your cd data.  

If this is homework I really can't imagine that your professor wouldn't introduce you to C++ and classes instead of C structs.  Are profesors still teaching C.  I would think that they would have switched to c++ or java by now...although I could be wrong.
Well switching to C++ is not really an advance, of course YMMV

Friedrich
>All structs should be passed by reference, not by value.

I did not know C had pass by reference :-)
fridom,

What does YMMV mean or stand for?

The only reason I suggest c++ over c in this particluar istance is that you could use the STL containers and you could easily sort your database for display by cd title, author, length, etc.  It also provides an easier way of saving and loading stuff from a file...ostream and istream.  This project would easily take me 2 to 3 hours coding in c...i believe I could do it in 30 mins to an hour and half if using c++.

In regards to your C your ? "I did not know C had pass by reference :-)"
C does not support passing by reference...to pass by reference you'd have to do something like this

void passByReference(int* ptrInt)
{
  *ptrInt = 5;
}

int main()
{
  int x = 7;
  passByReference(&x);
}

Its not really passing by reference but it achieves the same results.
Hi GJMill79,

If you can provide some feedback, may be we can start working towards a solution.

sunnycoder
This is entirely off subject :)  I find it very odd that Java does not support pass by reference...anyone know why?
Hi PerryDK,

I was just trying to make some humour :-) . Although thanks for the answer :-)

Sorry to all the posters for the following statements relating to Java
As far as Java is concerned (although i have seldom used it) it boasts of being purely OO (i know it isn't).
So passing anything by reference and allowing someone else to modify it is entirely against the philosophy of OO.
So java demands you to do all operations or modifications using an object and hence pass an object.
that is what OO is all about ain't it ?

Enjoy the discussion here if you are really keen to look more deep :-)

http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&threadm=MPG.1728b6f13e6f2bd989f08%40news.altopia.com&rnum=3&prev=/groups%3Fq%3Dpass%2Bby%2Breference%2Bjava%26ie%3DUTF-8%26oe%3DUTF-8%26hl%3Den


Cheers.
YMMV Your-mileage-may-vary

I can think of other tools then C for doing that job. But C++ would not be what I would choose.
Friedrich
Avatar of GJMill79

ASKER

The is program is for homework in a C programming course.  Everything must be in C.  
The first comment should show the outline I have thus far, I need some help in developing my program further.
Adding to what you have already done,

struct CD
{
    char title[20];
    char author[20];
    int min;
    int sec;
    //where's the next pointer
    //something like
    struct CD *next;
}*start;//start pointer

Initially set start=NULL.

When you want to insert a node,
for the first node,allocate memory for a new node and make the start pointer point to that node.

start=(struct CD *)malloc(sizeof(struct CD));
i.e. allocate memory the size of the structure,cast it and make it point to the start node.
set its fields to the data values.
start->min=32;
//similarly for other fields also.

start->next=NULL;

Next time around,for inserting,you'd do:
start->next=(struct CD *)malloc(sizeof(struct CD));
so that the new node is pointed to by the previous node's next pointer.
For displaying the entire list,think what is different in the last node compared to the other nodes.

The last node is the only one whose next pointer would be NULL.

So,run a loop,traversing the list,displaying each node until current node's next pointer is NULL.

Try to figure out how you'd traverse the list changing the current node to the next one after displaying its contents.
Hi GJMill79,

I can refer u to a good book where this topic is dealt with a great detail....
Beginning Linux Programming (second edition)
by
Neil Matthew and Richard Stones
[wrox publication]

The CDDB is used as an example to cover different aspects of C programming in Linux.

Avik.
Got any idea where I can get a copy of it by next Friday?  
U can get it from:
http://www.web-site-hosting-n-tools.com/cgi-bin/web_hosting-item_id-0764543733-search_type-AsinSearch-locale-us.html
But not for free :)
Any way the structure for ur CD database used in the book is somewhat like this....

#define CAT_CAT_LEN 30                 // catalogue length
#define CAT_TITLE_LENGTH 70        // title length
#define CAT_TYPE_LENGTH 30
#define CAT_ARTIST_LENGTH 70

typedef struct {
 char catalog[CAT_CAT_LENGTH + 1];
 char title[CAT_TITLE_LENGTH + 1];
 char type[CAT_TYPE_LENGTH + 1];
 char artist[CAT_ARTIST_LEN + 1];
} cdc_entry;

/* The tracks table, one entry per track */
#define TRACK_CAT_LENGTH CAT_CAT_LENGTH
#define TRACK_TTEXT_LEN 70

typedef struct {
 char catalog[TRACK_CAT_LENGTH + 1];
 int track_no;
 char track_txt[TRACK_TTEXT_LEN + 1];
}cdt_entry;

This is the data structure definition of ur CDDB. It defines structures and sizes for the 2 tables that comprises the database.
1. The catalog entry.
2. The track entry.

Since it is ur homework, I can't give the complete code but I think u r smart enough to figure out and utilize the CDDB data stucture and also add ur required fields...

Hope that helps.
Avik.

start=(struct CD *)malloc(sizeof(struct CD));
i.e. allocate memory the size of the structure,cast it and make it point to the start node.
set its fields to the data values. <---
start->min=32;              <----                  -----------------------What do you mean here?
//similarly for other fields also.

Was that for the insertCD function?
>Was that for the insertCD function?
Yes.

See,when you have allocated memory for the node,it allocates memory the size of your structure.

The cd information that you'll be entering for this node will have to be entered.
You enter it like:
start->min=32;

min is a field in your structure,so set its value=32(just an arbitrary value for the no. of minutes).

Similarly,for other fields:

start->sec=11;
strcpy(start->title,"Title1");
Am I the only one who missed the author's comment?

> The is program is for homework in a C programming course.  Everything must be in C.

There you have it.  This is homework.  GJMill79, I suggest you read the EE member agreement again.
Just read it...thank you.--I failed to read this to tell the truth.  I basically just want guidance.
Hi GJMill79,

So what is your progress?
This is what I'm working on so far...

#include <stdio.h>

struct CD
{
     char title[20];
     char author[20];
     int min;
     int sec;
     struct CD *next;
}struct CD *start;

void ViewCDs(struct CD list*)
{
     while (list!=NULL)
     {
          printf("%s", list->title);
          printf("%s", list->author);
          printf("%d", list->min);
          printf("%d", list->sec);


void InsertNewCD(struct CD new*)
{
   start=(struct CD*)malloc(sizeof(struct CD));

   for(i=0; i<3, i++)
      if (InsertNewCD(struct CD new*))                    -----> still trying to work this
                                                                                    entire function out
void InsertNewCD(struct CD new*)                                  
{
   start=(struct CD*)malloc(sizeof(struct CD));

   for(i=0; i<3, i++)
      if (InsertNewCD(struct CD new*))

      if (i=0)
         start->title=new;
      else if (i=1)
         start->author=new;
      else if (i=2)
        start->min=new;
      else if (i=3)e

        start->sec=new;
}

RewriteCD(struct CD*)
{

}


DeleteCD(struct CD*)
{

}


down in int main( )
 case 2:

                    printf("Enter title of CD:  \n");
                    fgets(buffer, sizeof(buffer), stdin);
                    sscanf(buffer, "%s", &add);

                    InsertNewCD(&add);

                    printf ("Enter name of artist:  \n");
                    fgets(buffer, sizeof(buffer), stdin);

                    printf ("Enter name of artist:  \n");
                    fgets(buffer, sizeof(buffer), stdin);
                    sscanf(buffer, "%s", &auth);

                    InsertNewCD(&add)

I just revised the InsertNewCD function to :

void InsertNewCD(struct CD new*)
{  int i=0
   start=(struct CD*)malloc(sizeof(struct CD));

   if (i=0)
     start->title=new;
     i++;
   else if (i=1)
     start->author=new;
     i++;
   else if (i=2)
     i++;
   start->min=new;
     i++;
   else if (i=3)
     start->sec=new;
     i=0;
}

Why do I need to set start = to NULL?  
First of all,

void ViewCDs(struct CD list*)
{
    while (list!=NULL)
    {
         printf("%s", list->title);
         printf("%s", list->author);
         printf("%d", list->min);
         printf("%d", list->sec);
          list=list->next;//make it point to the next node otherwise it will run in an infinite loop printing the same values again and again
    }
}

Keep trying on the insert function.BTW,in if,for checking for equality,u have to use ==
if(i==1)


Initially,when your list doesnt have any nodes,start pointer would be NULL,wouldnt it?
When you insert a node,set start to point to the first node.
Then,for all subsequent nodes,allocate memory, and set the last node's next pointer to point to this newly allocated node.

Dont set the start pointer everytime,otherwise how will you traverse the list if you dont know from where the list starts.
At compile time I'm getting an error message that reads:

"Program6.c", line 28.9: 1506-276 (S) Syntax error: possible missing ':'?
"Program6.c", line 31.7: 1506-045 (S) Undeclared identifier plus.

Please provide some guidance...

Function for adding a new CD to database:

void InsertNewCD(node *new)
{  int i=0;
   new=(node*)malloc(sizeof(node));
   node *plus;

   if (i=0)
   {  plus->title=new;
      i++;
   }
   else if (i=1)
   {  plus->author=new;
      i++;
   }
   else if (i=2)
   {  plus->min=new;
      i++;  
   }
   else (i=3)
      plus->sec=new;
      i=0;

}


>At compile time I'm getting an error message that reads:

>"Program6.c", line 28.9: 1506-276 (S) Syntax error: possible missing ':'?
>"Program6.c", line 31.7: 1506-045 (S) Undeclared identifier plus.

>void InsertNewCD(node *new)
>{  int i=0;
>   new=(node*)malloc(sizeof(node));
>   node *plus;

C requires all declarations in a block to precede any statement

reverse the order of malloc statement and node * plus

Can you point out which line is number 28 ?

Also in all ifs
> if (i=0)
you perhaps meant == in place of =
You're probably missing a semi-colon on the line above line 28.
ASKER CERTIFIED SOLUTION
Avatar of ankuratvb
ankuratvb
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial