Solved

Create a CD Database in C programming

Posted on 2004-04-29
33
337 Views
Last Modified: 2010-08-05
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");

          }
     }
}      
0
Comment
Question by:GJMill79
  • 9
  • 8
  • 4
  • +5
33 Comments
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10955185
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.

0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 10956278
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
0
 
LVL 24

Expert Comment

by:fridom
ID: 10956297
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
0
 
LVL 2

Expert Comment

by:Avik77
ID: 10956554
If u r in Linux u can exploit the dbm database library to do the job.

Avik.
0
 
LVL 4

Expert Comment

by:PerryDK
ID: 10956934
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.
0
 
LVL 4

Expert Comment

by:PerryDK
ID: 10956954
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.
0
 
LVL 24

Expert Comment

by:fridom
ID: 10957031
Well switching to C++ is not really an advance, of course YMMV

Friedrich
0
 
LVL 2

Expert Comment

by:anupvijay
ID: 10957032
>All structs should be passed by reference, not by value.

I did not know C had pass by reference :-)
0
 
LVL 4

Expert Comment

by:PerryDK
ID: 10957100
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.
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 10957168
Hi GJMill79,

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

sunnycoder
0
 
LVL 4

Expert Comment

by:PerryDK
ID: 10957197
This is entirely off subject :)  I find it very odd that Java does not support pass by reference...anyone know why?
0
 
LVL 2

Expert Comment

by:anupvijay
ID: 10957281
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.
0
 
LVL 24

Expert Comment

by:fridom
ID: 10959252
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
0
 

Author Comment

by:GJMill79
ID: 10960004
The is program is for homework in a C programming course.  Everything must be in C.  
0
 

Author Comment

by:GJMill79
ID: 10960133
The first comment should show the outline I have thus far, I need some help in developing my program further.
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10960158
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.
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 9

Expert Comment

by:ankuratvb
ID: 10960905
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.
0
 
LVL 2

Expert Comment

by:Avik77
ID: 10962450
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.
0
 

Author Comment

by:GJMill79
ID: 10963132
Got any idea where I can get a copy of it by next Friday?  
0
 
LVL 2

Expert Comment

by:Avik77
ID: 10963467
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.

0
 

Author Comment

by:GJMill79
ID: 10965030
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.

0
 

Author Comment

by:GJMill79
ID: 10965041
Was that for the insertCD function?
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10965455
>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");
0
 
LVL 5

Expert Comment

by:crazycomputers
ID: 10967376
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.
0
 

Author Comment

by:GJMill79
ID: 10969372
Just read it...thank you.--I failed to read this to tell the truth.  I basically just want guidance.
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10970049
Hi GJMill79,

So what is your progress?
0
 

Author Comment

by:GJMill79
ID: 10973446
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)

0
 

Author Comment

by:GJMill79
ID: 10973472
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?  
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10974270
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.
0
 

Author Comment

by:GJMill79
ID: 10982067
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;

}


0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 10983760
>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 =
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10984805
You're probably missing a semi-colon on the line above line 28.
0
 
LVL 9

Accepted Solution

by:
ankuratvb earned 500 total points
ID: 10984844
 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)//this is an error.either only else or else if(i==3)
     plus->sec=new;
     i=0;

I dont quite understand.When you insert a new CD,you have to set all its values.No?
You're making plus's various fields point to the new node.plus is also not allocated.

After you've allocated mem. for node *new;
set its values directly.
strcpy(new->author,"Author#1");
strcpy(new->title,"Title#1");
new->min=31;
new->sec=21;

Or you can get these values from the user.Ultimately its the user who'll be entering the values.

scanf("%s",new->author);
scanf("%s",new->title);
scanf("%d",&new->min);
scanf("%d",&new->sec);

then insert this new node in the list.
To insert at the head of the list,do:
Initially start=NULL;

new->next=start;
start=new;

For the first node,this will set new->next=NULL; and start to point to this node.

Next time you insert,the newly allocated node new's next pointer will point to the old start node and the start node will be updated to point to the new node.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now