2

Ok, so as a beginner programmer, I have been tasked with creating a simple math quiz program. It is supposed to prompt the user for how many questions to ask, congratulate or inform the user when their answer is either right or wrong. And then print out the number correct and the number incorrect at the end of the program. I have done all of this successfully, the only issue with my code now is that it asks the same questions over and over. I'm at a loss here so any help would be appreciated, thanks.

   #include <stdio.h>
   #include <stdlib.h>
   int main (void)

    {
    int i;
    int response;
    int correctAnswers = 0;
    int incorrectAnswers = 0;

    printf("\nMath Quiz\n");
    printf("Please enter # of problems you would wish to try:");
    scanf("%d", &response);

    if(response == 0)
    {
        printf("\nThanks for playing!\n");
        return 0;
    }

    for(i=0; i<response; i++)
    {
        int answer = 0;
        int a = rand() % 12;
        int b = rand() % 12;
        printf("\n%d * %d = ",a ,b);
        scanf("%d", &answer);
        if((a * b) == answer){
            printf("\nCongratulations You are correct!\n");
            correctAnswers++;
        }
        else{
            printf("Sorry you were incorrect!\n");
            incorrectAnswers++;
        }

    }
    printf("\n\nYour Results:\n\n\n");
    printf("Number Incorrect: %d\n", incorrectAnswers);
    printf("Number Correct: %d\n", correctAnswers);
    if(correctAnswers > incorrectAnswers){
        printf("You Passed!\nGood work!\n\n");
    }
    else{
        printf("You did not pass!\nYou need more work!\n\n");
    }

    return 0;
}

Additionally, any critiques as far as formatting are more than welcome. Thanks!

HouseDog
  • 33
  • 1
  • 2
  • 9
  • In general, it's good to keep formatting consistent. I've done some formatting here to make it more clear where a block starts and ends. Programmers can be quite aggressive about formatting styles (e.g. tabs/spaces, braces on the same line as for/if, etc), but as long as you're consistent and make an effort to make it readable, you should be fine. – beatgammit Nov 22 '13 at 18:16

2 Answers2

3

You need to understand how the randon number generator works in C.

rand() generates only pseudorandom numbers. This means that every time you run your code you will get exactly the same sequence of numbers.

Use the srand function to generate random numbers based upon a source number. If you want one that changes often, use the system time.

srand(time(NULL));

Also include the header file time.h to use the time function.

Call that function before any calls to rand(). If you don't call srand() before a call to rand() in your program, it is as if srand(1) was called: the seed value will be 1 at every execution of the program and the generated sequence will be always the same.

Community
  • 1
  • 1
Bert
  • 2,134
  • 19
  • 19
1

Use this srand in your code, like this...

int a;
int b; 

srand(time(0));
a  = rand() % 12;
b  = rand() % 12;