I always facing problem when deal with array......>_<
mm.... I know there is something wrong in my program..but I don't know where is it? Can anyone modify for me?? thank you very much.
*****************code*****************************
# include <stdio.h>
struct data {
int day;
int month;
int year;
};
typedef struct {
char ISBN[10];
char title[30];
char author[30];
char category[2];
float price;
struct data published_date;
}book_record;
book_record book_array[2]; //book record structure array declaration
int menu(void);
void add_book (void);
void display_book (void);
book_record *search (char[]);
int num=0, i, menu_choice; //global variables declaration
char isbn[10];
int main(){
menu_choice = menu();
do{
switch (menu_choice){
case 1: //add book record into array
add_book();
i=2;
menu();
break;
case 2 :
if (i==2) //check whether the data had been entered
display_book();
else
printf ("\n\tDisplay Error! The book record is empty!\n");
menu();
break;
case 3:
printf ("Enter the ISBN of the book you want to find: ");
scanf (" %s", isbn);
while (getchar() != '\n');
book_array[i] = *search (isbn);
printf ("\nBook Record %d\n", i);
printf ("~~~~~~~~~~~~~~~\n");
printf ("\nISBN : %s\n", book_array[i].ISBN);
printf ("Book Title : %s\n", book_array[i].title);
printf ("Author : %s\n", book_array[i].author);
printf ("Category : %s\n",book_array[i].category);
printf ("Price : RM %.2f\n", book_array[i].price);
printf ("Published date : %d/%d/%d\n",book_array[i].published_date.day,
book_array[i].published_date.month, book_array[i].published_date.year);
menu();
break;
}
}while (menu_choice!=4);
printf ("\n\tYou are choosing to exit....\n");
printf ("Program Terminate!\n");
return 0;
}
int menu(void){
printf ("\n\n\t****************************************** **********\n");
printf ("\t1. Add book record\n");
printf ("\t2. Display all book records\n");
printf ("\t3. Find and display book information given the ISBN\n");
printf ("\t4. Exit program\n");
printf ("\t********************************************** ******\n");
printf ("\n\tYour choice : ");
scanf ("%d", &menu_choice);
return (menu_choice);
}
void add_book (void){
for (i=0; i<2; i++){
printf ("\nBook Record %d\n", i+1);
printf ("~~~~~~~~~~~~~~~\n");
printf ("ISBN (Max.10): ");
scanf ("%s", &book_array[i].ISBN);
while (getchar() != '\n');
printf ("Book Title (Max.30): ");
scanf (" %[^\n]", &book_array[i].title);
while (getchar() != '\n');
printf ("Author (Max.30): ");
scanf (" %[^\n]", &book_array[i].author);
while (getchar() != '\n');
printf ("Category (SF-Science Fiction, HR-Horror, RM-Romance): ");
scanf (" %s", &book_array[i].category);
printf ("Price : RM");
scanf ("%f", &book_array[i].price);
printf ("Published date (dd/mm/yy): ");
scanf ("%d/%d/%d", &book_array[i].published_date.day,
&book_array[i].published_date.month,
&book_array[i].published_date.year);
}
}
void display_book (void){
for (i=0; i<2; i++)
{
printf ("\nBook Record %d\n", i+1);
printf ("~~~~~~~~~~~~~~~\n");
printf ("\nISBN : %s\n", book_array[i].ISBN);
printf ("Book Title : %s\n", book_array[i].title);
printf ("Author : %s\n", book_array[i].author);
printf ("Category : %s\n",book_array[i].category);
printf ("Price : RM %.2f\n", book_array[i].price);
printf ("Published date : %d/%d/%d\n",book_array[i].published_date.day,
book_array[i].published_date.month,
book_array[i].published_date.year);
}
}
book_record *search (char isbn[]){
for (i=0; i<2; i++){
if (isbn == book_array[i].ISBN){
break;
return (book_array);
}
}
return (book_array);
}
************************end*********************