d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Trivia Game > Needin A Tad Bit Of Help
Add Reply New Topic New Poll
Member
Posts: 51
Joined: Feb 7 2013
Gold: 0.00
Mar 14 2013 02:28pm
OK so the main part is finished I am keeping this extremely simple
however I need the game to keep track of how many questions were answered correctly or incorrectly
Should this be a global variable and if so where should I put it
I tried putting in the correct function but it failed and I don't even think its even running
here is my current code

Code
// Trivia Game
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
/********************************************
FUNCTION PROTOTYPES
********************************************/
int ZeldaQuestion1(void);
int PokemonQuestion1(void);
int MarioQuestion1 (void);
int DigimonQuestion1 (void);

void pause(int);
/*******************************************/
/********************************************
GLOBAL VARIABLE
********************************************/
int giResponse = 0;
/*******************************************/
main()
{
     do {
         system("cls");
          printf("\n\tTHE GAMERS TRIVIA GAME\n\n");
          printf("1\tZelda\n");
          printf("2\tPokemon\n");
          printf("3\tMario\n");
          printf("4\tDigimon\n");
          printf("5\tQuit\n");
          printf("\n\nEnter your selection: ");
          scanf("%d", &giResponse);
          switch(giResponse)
                            {
                            case 1:
                            if (ZeldaQuestion1() == 4)
                            printf("\nCorrect!\n");
                            else
                            printf("\nIncorrect\n");
                            pause(2);
                            break;
                            case 2:
                            if (PokemonQuestion1() == 2)
                            printf("\nCorrect!\n");
                            else
                            printf("\nIncorrect\n");
                            pause(2);
                            break;
                            case 3:
                            if (MarioQuestion1() == 3)
                            printf("\nCorrect!\n");
                            else
                            printf("\nIncorrect\n");
                            case 4:
                            if (DigimonQuestion1() == 1)
                            printf("\nCorrect!\n");
                            else
                            printf("\nIncorrect\n");
                            } //end switch
          } while ( giResponse != 5 );
} //end main function
/**********************************************************
FUNCTION DEFINITION
**********************************************************/
int ZeldaQuestion1(void)
{
   int iAnswer = 0;
   system("cls");
   printf("\nWho is the Main character in the zelda series? ");
   printf("\n\n1\tYourself\n");
   printf("2\tZelda\n");
   printf("3\tGannon\n");
   printf("4\tLink\n");
   printf("\nEnter your selection: ");
   scanf("%d", &iAnswer);
   return iAnswer;
} //end ZeldaQuestion1 function
/**********************************************************
FUNCTION DEFINITION
**********************************************************/
int PokemonQuestion1(void)
{
int iAnswer = 0;
system("cls");
printf("\tPokemon Question\n");
printf("\nWhat pokemon belongs to pokedex # 133 ");
printf("\n\n1\tPikachu\n");
printf("2\tEevee\n");
printf("3\tCharizard\n");
printf("4\tDragonite\n");
printf("\nEnter your selection: ");
scanf("%d", &iAnswer);
return iAnswer;
} //end PokemonQuestion1 function
/**********************************************************
FUNCTION DEFINITION
**********************************************************/
int MarioQuestion1(void)
{
   int iAnswer = 0;
   system("cls");
   printf("\nWhat is mario's last name? ");
   printf("\n\n1\tNothing\n");
   printf("2\tMario\n");
   printf("3\tLuigi\n");
   printf("4\tRomano\n");
   printf("\nEnter your selection: ");
   scanf("%d", &iAnswer);
   return iAnswer;
} //end MarioQuestion1 function
/**********************************************************
FUNCTION DEFINITION
**********************************************************/
int DigimonQuestion1(void)
{
   int iAnswer = 0;
   system("cls");
   printf("\nWhat does agumon originally digivolve to? ");
   printf("\n\n1\tGreymon\n");
   printf("2\tRiseGreymon\n");
   printf("3\tWargreymon\n");
   printf("4\tShiningGreymon\n");
   printf("\nEnter your selection: ");
   scanf("%d", &iAnswer);
   return iAnswer;
} //end ZeldaQuestion1 function
/***********************************************************
FUNCTION DEFINITION
************************************************************/
void pause(int inNum)
{
int iCurrentTime = 0;
int iElapsedTime = 0;
iCurrentTime = (time(NULL));
do
   {
   iElapsedTime = time(NULL);
   } while ( (iElapsedTime - iCurrentTime) < inNum );
} // end pause function

// int Answer_Correct = 0;
// -
// IF an answer is correct THEN increment Answer_Correct + 1
Member
Posts: 16,144
Joined: Mar 27 2008
Gold: 14,618.00
Mar 14 2013 04:16pm
first about your style in the main function:

make a new variable, called "answerCorrect".
before the switch, put it to zero
in the switch, make only these if's: "if the answer is correct" -> "then put answerCorrect to 1"
after the switch, you can make a simple if and there you should output "correct" or "incorrect"

second, about your question:
why is "int giResponse = 0;" before the main? its only required in the main... so put it in the main function at the start. there i also would make the counters for "numberCorrect" and "numberIncorrect"

