Random question simulator

This is a discussion on Random question simulator within the C Programming forums, part of the General Programming Boards category; Ok, I'm fairly new to programming and I have been working on one for my mother for her 4th grade ...

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    3

    Random question simulator

    Ok, I'm fairly new to programming and I have been working on one for my mother for her 4th grade class. They are learning Texas history and i just have an assortment of questions about that. Also, is the random number simulator used in the same way a random question simulator would be used? anyways here is my program so far:
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<time.h>
    #include<math.h>
    
    int compute (int x,int y,char r);
    int t3();
    
    int main ()
    {
    char z;
    int a,e,f,i;
    
    srand(time(NULL));
    
    printf("Hello, this is a standardized history test for 4th graders\n");
    printf("press 1 to begin. Press any key to end.\n");
    scanf("%d",&a);
    while(a=1)
    
    
    
    rand=t3();
    if(i==1)
    {
    printf("Davy Crockett lost his life at the _______\n");
    scanf("%d",&e);
    printf("1-Shreveport, Louisiana\n2-The Battle of the Alamo\n3-The Battle of San Jacinto\n4-The Seige of Bexar\n");
    if(e==2)
    printf("That's right!\n");
    else
    printf("Nope, try again\n");
    
    }
    if(i==2)
    {
    printf("How many flags have flown over Texas?\n");
    scanf("%d",&e);
    printf("1-two\n2-three\n3-four\n4-five\n5-six\n");
    if(e==5)
    printf("Good job!\n);
    else
    printf("Sorry, that's wrong\n);
    }
    if(i==3)
    {
    printf("What is David Bowie famous for?\n");
    scanf("%d",&e);
    printf("1-Killing Colonel Travis\n2-The Bowie knife\n3-Leader of the mexican army\n4-
    }
    if(i==4)
    {
    printf("Who led the Battle of Conception?\n");
    scanf("%d",&e);
    printf("1-David Bowie\n2-Davey Crockett\n3-Colonel William Travis\n4-Sam Houston\n);
    if(e==1)
    printf("That's right!\n);
    else
    printf("No, that is wrong);
    }
    if(i==5)
    {
    printf("What kind of terrain would you find in the Trans-Pecos Region?\n");
    scanf("%d",&e);
    printf("1-grasslands\n2-mountains\n3-desert\n4-forestry\n");
    if(e==3)
    printf("That's correct\n");
    else
    printf("Nope, I'm sorry.\n");
    }
    if(i==6)
    {
    printf("Where is the region known as the Piney Woods located?\n");
    scanf("%d",&e);
    printf("1-North Texas\n2-South Texas\n3-West Texas\n4-East Texas\n");
    if(e==4)
    printf("Yay, you got that one!\n");
    else
    printf("Oops, you missed that one!\n");
    }
    if(i==7)
    {
    printf("Which river borders Oklahoma and Texas\n");
    scanf("%d",&e);
    printf("1-The Mississippi River\n2-The Red River\n3-The Rio Grande\n4-The Nile\n");
    if(e==2)
    printf("That's right!\n");
    else
    printf("You missed that one!\n");
    }
    if(i==8)
    {
    printf("Which river borders Texas and Mexico?\n");
    scanf("%d",&e);
    printf("1-The Mississippi River\n2-The Red River\n3-The Rio Grande\n4-The Nile\n");
    if(e==3)
    printf("Right!\n");
    else
    printf("That is the wrong answer.\n");
    }
    return 0;
    I have about 30 more of those questions but i deleted them to save some space. What i also wanted to add was something that would add up all the missed and correct problems and tell you your percentage at the end. Would something like this work?:
    Code:
    if (ans= = correct)
    right=right+1;
    
    else
    wrong=wrong+1;
    I have a few more questions but that is the main jist of it. Thanks everyone.

  2. #2
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    That is coded in C, this is a C++ board.
    Also try to indent your code, makes reading and debugging it much easier.

    What i also wanted to add was something that would add up all the missed and correct problems and tell you your percentage at the end. Would something like this work?
    Yes your example would surfice as a way to acomplish that. But as you are only adding
    by 1 each time, increment the sum instead of doing it the long way.

    eg:
    Code:
    // add number by 1
    int num = 5; // original value
    num++; // add 1 to it by increment
    You could also put a loop round all the code that asks the question again if it is wrong, and
    will only increment the "correct" value if it is right, thus changing the question. I suggest
    using a for loop
    I'm just trying to be a better person - My Name Is Earl

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    176
    I may just be missing it, but I don't see where i is ever initialized for all this if (i == 1) if (i== 2) etc.

    plus you may want to look into creating a file with all the questions answers etc. in it.
    this way 1 for loop could handle all the reading, questioning etc. and it would be easier to modify and add questions

  4. #4
    Registered User
    Join Date
    Dec 2006
    Posts
    3
    Quote Originally Posted by sl4nted
    I may just be missing it, but I don't see where i is ever initialized for all this if (i == 1) if (i== 2) etc.

    plus you may want to look into creating a file with all the questions answers etc. in it.
    this way 1 for loop could handle all the reading, questioning etc. and it would be easier to modify and add questions
    How exactly would i do that?

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    176
    you would have a file setup as follows:

    q=1
    question
    choices
    answer
    q=2
    question
    choices
    answer
    q=3
    question
    choices
    answer

    or any format like that as long as it is consistant.

    you could then loop though

    read lines from file
    print question
    print choices
    check answer
    if correct increase score

  6. #6
    Registered User
    Join Date
    Nov 2006
    Posts
    176
    I was board so wrote a little program as an example for you.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define BUFFSIZE 256
    
    void flush_input()
    {
      int   ch;
    
      while ((ch = getchar()) != '\n' && ch != EOF);
    }
    
    int main(int argc, char **argv)
    {
            FILE *fp;
            char buffer[BUFFSIZE];
            char answer;
            int correct = 0;
            int total = 0;
            int i = 0;
    
            if ((fp = fopen("questions.txt", "r")) == NULL)
            {
                    perror("Unable to open file");
                    exit(1);
            }
    
            while (!feof(fp))
            {
                    fgets(buffer, BUFFSIZE, fp);
    
                    switch (i)
                    {
                            /* question number case */
                            case 0: i++; break;     /* can add functionality here if wanted */
    
                            case 1: printf("Question: %s", buffer); i++; break;
    
                            case 2: printf("Choose one: %s", buffer);
                                    printf("> ");
                                    answer = getchar();
                                    flush_input();
                                    i++;
                                    break;
    
                            case 3: if (buffer[0] == answer)    /* could also use strcmp and check a full answer */
                                    {
                                            printf("Correct!\n");
                                            correct++;
                                    }
                                    else printf("Incorrect\n");
    
                                    total++;
                                    i = 0;
                    }
            }
    
            fclose(fp);
    
            printf("Your Score: %d out of %d\n", correct, total);
            printf("%f percent\n", ((float)correct / (float)total) * 100);
            return 0;
    }
    File contents:
    q=1
    This is question 1
    a) answer1, b) answer b, c) answer c
    c
    q=2
    This is question 2
    a) another a, b) another b, c) another c
    b
    q=3
    Final question
    a) choice 1, b) choice 2, c) choice 3
    a

    Output:

    Question: This is question 1
    Choose one: a) answer1, b) answer b, c) answer c
    > c
    Correct!
    Question: This is question 2
    Choose one: a) another a, b) another b, c) another c
    > a
    Incorrect
    Question: Final question
    Choose one: a) choice 1, b) choice 2, c) choice 3
    > a
    Correct!
    Your Score: 2 out of 3
    66.666667 percent

    so basically you can just keep adding questions to the file and the program doesn't need to change

  7. #7
    Registered User
    Join Date
    Dec 2006
    Posts
    3
    Quote Originally Posted by sl4nted
    I was board so wrote a little program as an example for you.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define BUFFSIZE 256
    
    void flush_input()
    {
      int   ch;
    
      while ((ch = getchar()) != '\n' && ch != EOF);
    }
    
    int main(int argc, char **argv)
    {
            FILE *fp;
            char buffer[BUFFSIZE];
            char answer;
            int correct = 0;
            int total = 0;
            int i = 0;
    
            if ((fp = fopen("questions.txt", "r")) == NULL)
            {
                    perror("Unable to open file");
                    exit(1);
            }
    
            while (!feof(fp))
            {
                    fgets(buffer, BUFFSIZE, fp);
    
                    switch (i)
                    {
                            /* question number case */
                            case 0: i++; break;     /* can add functionality here if wanted */
    
                            case 1: printf("Question: %s", buffer); i++; break;
    
                            case 2: printf("Choose one: %s", buffer);
                                    printf("> ");
                                    answer = getchar();
                                    flush_input();
                                    i++;
                                    break;
    
                            case 3: if (buffer[0] == answer)    /* could also use strcmp and check a full answer */
                                    {
                                            printf("Correct!\n");
                                            correct++;
                                    }
                                    else printf("Incorrect\n");
    
                                    total++;
                                    i = 0;
                    }
            }
    
            fclose(fp);
    
            printf("Your Score: %d out of %d\n", correct, total);
            printf("%f percent\n", ((float)correct / (float)total) * 100);
            return 0;
    }
    File contents:
    q=1
    This is question 1
    a) answer1, b) answer b, c) answer c
    c
    q=2
    This is question 2
    a) another a, b) another b, c) another c
    b
    q=3
    Final question
    a) choice 1, b) choice 2, c) choice 3
    a

    Output:

    Question: This is question 1
    Choose one: a) answer1, b) answer b, c) answer c
    > c
    Correct!
    Question: This is question 2
    Choose one: a) another a, b) another b, c) another c
    > a
    Incorrect
    Question: Final question
    Choose one: a) choice 1, b) choice 2, c) choice 3
    > a
    Correct!
    Your Score: 2 out of 3
    66.666667 percent

    so basically you can just keep adding questions to the file and the program doesn't need to change
    Wow, that is awesome. But would you mind explaining what the void thing does? I am unfamiliar with that.

  8. #8
    Registered User
    Join Date
    Nov 2006
    Posts
    176
    void just means it doesn't return anything
    the code inside flushes the input buffer, its actually pretty much straight out of the FAQ. you can go and read up on it if you like.

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    > while (!feof(fp))
    > fgets(buffer, BUFFSIZE, fp);
    Why you shouldn't do this is in the FAQ.

    while ( fgets(buffer, BUFFSIZE, fp) != NULL )
    Achieves the same thing, and does the right thing when you get to the end of the file.
    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.

  10. #10
    Registered User
    Join Date
    Nov 2006
    Posts
    65
    You need to make your program more "general" at any time you want to allow your code to be added upon in a moments notice. You can use giant "if" statements with a chance that without a i>9 ending statement that could not loop back up to the question without restarting the program. I would recomend you use a Switch, It will allow you to test conditionals keep them in a loop (if you dont type break and run through every statement without the user from asking to be questioned again. Within each Case I would have a function. I would continue with a persudo code but I have learned my lession from that past, the best way to help someone is to allow them to code it themselfs so they can become more concrete in there work, not copy pase in others. Good luck homie

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Random number + guessing game trouble
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-08-2007, 03:33 AM
  2. Lesson #3 - Math
    By oval in forum C# Programming
    Replies: 2
    Last Post: 04-27-2006, 08:16 AM
  3. Testing Random Number Generator
    By Roaring_Tiger in forum C Programming
    Replies: 7
    Last Post: 08-12-2005, 12:48 AM
  4. Noob question for random number
    By SKINp in forum C++ Programming
    Replies: 6
    Last Post: 01-03-2003, 11:53 AM
  5. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 02:57 PM

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