We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,183 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Implementing a timer into math game

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

3
Contributors
2
Replies
2 Months
Discussion Span
1 Year Ago
Last Updated
3
Views
Joniniko
Newbie Poster
1 post since Nov 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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.

Narue
Bad Cop
Team Colleague
15,460 posts since Sep 2004
Reputation Points: 6,483
Solved Threads: 1,407
Skill Endorsements: 54
JilMakias
Newbie Poster
9 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0595 seconds using 2.7MB