you have those functions, inside you already know which answer he chose and which answer was right. so in the function you already know if it's right or not. the return value of these functions should be 1 or 0, depending if the answer was right... imo ^^
Member
Posts: 51
Joined: Feb 7 2013
Gold: 0.00
Mar 14 2013 07:50pm
I tried adding a few things in there however My correct and Incorrect counters read 2686788 at the end I am not sure what went wrong

Code
// Trivia Game
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
/********************************************
FUNCTION PROTOTYPES
********************************************/
int ZeldaQuestion1(void);
int PokemonQuestion1(void);
int MarioQuestion1 (void);
int DigimonQuestion1 (void);

void pause(int);
/*******************************************/
/********************************************
GLOBAL VARIABLE
********************************************/
int giResponse = 0;
/*******************************************/
main()
{
     int giResponse = 0;
     int NumberCorrect = 0;
     int NumberIncorrect = 0;
     
     do {
         system("cls");
          printf("\n\tTHE GAMERS TRIVIA GAME\n\n");
          printf("1\tZelda\n");
          printf("2\tPokemon\n");
          printf("3\tMario\n");
          printf("4\tDigimon\n");
          printf("5\tQuit\n");
          printf("\n\nEnter your selection: ");
          scanf("%d", &giResponse);
          switch(giResponse)
                            {
                            case 1:
                            if (ZeldaQuestion1() == 4)
                            printf("\nCorrect!\n"), NumberCorrect++;
                            else
                            printf("\nIncorrect\n"),NumberIncorrect++;
                            pause(2);
                            break;
                            case 2:
                            if (PokemonQuestion1() == 2)
                            printf("\nCorrect!\n"), NumberCorrect++;
                            else
                            printf("\nIncorrect\n"),NumberIncorrect++;
                            pause(2);
                            break;
                            case 3:
                            if (MarioQuestion1() == 3)
                            printf("\nCorrect!\n"),NumberCorrect++;
                            else
                            printf("\nIncorrect\n"),NumberIncorrect++;
                            case 4:
                            if (DigimonQuestion1() == 1)
                            printf("\nCorrect!\n"),NumberCorrect++;
                            else
                            printf("\nIncorrect\n"),NumberIncorrect++;
                            } //end switch
          } while ( giResponse != 5 );
          printf ("\nYou have correctly answered %d questions correctly\n"), NumberCorrect;
          printf ("\nYou have answered %d questions incorrectly\n"), NumberIncorrect;
          system ("pause");
} //end main function
/**********************************************************
FUNCTION DEFINITION
**********************************************************/
int ZeldaQuestion1(void)
{
   int iAnswer = 0;
   system("cls");
   printf("\nWho is the Main character in the zelda series? ");
   printf("\n\n1\tYourself\n");
   printf("2\tZelda\n");
   printf("3\tGannon\n");
   printf("4\tLink\n");
   printf("\nEnter your selection: ");
   scanf("%d", &iAnswer);
   return iAnswer;
} //end ZeldaQuestion1 function
/**********************************************************
FUNCTION DEFINITION
**********************************************************/
int PokemonQuestion1(void)
{
int iAnswer = 0;
system("cls");
printf("\tPokemon Question\n");
printf("\nWhat pokemon belongs to pokedex # 133 ");
printf("\n\n1\tPikachu\n");
printf("2\tEevee\n");
printf("3\tCharizard\n");
printf("4\tDragonite\n");
printf("\nEnter your selection: ");
scanf("%d", &iAnswer);
return iAnswer;
} //end PokemonQuestion1 function
/**********************************************************
FUNCTION DEFINITION
**********************************************************/
int MarioQuestion1(void)
{
   int iAnswer = 0;
   system("cls");
   printf("\nWhat is mario's last name? ");
   printf("\n\n1\tNothing\n");
   printf("2\tMario\n");
   printf("3\tLuigi\n");
   printf("4\tRomano\n");
   printf("\nEnter your selection: ");
   scanf("%d", &iAnswer);
   return iAnswer;
} //end MarioQuestion1 function
/**********************************************************
FUNCTION DEFINITION
**********************************************************/
int DigimonQuestion1(void)
{
   int iAnswer = 0;
   system("cls");
   printf("\nWhat does agumon originally digivolve to? ");
   printf("\n\n1\tGreymon\n");
   printf("2\tRiseGreymon\n");
   printf("3\tWargreymon\n");
   printf("4\tShiningGreymon\n");
   printf("\nEnter your selection: ");
   scanf("%d", &iAnswer);
   return iAnswer;
} //end ZeldaQuestion1 function
/***********************************************************
FUNCTION DEFINITION
************************************************************/
void pause(int inNum)
{
int iCurrentTime = 0;
int iElapsedTime = 0;
iCurrentTime = (time(NULL));
do
   {
   iElapsedTime = time(NULL);
   } while ( (iElapsedTime - iCurrentTime) < inNum );
} // end pause function
Member
Posts: 51
Joined: Feb 7 2013
Gold: 0.00
Mar 14 2013 08:00pm
ermagerd nvm -.-''
my counters every single one of em were out of the closed brackets
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll