working with structures

This is a discussion on working with structures within the C Programming forums, part of the General Programming Boards category; I'm writing a program that asks the user to enter information about a book or series of books (title, author, ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    39

    Cool working with structures

    I'm writing a program that asks the user to enter information about a book or series of books (title, author, year, version and price) which are store inside a structure. So this is my structure
    Code:
    typedef struct {
    char subject[MAXIMUM_TITLE];
    char author[MAXIMUM_AUTHOR];
    int year;
    int edition;
    float price;
    } book;
    I'm using it inside a function to enter the data like this
    Code:
    book getbookinfo(void){
    
         printf("enter the name of the subject: ");
         scanf("%c", &book.subject);
    
         printf("enter the author: ");
         scanf("%c", &book.author);
    
         printf("enter the year of the book: ");
         scanf("%d", &book.year);
    
         printf("enter the edition you want");
         scanf("%d", &book.edition);
    
         printf("enter the price for the book: ");
         scanf("%f", &book.price);
    
     return book;
    
    }
    when I compile it I receive some errors

    Code:
    nventory.c: In function ‘getbookinfo’:
    inventory.c:8: error: expected expression before ‘book’
    inventory.c:11: error: expected expression before ‘book’
    inventory.c:14: error: expected expression before ‘book’
    inventory.c:17: error: expected expression before ‘book’
    inventory.c:20: error: expected expression before ‘book’
    inventory.c:22: error: expected expression before ‘book’
    I don't understand why
    Also I have another function that will look for a book based on either the year of the book or the price entered by the user.

    Code:
    searchresult searchbook(book inventory[MAXIMUM_BOOKS], int size, int option, int value){
     28 
     29    int i, j, matches;
     30 
     31    if(option==0){
     32         for(i=0; i<size;i++){
     33            if(value==inventory[i].year){
     34                 searchresult.booksfound[i] = inventory[i];
     35                 searchresult.searchmatches = i;
     36           }
     37       }
     38   }
    inventory[] is just the values returned by the first function, size is the number of books, option is the option entered by the user (year or price) and the value to be searched. I have another structure for 'searchresult'

    with this part I get more errors
    Code:
    inventory.c:34: error: expected identifier or ‘(’ before ‘.’ token
    inventory.c:35: error: expected identifier or ‘(’ before ‘.’ token
    I don't understand why I'm getting these errors, what am I doing wrong?
    I would like some help on this
    thanks in advance

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,533
    You're using type names where you should be using variables.
    Code:
    book getbookinfo(void){
         book theResult;
         printf("enter the name of the subject: ");
         scanf("%c", &theResult.subject);
    
         printf("enter the author: ");
         scanf("%c", &theResult.author);
    
         printf("enter the year of the book: ");
         scanf("%d", &theResult.year);
    
         printf("enter the edition you want");
         scanf("%d", &theResult.edition);
    
         printf("enter the price for the book: ");
         scanf("%f", &theResult.price);
    
     return theResult;
    
    }
    You'll also need to fix your scanf calls to
    - read more than one character
    - read titles containing spaces.
    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.

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    39
    Thanks, I was able to fix that issue but now I have a run-time error. Every time I run it, it doesn't work.
    Also about entering titles with spaces, I don't have to do that as the title should only contain one word with up to 20 characters.

    So I have the following function and then pass it to a structure array in main. And that's where the problem is, it skips through the steps and doesn't let me enter all the data.

    Code:
    book getbookinfo(void){
    
         printf("enter the name of the subject: ");
         scanf("%c\n", &inventory.subject);
         printf("enter the author: ");
         scanf("%c\n", &inventory.author);
         printf("enter the year of the book: ");
         scanf("%d", &inventory.year);
         printf("enter the edition you want");
         scanf("%d", &inventory.edition);
         printf("enter the price for the book: ");
         scanf("%f", &inventory.price);
    
     return inventory;
    
    }
    
    //this is where I pass it to main()
    
    for(i=0; i<quantity; i++){    //quantity = the # of books
            inventory[i]= getbookinfo();     
          }
    inventory[] is an array of structure of the same type as inventory

    this is what happens when I run it.
    Code:
    welcome to your virtual book inventory
    you can enter up to 10 books. The name of titles and authors should be only one word each and not to exceed 20 characters
    enter the number of books you want: 2
    now enter the information for your books
    enter the name of the subject: math
    enter the author: enter the year of the book: enter the edition you wantenter the price for the book: enter the name of the subject: enter the author: enter the year of the book: enter the edition you wantenter the price for the book: you have 2 options to search for a book, 0 to search for year value and 1 to search for price of the book
    enter your option: you entered an invalid search option
    try again
    [terminal_prompt]
    after I enter the title, it skips through everything else and terminates

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,533
    What's missing on your line 2, that is present in my line 2?
    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.

  5. #5
    Registered User
    Join Date
    Feb 2011
    Posts
    39
    the declaration of the structure.
    I declared it global, sorry I didn't include it up there.
    I moved it inside my function but I get the same error

  6. #6
    Registered User
    Join Date
    Feb 2011
    Posts
    39
    I found my mistake
    I was using %c instead of %s
    that's what gave me the error
    thanks though

  7. #7
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Here's what's happening...

    You enter : math

    scanf reads the m and put's in subject, reads a and put's in the author, reads t but format says %d so scanf fails and t is left in buffer, reads t again and same problem, reads t again and format is %f so fails again.

    %c tells scanf to read a single character.

    %s reads a string.

    Consider fgets().

    Also see faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1044873249&id=1043284392

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by torquemada View Post
    I found my mistake
    I was using %c instead of %s
    that's what gave me the error
    thanks though
    The line should read...
    Code:
    scanf("%s", &inventroy.author);
    That will read one word...

    If you use fgets() it will read up to the enter you press... but you will have to remove the \n from the string.

  9. #9
    Registered User
    Join Date
    Feb 2011
    Posts
    39
    thanks,

    So I'm able to enter the data now.

    I pass that structure returned to another structure in main(), the I pass that information to another function. The user has to look up a book based on some information. Then I have to returned the matches trough another structure and then pass them to a function to display the data like this.

    this is my function
    Code:
    searchresult searchbook(book inventory[MAXIMUM_BOOKS], int size, int option, int value){
    
    searchresult result;  //structure of type searchresult to hold books found
    
       int i;
    
       if(option==0){
            for(i=0; i<size;i++){
               if(value==inventory[i].year){
                    result.booksfound[i] = inventory[i];
                    result.searchmatches = i;
              }
          }
      }
    
    
    if(option==1){
            for(i=0; i<size; i++){
               if(value<=inventory[i].price){
                    result.booksfound[i] = inventory[i];
                    result.searchmatches = i;
                  }
             }
       }
    
    
    return result;
    
    }
    
     void displayresult(book display[MAXIMUM_BOOKS], int matches){
        int i;
        printf("there are %d books found\n", matches);
    
        for(i=0; i<matches; i++){
            printf("%d\n", display[i].subject);
    
       }
    }
    //the for loop should have more information but I only have one line right now just to test.
    I have defined this structure
    Code:
    typedef struct {
    book booksfound[MAXIMUM_BOOKS];
    int searchmatches;
    } searchresult;
    this is in main()
    Code:
    searchresult search;
    
    search = searchbook(inventory, quantity, option, value);
    int matches = search.searchmatches
    printf("matches:%d\n ", matches);
     for(i=0; i<matches; i++){
       inventory[i] = search.booksfound[i];
      }
    
     displayresult(inventory, matches);

    So when I run it this is what I get

    Code:
    enter the number of books you want: 3
    now enter the information for your books
    enter the name of the subject: math
    enter the author: me
    enter the year of the book: 2001
    enter the edition of the book:1
    enter the price for the book: 10
    enter the name of the subject: math
    enter the author: you
    enter the year of the book: 2001
    enter the edition of the book:1
    enter the price for the book: 99
    enter the name of the subject: math
    enter the author: him
    enter the year of the book: 2001
    enter the edition of the book:1
    enter the price for the book: 99
    you have 2 options to search for a book, 0 to search for year value and 1 to search for price of the book
    enter your option: 0
    enter the year value you want to search: 2001
    matches:2
     there are 2 books found
    -1079421420
    -1079421368
    the number of matches is wrong and the information is not being passed to the display function. What am I doing wrong?
    Thanks for your help

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    I would not do it that way at all...

    You should ask for your search criteria... 1 = name, 2 = author, 3 = year, 4 = price...

    The branch to a individual functions that will read your inventory list in a loop...
    Code:
    int NameSearch(int last, char* name)
      { // seek to the last position
         // search forward
         // return index if found
         // return -1 if loop expires }
    Each time the function returns you can list the book on the screen then continue the search from where you left off.

    Trying to return arrays of structs is going to get way the heck and gone too strange.

  11. #11
    Registered User
    Join Date
    Feb 2011
    Posts
    39
    well, that sounds easier but it's not the point of the assignment.
    I have to be able to return an array of structures. can you tell me where is my main error in the search function?

    thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help working with array of structures!
    By black_stallion in forum C Programming
    Replies: 16
    Last Post: 09-11-2011, 10:23 AM
  2. Help working with functions, structures, and arrays
    By leway in forum C Programming
    Replies: 3
    Last Post: 05-09-2010, 05:24 PM
  3. working with pointers to structures
    By Delia in forum C Programming
    Replies: 2
    Last Post: 03-21-2010, 05:23 PM
  4. Working with arrays of structures, need advice...
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 08-14-2002, 08:38 AM
  5. Replies: 5
    Last Post: 04-11-2002, 11:29 AM

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