i am planning to make a game called spot the not.
the problem is i don't know how to have a timer and high score on it.
i already surf the net but i don't know how to apply it on my program.
many thanks in advance!
you really need to use code tags for formatting reasons that code is hard to read and more importantly it took me around 10 minutes to add all the inserted tabs(which is slightly more annoying). But your program works fine, for time you can use ctime, and use the time function as below: For reasons why you shouldn't use system calls can be seen from reading the sticky posts at top of forum.
/* clock example: countdown */
#include <stdio.h>
#include <time.h>
#include <iostream>
void wait ( int seconds )
{
clock_t endwait;
endwait = clock () + seconds * CLOCKS_PER_SEC ;
while (clock() < endwait) {}
}
int main ()
{
int n;
printf ("Starting countdown...\n");
for (n=10; n>0; n--)
{
printf ("%d\n",n);
wait (1);
}
printf ("FIRE!!!\n");
return 0;
}
For the high score there's a number of ways you can do this. I'm guessing you want the scores to persist, so personally I would use a write the scores to a file, then when the program starts up read the file into an array of high scores and makes adjustments as needed. As high scores typically only contain 10-20 high scores a const int for array size.
Additionally for round2! "round 2!" printed out at least 50+ times because everything scrolled off the console. Only once or a few times is needed for this. it could be a loop error or intentional I'm not usre I didn't look at code closely.
/* clock example: This function can be added to your class,
* calling it and passing an integer will make it wait a certain number of seconds
* as an example: wait(55);
* will make the program pause for 55 seconds.
* wait(1); will cause the program to pause for 1 second
*/
#include <stdio.h>
#include <time.h>
void wait ( int seconds )
{
clock_t endwait;
endwait = clock () + seconds * CLOCKS_PER_SEC ;
while (clock() < endwait) {}
}
you have implemented your own functions this shouldn't be too hard for you to use. simply:
wait (n);
ahh woops, I missread. I thought OP wanted a pause.
well you can get current time, and at the time you want to display the timer get the current time then get the difference between the two.
just format end_time as necessary it will be number of seconds.
edit:
1 2 3 4 5 6 7 8 9 10
time_t start_time;
start_time = time (NULL);
// when you want to display timer:
time_t end_time;
end_time = time (NULL);
end_time -= start_time;
printf ("%ld", end_time);
As I have been looking into this for person reasons found this statement was incorrect. you need to use difftime for differences between time. while - may work. probably better to use this. it is stored as double.
1 2 3 4 5 6 7 8 9 10 11
time_t start,end;
double dif;
// start timer
time (&start);
// end timer
time (&end);
dif = difftime (end,start);
printf ("It took you %.2lf seconds to complete game.\n", dif );
p.s all code is simply ripped from this website documentation on time. I haven't tested.