Link to home
Start Free TrialLog in
Avatar of GJMill79
GJMill79

asked on

Structures/Pointers/Functions

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 and rewrite over any CD with new information.  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.
Avatar of PennGwyn
PennGwyn

And your question is ... ?

(If you want to handle an infinite number, you want a database.  It's more likely that your assigment is to be able to handle as many as the available memory will permit, rather than hard coding a specific number into the program.)

Avatar of GJMill79

ASKER

Correct...I need to allocate memory accordingly to user input.  I just need some general ideas on how to get started.  My ideas are to set up a menu using the switch command and storing the CD information in a structure while using functions to add, delete, and view the database.
To allocate memory you may like to use the malloc function


#include <stdlib.h>

void *malloc(size_t size);

malloc() allocates size bytes and returns a pointer to the allocated memory.  The memory is not cleared.

the value returned is a pointer to the allocated memory, which is suitably aligned for any kind of variable, or NULL if the request fails.

hope it answersyour query
/abhijit/




Need some ideas on how to get started on 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 and rewrite over any CD with new information.  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.
1. First create the struct
2. An infinite number of CD's shold be able to be entered :  let's just say you have to  store  large number of cds ,, my suggestion would be to use a linked list ( just to keep things simple )
3. To view all the entered data .. just go through the linked list and print out all the cds info
4. to enter new cds : prompt the user for data and read user input using scanf and fill the structure you had created in step 1 and add to the linked list

/abhijit/
Use a linked list.That way,you get infinite number as long as you have memory.

First you need to define your struct:

struct CD
{
//all your field here
struct CD *next;//to point to next node
};

Functions would be:

ViewList(struct CD *);
InsertNewCD(struct CD *);

For replaceCD,ask the user which CD to search for,Look for the CD if it exists,and then ask the user for all fields of the CD to replace.

hi ,

I will suggest you to use std::map<> This will store nearly infinte amount of structures (related to your CD) and very efficeint in accessing any structure by an index.

Dennis
In creating the struct, how do I create it for CD length (in minutes and seconds, max 74 minutes and 60 seconds)?
amongst other field  in the struct CD have

unsigned int mm ;
unsigned int ss;


validate the max 74 minutes thing whenever you store a new CD

/abhiijt/
Got it!  Thank you.
After creating the struct how do I access the elements in my struct to view the CD title, author and length all at the same time.  Or can they only be accessed separately?

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");

          }
     }
}      
Also, how do you delete something from a struct?  

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.
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