creating a small maths game need hlp

hi guys!!
i am wrting a program for a small arithemtic game,it displays two numbers and ask user to enter the sum of these two numbers,the program runs fine but i want to add a countdown timer,i mean time limit for user to answer all 10 questions within 30 seconds.i hav tried everything but dint came up with any idea
it should display the timer and questions.plzz help i am new to programming

here is the code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
void addtion()
{
     int firstran,secondran,num1,num2,sum,in1,ans,count=0,right=0,worng=0,score=0;
     cout<<"\t\t\t\tADDTION\n";
                               for(int k=0;k<80;k++)
                              {
                                  cout<<"-";
                                  delay1();
                              }
                              cout<<"\n\nREADY????";
                              delay1();
                              cout<<"\n\nENTER A CHAR WHEN READY.....\n";
                              cin>>in1;
                              system("cls");
                              cout<<"\t\t\t\tLEVEL 1\n";
                              ////print lines----------------------------------
                              for(int i=0;i<80;i++)
                              {
                                  cout<<"-";
                                  delay1();  //delay to print cgar by char
                              }
                              cout<<"\t\t\t\tADDTION\n";
                               for(int k=0;k<80;k++)
                              {
                                  cout<<"-";
                                  delay1();
                              }
                              //////////--------------------------------------
                              cout<<"\nGO!!\n";
                              for(int i=10;i>0;i--)                                                                                                               
                              {
                                      
                                      firstran=random();                                                                                        
                              num1=gennumber(firstran);                          ///generate two random numbers for addtion//////
                              secondran=random2(num1);                           //gennumer generates number within 99 for level 1///
                              num2=gennumber(secondran);
                              count++;
                              sum=num1+num2;
                            cout<<count<<"  ->  "<<num1<<"  +  "<<num2<<"  = ";
                              cin>>ans;
                              
                              if(ans==sum)
                              {
                                    right++;
                                    score=score+10;
                              }
                              else { worng++;}                              
                              }
                              
                              
                              results(right,worng,score);                 ////prints the result to std output///
                              getinfo(score);                             ///get player name and store it in file with scores////
                              viewscores();                               ///read names from the file and display it on std output///
                                      
                              
                              
}


just give me some ideas or procedure to do it
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>

int main()
{
    int time_limit=10; //seconds
    int ans;
    bool ans_ready=false;

    int seconds=0;
    int prev_seconds=0;

    int start=clock();

    while (true)
    {
        if (kbhit())
        {
            cin>> ans;
            ans_ready=true;
        }

        if (ans_ready)
        {
            cout << "your answer: " << ans << endl;
            ans_ready=false;
        }

        seconds=((clock()-start)/CLOCKS_PER_SEC);

        if (seconds>prev_seconds)
        {
            cout << "seconds elapsed: ";
            cout << seconds << endl;
            prev_seconds=seconds;
        }

        if ( seconds>=time_limit )
        {
            cout << "time is up!" << endl;
            if (seconds>time_limit)
            {
                cout << "your last answer was given";
                cout << " too late... it will be ignored";
                cout << endl;
            }
            break;
        }
    }

    system("pause");
    return 0;
}
Last edited on
thanx m4sterr0shi i'll try your program now :D
wat is kbhit() function use for??

wat clock( ) function returns??

srry i'm new to programming ,can someone post a link for clock tutorial

i dont know how it works
Last edited on
to check if there is data in the input buffer

http://www.cprogramming.com/fod/kbhit.html
thanx again m4sterr0shi

does it work on DEV C++
http://cplusplus.com/reference/clibrary/ctime/clock/

if you don't understand something try googling or searching it in this site
ok thanx
Last edited on
i have used clock() function the program runs good,but i want to display the time at the corner of the screen and also display the questions at the same time
is there any way to do that??
Last edited on
Mmmm... This may be tricky... I'm afraid you'll have to resort to multithreading. Have a main thread that displays the questions and gets the answers and another one that handles the clock. You may also have to use some windows API to change the position of the console cursor.
Topic archived. No new replies allowed.