-3

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;
}
Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
  • 5
    Side note: [Why should I not #include ?](https://stackoverflow.com/q/31816095/12149471) – Andreas Wenzel Mar 28 '22 at 20:10
  • do you want the timer to be in realtime or do you just don't want to accept an answer if the user took too long? – Raildex Mar 28 '22 at 20:12
  • 2
    *Can someone help me add a timer in seconds?* -- What research have you done in doing this? What you're asking for is a somewhat broad topic. The type of timer, whether it will run on a different thread than the game, etc. etc. – PaulMcKenzie Mar 28 '22 at 20:12
  • You are already calling the function [`std::time`](https://en.cppreference.com/w/cpp/chrono/c/time) to initialize the random seed. Why don't you also use that function to remember the start time, and then call that function again at the end, and then subtract the end time from the start time? Then you will have the number of seconds the user needed. – Andreas Wenzel Mar 28 '22 at 20:14
  • 2
    What's with all the C++ standard tags? :( Can you pick one please? – Wyck Mar 28 '22 at 20:21

1 Answers1

0

Did some small changes to your code using chrono library:

// these two headers should do ;)
#include <iostream>
#include <chrono>
using namespace std;
using namespace chrono;

int main()
{
    int randomnr, guesses=0, yournr;
    system_clock::time_point start = system_clock::now();

    srand (start.time_since_epoch().count());
    // [...]
                    cout << "You guessed with " << guesses << " guesses and " << duration_cast<seconds>((system_clock::now() - start)).count() << " seconds, well done!" << "\n";
    // [...]
    return 0;
}

duration_cast helps you casting the interval between two time_points into a a given duration type (in this case, casting to seconds). Calling count() on the result returns the actual number (as seconds is a non-trivial type).

Didier
  • 146
  • 1
  • 4