Question:
Multiple choice trivia game in C?
dragon_lover50
2013-11-13 10:07:56 UTC
Basically, I'm trying to build a program in C (not C++) to call a function for a multiple choice trivia game. So the output has to have a main menu with topics, then calls a function for the question and tells the user whether or not they are right, then moves back to the main menu.

Sadly however I'm not sure how to do this with functions (Figured an array would be simpler but apparently the point of the assignment is to get us to work with functions). I missed a few classes during functions, i'm trying to learn from the book but its a little difficult.

My jumbled mess of code is this (yes, I know most of it probably doesn't even make sense, i'm terrible at this). It compiled but I cant seem to get it to call the function with the question in it after I input the proper digit... It needs anywhere between 4 or 5 questions but I cant even get one to work so far. It needs a lot of help. :/

Thank you!!


// Libraries
#include
#include

// Function Prototype
int funcnum1(int num1);
int funcnum2(int num2);
int funcnum3(int num3);
int funcnum4(int num4);
int funcnum5(int num5);

// Global Variables
int num1 = 0;
int num2 = 0;
int num3 = 0;
int num4 = 0;
int num5 = 0;

int main()
{
// Local main variables
int num1 = 1; // for use with current and additional future functions
int num2 = 2;
int num3 = 3;
int num4 = 4;
int num5 = 5;


// main Process
printf("\nPlease pick a topic:");
printf("\n1: History\n");

printf("\n2: Science\n");

printf("\n3: Health\n");

printf("\n4: Astronomy\n");

printf("\n5: Geology\n");
scanf("%d", &num1);
scanf("%d", &num2);
scanf("%d", &num3);
scanf("%d", &num4);
scanf("%d", &num5);
funcnum1(num1);
printf("\nYou are back in main.\n");
printf("The sum passed back to main is %d\n", num1);

funcnum2(num2);
printf("\nPlease pick another topic\n");
printf("The sum passed back to main is %d\n", num2);

system ("pause");

return 0;
}

int funcnum1(int num1)
{
int a1 = 1;
int a2 = 2;
int a3 = 3;
if (num1=1)
printf("\nIn what year did Napolean die?");
printf("\n1:1800\n");

printf("\n2:1777\n");

printf("\n3:1821\n");
scanf("%d", &a1);
scanf("%d", &a2);
scanf("%d", &a3);

if ( num1 = a1 )
printf( "You are correct!" );
return num1;
}


int funcnum2(int num2)
{
int input=0;





printf("\n Another question goes here.\n");
scanf("%d",&input);



return num2;
}
Three answers:
?
2016-12-18 09:35:30 UTC
Multiple Choice Trivia Games
Jonathan
2013-11-13 11:07:48 UTC
I guess your confusion is being passed on to me. I think I understand that you want the user to select a topic (like "science" or "famous mountains") and that you would call a function that would display some question underneath such a topic, get an answer, and check if it is right or not? And, as a side bar, you are supposed to be doing "this with functions" in order to learn about functions?



If this is your goal, you've got two tiers to work from. At the top tier, you have your list of topics. You should be able to vary this list, at will and without too much difficulty. On the next tier, each of the topics must link to a secondary list of specific questions within that topic and a list of choices (and the correct one noted.) I believe there should be some "randomness" added in order to select one of the specific questions underneath the topic. And perhaps a list kept to avoid re-asking the same one accidentally. (Which could also be used to indicate if the topic becomes exhausted and cannot be selected as a topic, anymore.)



That may be more than you want to "bite" off, though. I don't know. Are you wanting only ONE question per topic? And can you accept that if the same topic is selected twice, they get the same question over and over? It's important to know, because it affects the overall design approach.



EDIT: Okay. So the basic idea is:



        forever do,

            display topic menu

            get user input

            if user wants to quit, break out of here

            depending on user topic choice, do,

                history: call history function and add success to total

                science: call science function and add success to total

                health: call health function and add success to total

                astronomy:call astronomy function and add success to total

                geology: call geology function and add success to total



In C, this looks like:



typedef enum {

    history= 1, science= 2, health= 3, astronomy= 4, geology= 5, quit= -1

} choice_t;

int main( void ) {

    int total= 0;

    for ( ; ; ) {

        choice_t choice;

        displaytopics();

        choice= getchoice();

        switch ( choice ) {

        case history:

            total += historyq( );

            break;

        case science:

            total += scienceq( );

            break;

        case health:

            total += healthq( );

            break;

        case astronomy:

            total += astronomyq( );

            break;

        case geology:

            total += geologyq( );

            break;

        case quit:

        default:

            printf( "Total score: %d\n", total );

            return 0;

        }

    }

    return 0;

}



I used an enum type. I hope you don't mind. You don't have to do that, though. I just thought you might like to see it used.



What's missing at this point are the 5 functions for the questions, plus displaytopics() and getchoice(). You could replace displaytopics() with a simple printf(), instead. getchoice() could also be replaced with a scanf(). (DO NOT use a whole bunch of scanf() calls like I see in your code example.) Each of your 5 functions must display the question and return a value of either 0 (wrong) or 1 (correct.)
?
2016-09-18 01:28:22 UTC
That is not correct


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...