Thread: Attempting to create a multiple choice quiz, works but with a warning

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    51

    Attempting to create a multiple choice quiz, works but with a warning

    I am trying to create a multiple choice quiz so I can learn the menu at my new job, while doing a side project but I am having a warning when outputting. Can you guys take a look at this to see what it is? I would appreciate it. Its NOT homework, its a side project!

    PS. Speaking of side projects, is this a kind of side project people are looking for on a resume?

    Code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <stdlib.h>
    #include <string.h>
    
    typedef struct quiz
    {
        char question[20][100];
        char answer[20];
    } quiz;
    
    // function prototypes
    void create_boxed_menu();
    void create_top();
    void create_bottom();
    void create_sides();
    void drinks_was_selected();
    void wine_was_selected();
    
    int main()
    {
        int command_code, i, drink_category_selector;
    
        printf("\n");
        create_top();
        create_boxed_menu();
    
        create_sides();
        
        printf("-\tYour selection is:\t\t\t\t\t\t  -\n");
        printf("\t");
        scanf("%d", &command_code);
        create_sides();
        
        if (command_code == 1)
        {
            // Running the drink_was_selected function that displays the different category of drinks
            drinks_was_selected();
            
            printf("-\tWhich category of drinks would you like to be quizzed on:\t  -\n");
            printf("\t");
            
            // Scanning in the users selection of drinks to be quizzed on
            scanf("%d", &drink_category_selector);
    
            // The selection is Wine
            if (drink_category_selector == 1)
            {
                wine_was_selected();
            }
    
            // The selection is Beer
            else if (drink_category_selector == 2)
            {
                
            }
    
            // The selection is Crushes
            else if (drink_category_selector == 3)
            {
                
            }
    
            // The selection is Slushes
            else if (drink_category_selector == 4)
            {
                
            }
    
            // The selection is Shakers
            else if (drink_category_selector == 5)
            {
                
            }
    
            // The selection is House Specialties
            else if (drink_category_selector == 6)
            {
                
            }
    
            // The selection is Ultimates 
            else
            {
    
            }
    
            for (i=0; i<2; i++)
                create_sides();
        }
            
    
        else if (command_code == 2)
        {
            printf("-\tYou have entered food!\t\t\t\t\t\t  -\n");    
            printf("\n");
        }
    
        else
        {
            printf("\tYou have entered an invalid choice!\n");
            printf("\tPlease restart the program!\n");
            printf("\n");
        }
            
    
        return 0; 
    }
    
    void create_boxed_menu()
    {
        // Creating variables
        int i;
    
        printf("\n");
        printf("-   \t\tWelcome to the T.G.I. Friday's Menu Quiz!    \t\t  -\n");
    
        for (i=0; i< 2; i++)
        {
            create_sides();
        }
    
        printf("-   \tPlease select a category that you would like to be quizzed on:    -\n");
        create_sides();
        printf("-   \tFor drinks, type 1 and hit enter \t\t\t\t  -\n");
        printf("-   \tFor food, type 2 and hit enter \t\t\t\t\t  -\n");
    
        for (i=0; i< 1; i++)
        {
            create_sides();
        }
    
    }                       
    
    void create_top()
    {
        int i, width=75;
    
        for (i=0; i< width; i++)
        {
            printf("-");
        }
    }
    
    void create_bottom()
    {
        int i, width=75;
    
        for (i=0; i< width; i++)
        {
            printf("-");
        }
    }
    
    void create_sides()
    {
        printf("-\t\t\t\t\t\t\t\t\t  -\n");
    }
    
    void drinks_was_selected()
    {
        int i;
        char *drink_categories[7] = {"Wine", "Beer", "Crushes", "Slushes", "Shakers", "House Specialties", "Ultimates"};
    
        printf("-\tPlease select from the following drinks:\t\t\t  -\n");
        create_sides();
        
        // Loop beer and wine only so the dashes line up!
        for (i=0; i<2; i++)
        {
            printf("-\t%d. %s\t\t\t\t\t\t\t\t  -\n", i+1, drink_categories[i]);
        }
    
        // Loop Crushes through Shakers so the dashes line up!
        for (i=2; i<5; i++)
        {
            printf("-\t%d. %s\t\t\t\t\t\t\t  -\n", i+1, drink_categories[i]);
        }
    
        // Printing House Specialties
        for (i=5; i<6; i++)
        {
            printf("-\t%d. %s\t\t\t\t\t\t  -\n", i+1, drink_categories[i]);
        }
    
        // Printing Ultimates
        for (i=6; i<7; i++)
        {
            printf("-\t%d. %s\t\t\t\t\t\t\t  -\n", i+1, drink_categories[i]);
        }
        create_sides();
    }
    
    void wine_was_selected()
    {
        quiz* wine = malloc(sizeof(quiz));
        int i, number_of_wine_questions;
        number_of_wine_questions = 15;
    
        // Create a temporary buffer array that holds the wine questions
        char wine_questions[20][100] = {"Which wine has citrus combined with caramel and fresh baked shortbread?", 
                                        "Which wine has apples, lemon, pineapple and mango with honey and vanilla?",
                                        "Which wine has honeyed peaches and apple flavor with a vanilla aroma?",
                                        "Which wine has peach, apricot, and a hint of citrus?", 
                                        "Which wine has sweet passion fruit and sweet lime?",
                                        "Which wine has red berry, citrus and melon, nutmeg and clove?",
                                        "Which wine has a wild flower aroma with a tropical fruit flavor?",
                                        "Which sparkling wine has an extremely delicate note?",
                                        "Which wine has an aroma of cherries with red currants and spice?",
                                        "Which wine has black cherry, cranberry with vanilla and oak?",
                                        "Which wine has an aroma of dark cherry with clove and oak?",
                                        "Which wine has blackberry, black cherry with black pepper?",
                                        "Which wine has currant, raspberry, and blackberry with a vanilla finish?",
                                        "Which wine has currants, berries and tannins in this wine?",
                                        "Which wine has anise, mocha, dark berry, black cherry, spices, and chocolate?"};
    
        for (i=0; i< number_of_wine_questions; i++)
        {
            strcpy(wine[i].question, wine_questions[i]);
            printf("%s\n", wine[i].question);
        }
        
    }
    Output:
    Code:
    menuquiz.c: In function ‘wine_was_selected’:
    menuquiz.c:226:9: warning: passing argument 1 of ‘strcpy’ from incompatible pointer type [enabled by default]
    /usr/include/string.h:128:14: note: expected ‘char * __restrict__’ but argument is of type ‘char (*)[100]’
    menuquiz.c:227:9: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[100]’ [-Wformat]
    
    ---------------------------------------------------------------------------
    -           Welcome to the T.G.I. Friday's Menu Quiz!              -
    -                                      -
    -                                      -
    -       Please select a category that you would like to be quizzed on:    -
    -                                      -
    -       For drinks, type 1 and hit enter                   -
    -       For food, type 2 and hit enter                       -
    -                                      -
    -                                      -
    -    Your selection is:                          -
        1
    -                                      -
    -    Please select from the following drinks:              -
    -                                      -
    -    1. Wine                                  -
    -    2. Beer                                  -
    -    3. Crushes                              -
    -    4. Slushes                              -
    -    5. Shakers                              -
    -    6. House Specialties                          -
    -    7. Ultimates                              -
    -                                      -
    -    Which category of drinks would you like to be quizzed on:      -
        1
    Which wine has citrus combined with caramel and fresh baked shortbread?
    Which wine has apples, lemon, pineapple and mango with honey and vanilla?
    Which wine has honeyed peaches and apple flavor with a vanilla aroma?
    Which wine has peach, apricot, and a hint of citrus?
    Which wine has sweet passion fruit and sweet lime?
    Which wine has red berry, citrus and melon, nutmeg and clove?
    Which wine has a wild flower aroma with a tropical fruit flavor?
    Which sparkling wine has an extremely delicate note?
    Which wine has an aroma of cherries with red currants and spice?
    Which wine has black cherry, cranberry with vanilla and oak?
    Which wine has an aroma of dark cherry with clove and oak?
    Which wine has blackberry, black cherry with black pepper?
    Which wine has currant, raspberry, and blackberry with a vanilla finish?
    Which wine has currants, berries and tannins in this wine?
    Which wine has anise, mocha, dark berry, black cherry, spices, and chocolate?
    -                                      -
    -                                      -

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    You create one wine struct:
    Code:
    quiz* wine = malloc(sizeof(quiz));
    This struct has a member called question, which is an array of arrays of char (i.e. an array of strings):
    Code:
    typedef struct quiz
    {
        char question[20][100];
        char answer[20];
    } quiz;
    Now look at this:

    Code:
    wine[i].question
    Do you see the problem?

    Hint: Is wine an array? Is wine.question an array?
    Last edited by Cat; 04-10-2013 at 11:04 PM.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    51
    I tried to change it to
    Code:
    wine.question[i]
    to no avail. Not sure how to fix this issue.

  4. #4
    Registered User
    Join Date
    Jun 2009
    Posts
    120
    You forgot that wine is a pointer
    Code:
    wine->question[i]
    Also remember that memory allocated dynamically should be deallocated when object is no longer in use.
    Code:
    void wine_was_selected()
    {
        quiz* wine = malloc(sizeof(quiz));
        /*
        ...
        */
        free(wine);
    }

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    51
    Thanks! I got it to work!
    Last edited by C0__0D; 04-11-2013 at 10:08 AM.

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    51
    I realized that instead of initializing the question, I need to initialize the wine such as:
    Code:
    for (i=0; i< number_of_wine_questions; i++)
    {
         strcpy(wine[i].question, wine_questions[i]);
         printf("%s\n", wine[i].question);
    }
    instead of:
    Code:
    for (i=0; i< number_of_wine_questions; i++)
    {
         strcpy(wine->question[i], wine_questions[i]);
         printf("%s\n", wine->question[i]);
    }
    That way later I can match up the answer to that specific wine, such as:
    Code:
    wine[i].answer
    but I get this warning:
    Code:
    menuquiz.c: In function ‘wine_was_selected’:
    menuquiz.c:227:9: warning: passing argument 1 of ‘strcpy’ from incompatible pointer type [enabled by default]
    /usr/include/string.h:128:14: note: expected ‘char * __restrict__’ but argument is of type ‘char (*)[100]’
    menuquiz.c:228:9: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[100]’ [-Wformat]

  7. #7
    Registered User
    Join Date
    Nov 2012
    Posts
    51
    Bump

  8. #8
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Quote Originally Posted by C0__0D View Post
    I realized that instead of initializing the question, I need to initialize the wine such as:

    That way later I can match up the answer to that specific wine, such as:
    Code:
    wine[i].answer
    Have you first changed your code to actually MAKE more than one wine? Your prior code creates one, only one, and never more than one quiz struct:

    Code:
    quiz* wine = malloc(sizeof(quiz));
    wine[i] doesn't make sense for the same reason that:

    Code:
       int i;
       i[23] = 234;
    makes no sense. If you want to treat a variable as an array, you need to MAKE it an array first.

    Your whole data structure is bizarre and probably wrong. I'd start over with creating a more sensible quiz struct - something that stores ONE question and ONE answer in one struct, for example, unless there isn't a one-to-one mapping of questions to answers.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Code:
    strcpy(wine->question[i], wine_questions[i]);
    Guess only.
    Code:
    strcpy(wine->question[i], wine_questions[i][0]);
    I really almost never use multi-dim arrays; so, it is just a educated guess.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multiple choice question.
    By ungalnanban in forum C Programming
    Replies: 2
    Last Post: 02-18-2010, 10:40 AM
  2. multiple choice
    By iLike in forum C Programming
    Replies: 6
    Last Post: 10-30-2009, 03:53 PM
  3. Replies: 5
    Last Post: 03-08-2005, 07:45 PM
  4. Multiple Choice!!!
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 09-08-2002, 02:30 PM