0

I am trying to make a quiz to help my son master his times tables. Is it possible to add a function that computes how quickly he completed the questions (in seconds, or minutes).

Also would like to ask: Why does the program work for any sums that don't have zero as first number? For example (3*5=15 or 5*0=0 but when the question is 0*5 it's given incorrect for entering 0).

Thanks for your help. This is the main part of the code.

#include <stdio.h>
#include <time.h>
#include <conio.h>
int main(void){
    int response=0;
    int correctAnswers=0;
    int incorrectAnswers=0;
    int i;//counter
    int percentage;
    int product;
    time_t t;
    printf ("Enter number of sums you want to attempt:\n");
    scanf ("%d",&response);

    if (response==0){
                    printf ("Thanks for playing\n");
                    return 0;
                    }
    srand((unsigned) time(&t));
    for (i=0;i<=response;i++){
        int answer=0;
        int a=rand()%12;
        int b=rand()%12;
        product=a*b;
    printf ("%d * %d=\n",a,b);
    scanf("%d",&answer);
    if ((product=answer)){
                       printf("That's correct\n");
                       correctAnswers++;
                       }
    else {
         printf ("That's wrong!\n");
         incorrectAnswers++;
         }
Raphael Jones
  • 115
  • 2
  • 12

1 Answers1

3

if condition: Change this: if ((product=answer)){ to this: if ((product==answer)){

Also, for computing time, you can use time_t and clock from the time.h library. See this question: How to use timer in C?

An example of how you can use the timer (no need for a function - it's just a couple of statements): (You can place the STARTING TIME and ENDING TIME anywhere in your code)

#include <stdio.h>
#include <time.h>
#include <conio.h>
#include<stdlib.h>
int main(void){
int response=0;
int correctAnswers=0;
int incorrectAnswers=0;
int i;//counter
int percentage;
int product,msec;
time_t t;
clock_t before = clock();   // STARTING TIME
printf ("Enter number of sums you want to attempt:\n");
scanf ("%d",&response);

if (response==0){
                printf ("Thanks for playing\n");
                return 0;
                }
srand((unsigned) time(&t));
for (i=0;i<=response;i++){
    int answer=0;
    int a=rand()%12;
    int b=rand()%12;
    product=a*b;
printf ("%d * %d=\n",a,b);
scanf("%d",&answer);
if ((product==answer)){
                   printf("That's correct\n");
                   correctAnswers++;
                   }
else {
     printf ("That's wrong!\n");
     incorrectAnswers++;
}

 }

// ENDING TIME
   clock_t difference = clock() - before;
msec = difference * 1000 / CLOCKS_PER_SEC;
printf("Time taken %d seconds %d milliseconds \n",
msec/1000, msec%1000);
}
Community
  • 1
  • 1
Ayushi Jha
  • 4,003
  • 3
  • 26
  • 43