Structure Array searching

This is a discussion on Structure Array searching within the C Programming forums, part of the General Programming Boards category; I always facing problem when deal with array......>_< mm.... I know there is something wrong in my program..but I don't ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    12

    Question Structure Array searching

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




  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    12

    Smile

    sorry...I press the "post" button to fast...

    the problem actually was in - book_record *search (char isbn[]) function.

    It always display the first book record information to me. why??

  3. #3
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    Originally posted by ling
    sorry...I press the "post" button to fast...

    the problem actually was in - book_record *search (char isbn[]) function.

    It always display the first book record information to me. why??
    if (isbn == book_array[i].ISBN)

    isbn is a string and book_array[i].ISBN is also a string , to compare strings use

    if(strcmp(isbn,book_array[i].ISBN) == 0)

    /*
    int strcmp(const char* cs, const char* ct);
    Compares cs with ct, returning negative value if cs<ct, zero if cs and ct are the same, positive value if cs>ct.
    */

    Did this work ?
    scanf ("%s", &book_array[i].ISBN);

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    12

    Lightbulb

    >Did this work ?
    >scanf ("%s", &book_array[i].ISBN);


    sorry................ I should take out the '&'.......

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,749
    > if (isbn == book_array[i].ISBN){
    Use strcmp for comparing strings

    Code:
    if ( strcmp(isbn,book_array[i].ISBN) == 0 ){
        // same
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 01:13 AM
  2. pointer to an array in a structure
    By samual_van in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2005, 11:51 AM
  3. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 01:04 PM
  5. Moving to the next structure array
    By mattz in forum C Programming
    Replies: 2
    Last Post: 11-30-2001, 03:43 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21