-
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
-
>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:
>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
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
-
I thought you had a problem with unions as well? You have not even used a union?!?!?!?!?
-
That would be another question about how would I implement a union into this??
-
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!