Can anyone explain why this simple trivia game is not working?

This is a discussion on Can anyone explain why this simple trivia game is not working? within the C Programming forums, part of the General Programming Boards category; Code: #include <stdlib.h> #include <stdio.h> #include <time.h> #include <ctype.h> //Trivia Game //Function Prototypes int people(void); int places(void); void pause(int); //Global ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    2

    Can anyone explain why this simple trivia game is not working?

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <time.h>
    #include <ctype.h>
    
    
    //Trivia Game
    
    
    //Function Prototypes
    int people(void);
    int places(void);
    void pause(int);
    
    
    //Global Variables
    int answer=0;
    
    
    //Main
    
    
    main()
    {
    
    
      do
      {
          system("clear");
        printf("\t\tTrivia Game\n");
        printf("\tEnter a Choice:\n");
        printf("1.People");
        printf("2.Places");
        printf("3.Quit");
        scanf("%d",answer);
        switch(answer)
        {
            case 1:
            if (people()==2)
            printf("\nCorrect\n");
            else
            printf("\nIncorrect\n");
            pause(2);
            break;
    
    
            case 2:
            if (places()==2)
            printf("Correct\n");
            else
            printf("Incorrect\n");
            pause(2);
            break;
        }
    while (answer!=3);
      }
    }
    
    
    int places(void){
        int choice=0;
        printf("What place is warmer?\n");
        printf("1.Alaska\n");
        printf("2.Hawaii\n");
        printf("Pick Your Choice!\n");
        scanf("%d",&choice);
        pause(2);
        return choice;
    }
    
    
    int people(void)
    {
        int choice=0;
        printf("What person was president?\n");
        printf("1.Hilary Clinton");
        printf("2.Bill Clinton");
        scanf("%d",&choice);
        pause(2);
        return choice;
        }
    
    
    void pause(int inNum)
    {
        int iCurrentTime=0;
        int iElapsedTime=0;
        iCurrentTime=time(NULL);
        do
        {
            iElapsedTime = time(NULL);
        }
        while
        ((iElapsedTime-iCurrentTime)<inNum);
    }

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,434
    You are going to be more specific than "not working"

    What is it doing? What would you like it to do?
    Fact - Beethoven wrote his first symphony in C

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    5
    First thing that jumps out at me:

    main() should be "int main (void)" and you should have a return statement at the end of main.

    also:

    scanf("%d", &answer);

    you forgot the &

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    It doesn't work, because it doesn't compile.

    $ gcc -Wall foo.c
    foo.c:23:1: warning: return type defaults to ‘int’ [-Wreturn-type]
    foo.c: In function ‘main’:
    foo.c:35:5: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘int’ [-Wformat]
    foo.c:57:1: error: expected ‘while’ before ‘}’ token
    foo.c:95:1: error: expected declaration or statement at end of input
    foo.c:95:1: warning: control reaches end of non-void function [-Wreturn-type]

    You have a mis-placed brace, and a broken scanf to fix.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,307
    So far, PEBKAC.
    Start over and explain the problem.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    Jan 2012
    Location
    Chennai, Tamil nadu, India
    Posts
    30
    Just add "&" to scanf statement as mentioned before..., and add return statement to main function.. Thats it.. Here's the output
    Attached Images Attached Images  

  7. #7
    Registered User
    Join Date
    Oct 2012
    Posts
    2
    Wow. You guys are great. I'll try it later. Thanks again. God Bless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-25-2010, 10:07 AM
  2. a computer trivia game
    By Leeman_s in forum Game Programming
    Replies: 2
    Last Post: 01-22-2003, 07:30 PM
  3. come preview my trivia game!
    By Leeman_s in forum C++ Programming
    Replies: 3
    Last Post: 12-31-2002, 06:19 PM
  4. First Game (text trivia)
    By NewbieVB in forum C++ Programming
    Replies: 4
    Last Post: 04-19-2002, 07:13 AM
  5. Someone explain me why this isn't working!
    By Flikm in forum C++ Programming
    Replies: 26
    Last Post: 09-09-2001, 09:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21