Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

How to I make a delay in C++?

0 votes
asked Jun 29, 2018 by BroudyMan (530 points)
#include <iostream>

using namespace std;

int main()

{

cout <<"Hello"<< endl;

//how do I delay?

cout <<"world."<< endl;

return 0;

}

2 Answers

+1 vote
answered Jul 1, 2018 by anonymous
#include <stdio.h>
#include <unistd.h>

int main()
{
    for(int i=0;i<5;i++) {
      printf("Hi.."); 
      fflush(stdout);
      usleep(1000000); // wait for 1000000 micro seconds
    }

    return 0;
}
commented Jul 5, 2018 by yasser
i just tried to use" cout" instead of "printf" but it didn't work,do you know why?
commented Jul 7, 2018 by anonymous
cout << :Hi.."<<endl;
that's the c++ equivalent for printf
commented Jan 4, 2020 by Mahesh Morem (200 points)
you should also change the stdio.h as iostream.h
0 votes
answered Sep 28, 2018 by LeoSar
#include <iostream>
#include <chrono>
#include <thread>

int main()
{
    std::cout<<"Hello World\n";
    std::this_thread::sleep_for(std::chrono::milliseconds(20)); //delay
    std::cout << "Hello World Again\n";
    return 0;
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...