Thread: Structure array question

  1. #1
    Registered User
    Join Date
    Nov 2015
    Posts
    17

    Structure array question

    Hi,
    Im doing an assignment that needs us to create a program to make a movie database.

    In main() there is the structure array declarations, a main menu and 2 movie entries (so its not empty)

    My main menu consists of a switch statement based on the user input.
    From there it is passed to 1 of 3 functions that will either search database, add entry or print entire list of database.

    Im having a hard time setting up my structure array and Im just looking for pointers of what I'm doing wrong.

    the array is supposed to be able to fit 100 movies.

    The code Im posting is only my global declarations, main and first function.

    Whats happening right now:
    -main menu works, hands to functions fine.
    -passing to first function doesn't ask to enter anything
    -initial 2 entries don't work and have error message "unused variable entry"
    Code:
    struct movie_list entry[size] = {{"Children of Men",109,8,"Science Fiction"},                                     {"Donnie Darko",133,6,"Science Fiction"}
    any hints and pointers appreciated. Thanks!*edit* I'm using Eclipse and mingw


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define size 100 //Size of array defined
    
    
    void enter_movie(void); //Prototypes for 3 functions
    void print_movie(void);
    void search_movie(void);
    int counter;
    
    
    struct movie_list { //Movie structure prototype
        char title[30];
        float length;
        float rating;
        char genre[20];
    }entry[size];
    
    
    int main(void)
    {
        int exit; //This variable is for the while controlling while loop. It breaks out the while loop to end the program
        int menu;     // variable for menu selection
        struct movie_list entry[size] = {{"Children of Men",109,8,"Science Fiction"},
                                         {"Donnie Darko",133,6,"Science Fiction"}
        };
    
    
        exit = 1; //sets parameter for while loop expectation to be met.
    
    
        while(exit >=1){ //while loop to bring end of function operation below back to main menu
    
    
            printf("The Movie List Builder V1.0\n\n");
            printf("What would you like to do? \n Enter selection number between 1-4 below:\n");
            printf("1: Add movie entry\n");
            printf("2: Show entire movie list\n");
            printf("3: Search for movie\n");
            printf("4: Exit program\n");
            scanf("%d", &menu);
    
    
            switch(menu)
            {      case 1 : //If user chooses 1 enter entry function is called
                enter_movie();
                break;
    
    
            case 2 :
                print_movie();//If user chooses 2 search movie function is called
                break;
    
    
            case 3 :
                search_movie();  //If user chooses 3 print all entries function is called
                break;
    
    
            case 4 :
                exit = 0;  //If user chooses 4 the variable exit changes to 0 and the while loop ends.
                //leads to program ending.
                break;
    
    
            default:       //default if user doesn't input a number between 1-4
                printf("\n Error:Selection is not between 1-4.");
                break;
            }} //end of while loop and switch statement
    
    
            printf("Have a nice day!");
            system("pause");
            return 0;
        } //main() end
    
    
    
    
        void enter_movie (void) { //start of add entry function
            int num;
    
    
            printf("How many movies entries would you like to make?");
            scanf ("%d" ,&num);
            for (counter=2;counter>=num+2;counter++){
    
    
            printf("Enter Movie Title:");
            scanf(" %s",&entry[counter].title[30]);
            printf("Enter Movie Length (in minutes):");
            scanf(" %g",&entry[counter].length);
            printf("Enter Movie Rating (Between 1-10):");
            scanf(" %g",&entry[counter].rating);
            printf("Enter Movie Genre:");
            scanf(" %s",&entry[counter].genre[20]);
                }
            return;
        } //End of add entry function
    Last edited by Glassjaw; 11-22-2015 at 08:06 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This looks problematic:
    Code:
    void enter_movie(void);
    The function accepts no arguments and has no return value. Therefore, for it to do anything useful, it must affect global state. This is okay if the global state is standard output, e.g., if you will call the function to print a menu or some other message. But here the function is named "enter_movie", and indeed it turns out that you are using global variable named entry. Unfortunately, global variables tend to make it harder for you to reason about your program, and consequently can make it harder to develop and maintain your program. Instead, make entry a local variable, e.g., local to the main function.

    Your struct type is named struct movie_list, but it is not a movie list; it models a single movie. Therefore, declare it as such:
    Code:
    #define MOVIE_ENTRIES_MAX_SIZE 100
    
    struct movie {
        char title[30];
        float length;
        float rating;
        char genre[20];
    };
    
    struct movie_list {
        struct movie entries[MOVIE_ENTRIES_MAX_SIZE];
        size_t n; /* number of entries in use */
    };
    You can then declare two functions:
    Code:
    void enter_movie(struct movie *movie);
    void enter_movie_list(struct movie_list *movies);
    In the main function, you can then declare:
    Code:
    struct movie_list movies;
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2015
    Posts
    17
    So the main chunk of code you posted, and the functions are global?
    and then you just declare it in main?

    I thought I could make it void because everything I'm doing happens within the function.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Glassjaw
    So the main chunk of code you posted, and the functions are global?
    Yes. That is fine because the macro names a constant, i.e., not global state. Note that I used a descriptive name as well as the convention for macro names to be fully uppercase: you definitely do not want a name collision with macros. The functions have to be global (or rather, at file scope: you can have static functions that are technically not global): standard C does not allow for nested functions.

    Quote Originally Posted by Glassjaw
    and then you just declare it in main?
    As in the variable of type struct movie_list named movies? Yes.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Nov 2015
    Posts
    17
    Okay,
    Im still a little confused between which iteration of the structure you use for different things...

    for example if I was to declare the first movie.. Would I put something like this in main?

    Code:
    strcpy(movie_list entry[0].title, "Children of men");
            movie_list entry[0].length = 109;
            movie_list entry[0].rating = 8;
            movie_list entry[0].genre, "Science Fiction");
    and then relevent printf statements or whatever I wanted to do?



    and what about passing in a function or to a function (say the beginning of my functions above or going to the function like in my switch statements?

    would I use:

    Code:
    void enter_movie(struct movie *movie)
    and what does the * in *movie do?



    I tried changing the code to the variables you recommended and I have errors everywhere
    so I'm still clearly not fully understanding
    Thank you and sorry for so many questions! this is confusing!
    Last edited by Glassjaw; 11-23-2015 at 12:00 AM.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Glassjaw
    Im still a little confused between which iteration of the structure you use for different things...
    Basically, struct movie models a single movie; struct movie_list models the list of movies.

    Quote Originally Posted by Glassjaw
    for example if I was to declare the first movie.. Would I put something like this in main?
    No, struct movie_list is the struct type. The object is named movies. Therefore:
    Code:
    strcpy(movies.entries[0].title, "Children of men");
    movies.entries[0].length = 109;
    movies.entries[0].rating = 8;
    strcpy(movies.entries[0].genre, "Science Fiction");
    Quote Originally Posted by Glassjaw
    and what about passing in a function or to a function (say the beginning of my functions above or going to the function like in my switch statements?
    For example:
    Code:
    enter_movie_list(&movies);
    Quote Originally Posted by Glassjaw
    and what does the * in *movie do?
    It declares that movie is a pointer to a struct movie object.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 07-19-2015, 05:51 PM
  2. A question about structure and array
    By winggx in forum C Programming
    Replies: 2
    Last Post: 04-03-2010, 10:28 PM
  3. question about holding a structure in a char array
    By spank in forum C Programming
    Replies: 8
    Last Post: 09-04-2007, 07:21 AM
  4. Dynamic structure with array and array count
    By Nazgulled in forum C Programming
    Replies: 14
    Last Post: 06-08-2007, 10:10 PM
  5. Array and Structure Question
    By loopshot in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2004, 05:10 PM