Ask Question

Name:
Title:
Your Question:

Answer Question

Name:
Your Answer:
User Submitted Source Code!


Description:
  timer
Language: C/C++
Code:
#include<cstdio>
#include <ctime>
#include <iostream.h>
using namespace std;

class timer {
     private:
          unsigned long begTime;
     public:
          void start() {
               begTime = clock();
          }

          unsigned long elapsedTime() {
               return ((unsigned long) clock() - begTime) / CLOCKS_PER_SEC;
          }

          bool isTimeout(unsigned long seconds) {
               return seconds >= elapsedTime();
          }
};


int main() {
     char answer;
     char ans;
     unsigned long seconds = 5;

     timer t;
     t.start();
     cout << "You have " << seconds << " seconds to answer the question: " << endl;
     cout << "Are you ready? (y/n)" << endl;
     cin >> answer;
     while(answer == 'y' || answer == 'Y')
     {
          if(t.elapsedTime() > seconds) {
               return 0;
          }
          else if(t.elapsedTime() < seconds){
               cout << "What is the capitol of California?" << endl;
               cin >> ans;
               if(ans == 's')
                    cout << "correct!" << endl;
          
          }
     }
cin.get();     
}

          
Comments: