Controlling couts

Hello guys, i just wanna ask how to control couts. for example:
cout<<"Hello"<<endl;
cout<<"end"<<endl;
how to control it so it will display:
Hello
//after few seconds for example 2 seconds
end

please reply if you don't understand my question. Thanks :D
Last edited on
This is not a couts controlling! you need a time related function to do such

This is one way to do it

1
2
3
4
5
6
7
8
9
10
11
12
# include <iostream>
# include <ctime>
using namespace std;

int main()
{
    cout << "Hello" << endl;
    while(2000-clock()>0);
    cout << "End" << endl;

    return 0;
}


hope that helps
i just use the word control because i can't express my self.. anyway, (2000-clock()>0) would show end 2s after hello ?
Last edited on
anyway, (2000-clock()>0) would show end 2s after hello ?


It might. It might not. The granularity of clock is implementation defined as is the initial value returned from clock at the beginning of a program (it is not required to be 0.) Spinning in a loop doing nothing is not a great recommendation unless you like 100% utilization of your cpu checking to see if it's done waiting. Use an operating specific function (such as Sleep on Windows) or if you have C++11 support:

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

int main()
{
    const char* helloWorld = "Hello world!";
    auto delay = std::chrono::milliseconds(500);

    for (const char* p = helloWorld; *p; ++p)
    {
        std::cout << *p;
        std::this_thread::sleep_for(delay);
    }
}
Last edited on
Hiii @cire I am using code::blocks 10.05 ,when I compile your code It gave me an error

error: 'std::this_thread' has not been declared
Can you know why
Hiii @cire I am using code::blocks 10.05 ,


Your compiler version would be more relevant than that of your IDE, although you're two major versions behind on Code::Blocks (which may mean you would need to specify the option for C++11 support to it rather than just click an option like so: http://s17.postimg.org/l9nhiir3j/Code_Blocks_Compiler_Settings.png )


error: 'std::this_thread' has not been declared

Can you know why


cire wrote:
Use an operating specific function (such as Sleep on Windows) or if you have C++11 support:


It would seem your compiler doesn't support C++11 in it's current configuration.
Thanks
Here's something you can do without C++11:
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <windows.h>
using namespace std;

int main()
{
    std::cout << "Hello" << std::endl;
    Sleep(2000);
    std::cout << "End" << std::endl;

    return 0;
}


Or in linux:
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <unistd.h>
using namespace std;

int main()
{
    std::cout << "Hello" << std::endl;
    usleep(2);
    std::cout << "End" << std::endl;

    return 0;
}
Last edited on
Hi, the code below should help make it clear how to delay the "End" Message for a specific amount of time:

1
2
3
4
5
6
7
int seconds = 2;

cout<<"Hello"<<endl;
Sleep(seconds);

cout<<"End"<<endl;
Sleep(seconds);


The Sleep() function waits X amount of seconds before executing the next piece of code. But remember to include the #include <ctime> header file.

PS: If you are using a mac drop the casing on the Sleep() method.
Last edited on
Topic archived. No new replies allowed.