Alright so basically i need to create a simple maths game in C. Involving addition subtraction and multiplication of random numbers. But i also need to add a timer to that. So lets say you are given 10 seconds to answer the question and every correct answer adds you 10 seconds. How would i implement this?
Here is what i got so far. Any other comments/suggestions about the code are welcome among with criticism as i want my code to be as efficient as possible and in good style.
Btw i'm required to use system("pause"); at the end, just to let you know that i did not do that on purpose.
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int GetRand(int min, int max);
int main() {
int x,y,z,i,cho,cor=0,err=0;
printf("Please select what you would like to do\n1)Addition\n2)Subtraction\n3)Multiplication\n");
scanf("%d",&cho);
if (cho==1) {
for(i=0;i<5;i++) {
x = GetRand(0, 100);
y = GetRand(0, 100);
printf("%d + %d\n",x,y);
scanf("%d",&z);
if (z!=x+y) { printf("Wrong answer\n"); err=err+1; }
if (z==x+y) { printf("Correct answer\n"); cor=cor+1;} }
printf("Correct answers: %d\nWrong answers: %d\n",cor,err);
}
if (cho==2) {
for(i=0;i<5;i++) {
x = GetRand(0, 100);
y = GetRand(0, 100);
printf("%d - %d\n",x,y);
scanf("%d",&z);
if (z!=x-y) { printf("Wrong answer\n"); err=err+1; }
if (z==x-y) { printf("Correct answer\n"); cor=cor+1;} }
printf("Correct answers: %d\nWrong answers: %d\n",cor,err);
}
if (cho==3) {
for(i=0;i<5;i++) {
x = GetRand(0, 15);
y = GetRand(0, 15);
printf("%d * %d\n",x,y);
scanf("%d",&z);
if (z!=x*y) { printf("Wrong answer\n"); err=err+1; }
if (z==x*y) { printf("Correct answer\n"); cor=cor+1;} }
printf("Correct answers: %d\nWrong answers: %d\n",cor,err);
}
system("pause");
return 0;
}
int GetRand(int min, int max)
{
static int Init = 0;
int rc;
if (Init == 0)
{
srand(time(NULL));
Init = 1;}
rc = (rand() % (max - min + 1) + min);
return (rc);
}
Thank you