Thread: Unions and Structures

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    25

    Unions and Structures

    Hi everyone. I've been struggling a bit with some code using unions and structures. Right now, I'm just trying to get the following code to work the way I want it too. The way it SHOULD work is prompt the user for the type of record they want to enter...either 'b' for book or 'm' for CD. It will then go into the appropriate list of what needs to be enter for that record and place that information into an array....It needs to do other stuff also, but I think I can manage it....At least I think so. Anyways, here is the code that I have written...It does compile and run...But not the way I want it to. Any help or suggestions are greatly appreciated.
    Code:
    #include<stdio.h> 
    
    struct book 
    {
    char name[30];
    char title[30]; 
    char publisher[30]; 
    char year[4]; 
    float price; 
    char code[8];
    };
    
    struct cd
    {
    char ComposerName[25];
    char CDTitle[35]; 
    char interpr[40]; 
    char Publishedyear[4]; 
    float CDPrice; 
    char CatCode[8]; 
    };
    
    struct book booklist[3]; 
    struct cd CDlist[3]; 
    
    int main(){ 
    char ch;
    int i;
    
    printf("Please enter 'b' if you want to add a book record or 'm' to enter a CD record: "); 
    	ch = getche();
    	if (ch = 'b')
    	{ 
                    for(i=0;i<3;i++)
    	{ 
    	printf("Enter author's name: "); 
    	scanf("%s", booklist[i].name); 
    
    	printf("Enter the book title: "); 
    	scanf("%s\n", booklist[i].title); 
    
    	printf("Enter the publisher: "); 
    	scanf("%s\n", booklist[i].publisher); 
    
    	printf("Enter the year: "); 
    	scanf("%s\n", booklist[i].year); 
    
    	printf("Enter the price: "); 
    	scanf("%d\n", booklist[i].price); 
    
    	printf("Enter the book code: "); 
    	scanf("%s\n", booklist[i].code);
    	} 
    	}
    
    	if (ch = 'm')
    	{
    	for(i=0;i<3;i++)
    	{ 
    	printf("Enter composer's name: "); 
    	scanf("%s", CDlist[i].ComposerName); 
    
    	printf("Enter the CD title: "); 
    	scanf("%s\n", CDlist[i].CDTitle); 
    
    	printf("Enter the interpreter(s): "); 
    	scanf("%s\n", CDlist[i].interpr); 
    
    	printf("Enter the year it was published: "); 
    	scanf("%s\n", CDlist[i].Publishedyear); 
    
    	printf("Enter the price of the CD: "); 
    	scanf("%d\n", CDlist[i].CDPrice); 
    
    	printf("Enter the catalog code: "); 
    	scanf("%s\n", CDlist[i].CatCode);
    			
    	} 
    	}
    
    }
    Read this before posting code
    http://www.cprogramming.com/cboard/m...bbcode#buttons

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >char ch;
    This really should be int since character input functions actually return an int to account for EOF, which must not be representable by a char.

    >ch = getche();
    You forgot to include conio.h.

    >if (ch = 'b')
    Note the assignment operator instead of the equality operator. This is a common mistake, it should be:
    Code:
    if ( ch == 'b' )
    >if (ch = 'm')
    Same problem.

    >int main(){
    You declare main to return an int yet you don't return anything, it would be a good idea to place
    Code:
    return 0;
    at the end of main, if only to satisfy those pesky standards committees.

    >scanf("%s", booklist[i].name);
    Two problems, scanf is horrible for user input and should be avoided if you know how to use more suitable methods, and you should always check the return value of input functions. Since input is the most likely thing to be corrupted or invalid, this is always a good idea.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Black Mage Extraordinaire VegasSte's Avatar
    Join Date
    Oct 2002
    Posts
    167
    I thought you had a problem with unions as well? You have not even used a union?!?!?!?!?

  4. #4
    Registered User
    Join Date
    Mar 2003
    Posts
    25
    That would be another question about how would I implement a union into this??

  5. #5
    Black Mage Extraordinaire VegasSte's Avatar
    Join Date
    Oct 2002
    Posts
    167
    Unions must have common field(s) which allow you to select which of the structures housed inside to use, so:
    Code:
    struct book 
    {
    char type;
    char name[30];
    char title[30]; 
    char publisher[30]; 
    char year[4]; 
    float price; 
    char code[8];
    };
    
    struct cd
    {
    char type;
    char name[30];
    char CDTitle[35]; 
    char interpr[40]; 
    char Publishedyear[4]; 
    float CDPrice; 
    char CatCode[8]; 
    };
    union collection{
        struct book b;
        struct cd c;
    };
    
    int main()
    {
         union collection record;
    
        /*accept input reading the type as b or c straight into the union like this*/
         printf("Please enter record type:(b)ook or (c)d?");
         scanf("%c", &record.b.type);
    /*it does not matter what structure you read this into as they share the same location in memory, you then just check the type and read the input into the appropriate structure from this point on!*/
    ...
    return 0;
    Hope this helps!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 09-21-2008, 04:18 PM
  2. Structures, Unions and Classes
    By Makoy in forum C++ Programming
    Replies: 2
    Last Post: 02-23-2004, 02:57 PM
  3. Structures - Unions
    By AProg in forum C Programming
    Replies: 16
    Last Post: 05-27-2003, 12:43 AM
  4. Structures and unions
    By Paninaro in forum C Programming
    Replies: 6
    Last Post: 06-21-2002, 01:35 PM
  5. Replies: 7
    Last Post: 12-29-2001, 11:25 PM