1,105,578 Community Members

Implementing a timer into math game

Member Avatar
Joniniko
Newbie Poster
1 post since Nov 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
Narue
Bad Cop
12,139 posts since Sep 2004
Reputation Points: 5,693 [?]
Q&As Helped to Solve: 1,537 [?]
Skill Endorsements: 81 [?]
Team Colleague
 
0
 

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?

The simplest approach would have you checking the difference of the time between the point where the question is displayed and the point where the user has submitted an answer. The benefit of this is it doesn't have to be real-time, which would require more complex concurrency techniques.

Member Avatar
JilMakias
Newbie Poster
9 posts since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 
You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: