I'm doing a program on a quiz using DEV C++ and i want to set a countdown timer for it. As the user is doing the quiz, he's able to see the timer and once the timer reaches 0, the quiz automatically stops. Is it possible to do this? Thanks in advance for your views =D

Its not possible to do this with just C++. But you can approximate it like so :

#include <iostream>
#include <string>
#include <ctime>
using namespace std;

int ask(const string& question){
  cout << question;
  int ans = 0;
  cin >> ans;
  return ans;
}

int main(){
  const int MAX_TIME_LIMIT = 10000; //10 seconds or 10 * 1000 milliseconds
  long start_time = clock();
  
  int ans = ask("What is 2 + 2 * 5 = ");
  int ans2 = ask("What is 6 mod 2 = ");
  int numOfCorrect = 0;
  long end_time = clock();
  if( end_time - start_time > MAX_TIME_LIMIT){ 
      cout << "Time limit exceeded\n"; 
   }
  if( ans == 12){ numOfCorrect++; }
  if( ans2 == 0){ numOfCorrect++; }

  cout << "You got " << numOfCorrect << " correct answer out of 2 question\n";
  cout << "Your percentage is " << 100*(numOfCorrect/2.0f) << endl;

 return 0;
}

No guarantees in the above code.

Its not possible to do this with just C++. But you can approximate it like so :

#include <iostream>
#include <string>
#include <ctime>
using namespace std;

int ask(const string& question){
  cout << question;
  int ans = 0;
  cin >> ans;
  return ans;
}

int main(){
  const int MAX_TIME_LIMIT = 10000; //10 seconds or 10 * 1000 milliseconds
  long start_time = clock();
  
  int ans = ask("What is 2 + 2 * 5 = ");
  int ans2 = ask("What is 6 mod 2 = ");
  int numOfCorrect = 0;
  long end_time = clock();
  if( end_time - start_time > MAX_TIME_LIMIT){ 
      cout << "Time limit exceeded\n"; 
   }
  if( ans == 12){ numOfCorrect++; }
  if( ans2 == 0){ numOfCorrect++; }

  cout << "You got " << numOfCorrect << " correct answer out of 2 question\n";
  cout << "Your percentage is " << 100*(numOfCorrect/2.0f) << endl;

 return 0;
}

No guarantees in the above code.

Thanks for the reply. The code above only writes "time exceeded" after the person has finished the quiz. Is it possible to prompt the command even if the user doesnt enter anything? Lets say he's only less than halfway through the quiz.

Well you can check after each answer if the time has been exceeded.

Thanks for the reply. The code above only writes "time exceeded" after the person has finished the quiz. Is it possible to prompt the command even if the user doesnt enter anything? Lets say he's only less than halfway through the quiz.

It is possible, and it is called "threading".

The problem is that code like this is executed one line at a time. So, there's no way your program can count down AND wait for your input at the same time, without threading.

It basically means you assign part of the program called thread to do the countdown, while at the same time your main program waits your input.

The entire coding and process isn't quite simple, and quite honestly, i don't know how to do it, but if you really want, you can look up some tutorials. Have fun

#include <iostream>
#include <conio.h>
#include <Windows.h>

using namespace std;

void main(){
    char a,b=' ',e=' ';
    int t=0;
    for(int i=60;i>=1;i--){
        system("cls");
        cout<<i<<" ";
        cout<<"\nConfirma prin ENTER.\nCum zici? \n a)da;    b)nu\nc)poate  d)sau\nRaspuns : ";
        if(_kbhit()){
            a=_getch();
            if(int(a)==13){
                e=a;
            }
            else
                b=a;
        }
        cout<<b;
        if(int(e)==13&&b=='d'){
            t=1;
            break;
        }
        else
            if(int(e)==13&&b!='d')
                break;
        Sleep(1000);    
    }
    system("cls");
    if(t==0)
        cout<<"time is up or wrong answear, looser";
    else
        cout<<"congrats!!!!";
    _getch();
}

este posibil si fara threading :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.