I tried making a guess the number game and I'd like to add a timer so the user can see in how many seconds he guessed. Can someone help me add a timer in seconds? Here is the code (don't mind the #include, I didn't know which one I needed xD):
#include <bits/stdc++.h>
#include <unistd.h>
#include <stdlib.h>
#include <iomanip>
using namespace std;
int main()
{
int randomnr, guesses=0, yournr, timer=0;
srand (time(NULL));
randomnr = rand () % 100;
cout << randomnr << "\n";
while (yournr != randomnr)
{
cin >> yournr;
if (yournr == randomnr && guesses == 0)
{
cout << "Wow you guessed it from the start!";
break;
}
if (yournr < randomnr)
{
cout << "Your number is lower than the random one! Try again!" << "\n";
guesses++;
}
else
if (yournr > randomnr)
{
cout << "Your number is bigger than the random one! Try again!" << "\n";
guesses++;
}
else
if (yournr == randomnr)
{
cout << "You guessed with " << guesses << " guesses and " << timer << " seconds, well done!" << "\n";
}
}
return 0;
}