Create a delay between each output?

Hey Everyone,

I have been working on a simple 'fight' program where 2 players fight each other and one comes out victorious. step by step i have been adding things and now i want to add a time delay between each output so that it doesn't all pop up at once. (i know my code is bad, im new and will learn over time how to make it better, hopefully)

Long story short: I want a delay between each output. I've tried Sleep() but it sometimes will just stop the program and output "Time limit Exceeded". any advice is appreciated!

Here is what i have so far:

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
58
59
60
61
62
63
64
  #include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;

int ROLL_DICE(int sides);

class Player{
    private:
        int hp;
        string name;
    public:
        Player(string myname, int myhp){
            name = myname;
            hp = myhp;
        }
        Player(){
            name = "blank";
            hp = 10;
        }
        string GetName() const {
            return name;
        }
        int GetHp() const {
            return hp;
        }
        void GetHurt(){
            int damage;
            damage = ROLL_DICE(6);
            hp = hp - damage;
        }

  
};

int ROLL_DICE(int sides){
    int roll = 1 + (rand()%sides);
    if (roll == sides)
        cout << "CRITICAL!\n";
    return roll;
}

int main() {
    srand(time(0)); // using the current time to generate a random seed to ensure a true random number
    int damage;
    Player Josh("Josh",20);
    Player Curie("Curie", 20);
    cout << "Welcome to the arena " << Josh.GetName() << " and " << Curie.GetName() << "!" << endl;
    cout << Curie.GetName() << ", your HP is " << Curie.GetHp() << "." << endl;
    cout << Josh.GetName() << ", your HP is " << Josh.GetHp() << "." << endl;
    cout << "Fight!\n";
    while (Josh.GetHp() > 0 && Curie.GetHp() > 0){
        Josh.GetHurt();
        cout << Josh.GetName() << "'s" << " new HP is " << Josh.GetHp() << endl;
        Curie.GetHurt();
        cout << Curie.GetName() << "'s" << " new HP is " << Curie.GetHp() << endl;
    }
    if (Josh.GetHp() > 0)
        cout << "Josh Won!";
    else
        cout << "Curie Won!";
	return 0;
    }
Last edited on
Maybe something like std::this_thread::sleep_for(std::chrono::seconds(1)); C++ 11

More info is here: http://www.cplusplus.com/reference/thread/this_thread/sleep_for/
now do i have to put that in between each input to make it delay between each output? or can i just put it in there once so that it always delays?
put it in every place that you want the delay

> I've tried Sleep() but it sometimes will just stop the program and output "Time limit Exceeded"
¿? ¿what are you using to execute your code?
Yes, put it where you want it to sleep for 1 second.

If you want to use it multiple times, you could factor the printing out to a function perhaps. But that might be more trouble than it's worth.

I've tried Sleep() but it sometimes will just stop the program and output "Time limit Exceeded".

You should be doing this on your actual computer, not some unreliable web application.
oh okay i see, I have been using the solo learn website. so that makes since. Thanks yall!
Topic archived. No new replies allowed.