Thread: displaying data from a structure of arrays

  1. #1
    Unregistered
    Guest

    displaying data from a structure of arrays

    the user has entered data, and i am wanting to print off specific parts of the data. The user is to enter a catagory, and all the videos in this catagory to be printed off. I cant figure out what is wrong with this code, can anyone help?

    printf("Enter the catagory of video you would like displayed (Kids,Action,Romance,Thriller) - ");
    fflush(stdin);
    scanf("%s", Selection);
    fflush(stdin);

    printf("\n\n");

    printf("Number Title Certificate Catagory Daily Rental Available\n\n");

    for (j = 0; j < i; j++)
    if (0 != (strstr(((VideoData[j].videotitle)[j])=VideoData[j].videotitle, Selection)))
    {
    PrintOne((VideoData[j].videotitle)[j]);
    }

    printf("\n\nTo return to the main menu press any key ");
    getch();

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >printf("Enter the catagory of video you would like displayed(Kids,Action,Romance,Thriller) - ");
    >fflush(stdin);
    >scanf("%s", Selection);
    >fflush(stdin);
    This is really not a good idea. Using fflush on input streams results in undefined behavior. If you want to get rid of floating characters after calls to scanf replace fflush ( stdin ); with

    while ( getchar() != '\n' );

    every time you use scanf to read string data. You could avoid this problem altogether by using fgets when reading string data and scanf only when reading numeric data.

    >if (0 != (strstr(((VideoData[j].videotitle)[j])=VideoData[j].videotitle, Selection)))
    This is just plain ugly, if your structure is formatted with any sanity at all then you should simply be able to do something like this:
    Code:
    /* Add an enum to make the mode easier to understand 
    */
    enum {CATEGORY, TITLE, and so on };
    
    /* Use a switch to dermine how to test the struct 
    */
    switch ( modeSelection ) {
    case CATEGORY:
      if ( strcmp ( VideoData[j].category, Selection ) == 0 )
        PrintOne ( VideoData[j], modeSelection );
      break;
    .
    .
    }
    
    /* The definition of PrintOne would look something like this.
    ** Using another switch to determine what to print. 
    */
    void PrintOne ( struct Video *vid, int mode )
    {
      switch ( mode ) {
      case CATEGORY:
        /* Print the category details */
      case TITLE:
        /* Print the title details */
      .
      .
      }
    }
    -Prelude
    My best code is written with the delete key.

  3. #3
    Unregistered
    Guest
    okay, i've changed a bit, but this still sint working. it accepts the user input of a catagory, but it doesnt seem to look through the array to find any videos with that catagory. any ideas?

    printf("Enter the catagory of video you would like displayed (Kids,Action,Romance,Thriller) - ");
    scanf("%s", UserCat);

    printf("\nSearching for \"%s\"...\n", UserCat);

    printf("\n\n");

    printf("Number Title Certificate Catagory Daily Rental Available\n\n");

    for (j = 0; j < i; j++)
    if ( strcmp ( VideoData[j].videotitle, UserCat ) == 0 )
    {
    printf("%-9.9s %-16.16s %-12.2s %-10s %-14.2f %c\n", VideoData[j].videonumber, VideoData[j].videotitle, VideoData[j].videocert, VideoData[j].videocat, VideoData[j].dailyrental, VideoData[j].available);
    }
    else
    {
    printf("Sorry we have no videos in that catagory available");
    }

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    I may be wrong and you have unusual naming conventions, but
    if ( strcmp ( VideoData[j].videotitle, UserCat ) == 0 )
    should be
    if ( strcmp ( VideoData[j].category, UserCat ) == 0 )

    If you plan to check all of the videos by category then it might be a good idea to test the user input ( which is a category ) with the category member of the struct. Otherwise the search probably won't find anything worthwhile.

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

  5. #5
    Unregistered
    Guest
    thanks! i got it!

  6. #6
    Unregistered
    Guest
    i also now want to check that the video is available and has not been rented out allready. The bit of code ive added doesnt seem to be working. Any ideas?for (j = 0; j < i; j++)
    if ( VideoData[j].available == "Y" &&
    strcmp ( VideoData[j].videocat, UserCat ) == 0 )
    {
    printf("%-9.9s %-16.16s %-12.2s %-10s %-14.2f %c\n", VideoData[j].videonumber, VideoData[j].videotitle, VideoData[j].videocert, VideoData[j].videocat, VideoData[j].dailyrental, VideoData[j].available);
    }

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >if ( VideoData[j].available == "Y" &&
    If the user won't be looking at the available option, you can declare it as int and use 0 and 1 to determine availability. Otherwise if it's already declared as a char use 'Y' in your test and make sure that when you change the availability you use upper case letters.

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

  8. #8
    Unregistered
    Guest
    its not working, cant see why. Its only stopped working since i added "VideoData[j].available == "Y" &&" to it.

    for (j = 0; j < i; j++)
    if (VideoData[j].available == "Y" && strcmp (VideoData[j].videocat, UserCat) == 0)
    {
    printf("%-9.9s %-16.16s %-12.2s %-10s %-14.2f %c\n", VideoData[j].videonumber, VideoData[j].videotitle, VideoData[j].videocert, VideoData[j].videocat, VideoData[j].dailyrental, VideoData[j].available);
    }

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    First, make sure that your struct actually has a member called available. Then if it's a char, use this
    if (VideoData[j].available == 'Y' && strcmp (VideoData[j].videocat, UserCat) == 0)

    Then check to see that you set the variable at some point. If you don't set available videos to 'Y' then the chance of a random value being what you want is slim.

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help...accessing character arrays in a structure
    By mathewmc in forum C Programming
    Replies: 7
    Last Post: 10-31-2006, 11:20 AM
  2. passing structure arrays to functions?
    By bem82 in forum C Programming
    Replies: 3
    Last Post: 10-30-2006, 06:17 AM
  3. C diamonds and perls :°)
    By Carlos in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 05-16-2003, 10:19 PM
  4. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM