C++ delay() problem

Hi guys,
I am trying to make a program that output "Loading", then delay for 1 sec, then output ".", then delay for 1 sec, then output "." Just like "Loading..."

This is my code:
#include <iostream>
#include <ctime>
int main()
{
cout << "Loading";
delay(30000000); // About a second
cout << ".";
delay (30000000);
cout << ".";
delay(30000000);
cout << ".";
return 0;
}

but the program just waited for 3 seconds and outputted "loading..." at once.
could someone help me solve this problem please?
What are you, a Microsoft developer?

Jokes aside, delay() isn't a valid function as far as I can tell. If you're using C++11 or greater, include <thread> and <chrono> and use sleep_for.
Whatever method you use to kill time, you need to flush the output stream.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <ctime>

int main()
{
    std::cout << "Loading" << flush;
    delay(30000000); // About a second
    std::cout << "." << flush;
    delay(30000000);
    std::cout << "." << flush;
    delay(30000000);
    std::cout << "." << flush;
    return 0;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
//this use system processor speed for delay
#include<iostream>
#include<ctime>
using namespace std;
int main()
{
	float sec = 3;// specify the number of seconds here.
	clock_t start = clock();
	clock_t delay = sec * CLOCKS_PER_SEC;
	while(clock()-start<delay);
	return 0;
}
Last edited on
Thanks! You guys let me learned a lot of new stuff
Topic archived. No new replies allowed.