# How to add a timer and background music in a C program?



## aditya.shevade (Jul 31, 2006)

Hi

I am attaching a program that I wrote in C.

I want to display a timer on the screen which will run till main is called back.

The time should be displayed when the number guessed is correct. (With the final output).

Can I add background music to this program? Ias there any way? (I am not talking of the sound(frequency) function, a music in maybe midi format like we do in html).

Aditya

The attached file contains following code....

#include<stdlib.h>
#include<time.h>
main()
{
	int w,x=0,r1,r2,r3,r4,r5,r6,r7,r8,y,nc=0,pc=0,quit;
	clrscr();

	y = input(w);

	r2=(y-y%10)/10;
	r4=(r2-r2%10)/10;
	r6=(r4-r4%10)/10;
	if(y%10==r2%10||y%10==r4%10||y%10==r6%10||r2%10==r4%10||r2%10==r6%10||r4%10==r6%10)
	{
		main();
	}
	if(y<1000||y>10000)
	{
		main();
	}
	if(y>999&&y<10000)
	{
		while(x!=y)
		{

			printf("\n\n\t\tEnter Your Choise Between 999 And 10000\n\n\t\t\t\t");
			scanf("%d",&x);
			if(x<10000&&x>999)
			{
				pc=0;
				nc=0;
				if(x%10==y%10)
				pc++;
				r1=(x-x%10)/10;
				r2=(y-y%10)/10;
				if(r1%10==r2%10)
				pc++;
				r3=(r1-r1%10)/10;
				r4=(r2-r2%10)/10;
				if(r3%10==r4%10)
				pc++;
				r5=(r3-r3%10)/10;
				r6=(r4-r4%10)/10;
				if(r5%10==r6%10)
				pc++;
				if( y%10==x%10||y%10== r1%10||y%10== r3%10||y%10== r5%10)
				nc++;
				if(r2%10==x%10||r2%10==r1%10||r2%10==r3%10||r2%10==r5%10)
				nc++;
				if(r4%10==x%10||r4%10==r1%10||r4%10==r3%10||r4%10==r5%10)
				nc++;
				if(r6%10==x%10||r6%10==r1%10||r6%10==r3%10||r6%10==r5%10)
				nc++;

				printf("\n\n\t\t%d - Positions Correct",pc);
				printf("\n\n\t\t%d - Numbers Correct",nc);
				if(pc==4&&nc==4)
				{
					clrscr();
					printf("\n\n\n\n\n\t\tThe Number You Asked Me To Guess Is \n\n\n\n\n\t\t\t\t%d\n\n\n",x);
					printf ("\n\n\t\tPress 1 to play again or any other key to quit\n\n\n\t\t");
					scanf ("\n\n%d",&quit);
					if (quit==1)
					{
						main();
					}
					else
					{
						thankyou();
					}
				}
			}
			if (x==-1)
			{
				clrscr();
				printf("\n\n\n\t\tThank You For Playing The Game. Hope You Enjoyed.");
				getch();
				abort();
			}
			if (x<1000||x>10000)
			printf("\n\t\tThe Number You Have To Guess Is Between 999 And 10000");
		}
	}
	else
	printf("\n\n\n\n\n\tThe Number You Want Me To Guess Should Be Between 999 And 10000\n\n\t\t\t\t");
	getch();
	return 0;
}

input(u)
{
	printf("\n\n\n\n\n\n\t\tPlease Press Enter To Start The Game\n\n\n\t\tIf You Want To Quit The Game Before It Finishes\n\n\n\t\tThen Enter The Number -1.\n\n\n\n\t\t\t\t");
	getch();
	randomize();
	u = rand() % 10000;
	clrscr();

	return (u);
}

thankyou()
{
	clrscr();
	printf ("\n\n\n\t\tThank You For Playing This Game. See You Next Time");
	getch();
	abort();
	return 0;
}


----------



## Pathik (Jul 31, 2006)

i m getting errors wen compiling it...


----------



## JGuru (Jul 31, 2006)

@Aditya, if you want to add background music like MIDI/WAV/MP3 use VC++ or JAVA.
Regarding the Timer:
  This is how you do it. Use a for loop & use the sleep function, start this in a separate
 Thread. Adjust the value for sleep() function , so that it works perfectly. It should 
 display 1 sec, 2 sec to 59 sec, 1 min. and so on.


----------



## aditya.shevade (Jul 31, 2006)

@pathics

Which error are you getting?

Is it related to function declaration? If it is then please select the default type to c. ( I was having this problem when I saved the file as cpp in turboc and tried to compile it.).

Aditya


----------



## Pathik (Jul 31, 2006)

yup i saved it as c.. i got 2 errors... fucn decl..


----------



## aditya.shevade (Jul 31, 2006)

@pathiks

I have compiled the program and it's running it fine.

What is the message that you are getting?

I can't even give an exe file.

Aditya


----------

