Solved

Structures/Pointers/Functions

Posted on 2004-04-23
15
249 Views
Last Modified: 2010-04-15
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.
0
Comment
Question by:GJMill79
  • 7
  • 3
  • 3
  • +2
15 Comments
 
LVL 11

Expert Comment

by:PennGwyn
ID: 10905051
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.)

0
 

Author Comment

by:GJMill79
ID: 10905772
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.
0
 
LVL 11

Expert Comment

by:avizit
ID: 10905822
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/




0
 

Author Comment

by:GJMill79
ID: 10906091
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.
0
 
LVL 11

Expert Comment

by:avizit
ID: 10906153
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/
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10906164
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
};

0
 
LVL 9

Expert Comment

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

0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 5

Expert Comment

by:dennis_george
ID: 10906578
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
0
 

Author Comment

by:GJMill79
ID: 10907752
In creating the struct, how do I create it for CD length (in minutes and seconds, max 74 minutes and 60 seconds)?
0
 
LVL 11

Expert Comment

by:avizit
ID: 10910833
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/
0
 

Author Comment

by:GJMill79
ID: 10913108
Got it!  Thank you.
0
 

Author Comment

by:GJMill79
ID: 10954654
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?

0
 

Author Comment

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

Author Comment

by:GJMill79
ID: 10954836
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.
0
 
LVL 9

Accepted Solution

by:
ankuratvb earned 500 total points
ID: 10955176
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

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

705 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

18 Experts available now in Live!

Get 1:1 Help Now