Controlling couts

Aug 22, 2013 at 7:17am
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 Aug 22, 2013 at 7:17am
Aug 22, 2013 at 7:30am
closed account (28poGNh0)
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
Aug 25, 2013 at 1:40pm
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 Aug 25, 2013 at 1:40pm
Aug 25, 2013 at 2:53pm
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 Aug 25, 2013 at 2:54pm
Aug 26, 2013 at 12:53pm
closed account (28poGNh0)
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
Aug 26, 2013 at 4:57pm
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.
Aug 26, 2013 at 6:13pm
closed account (28poGNh0)
Thanks
Aug 26, 2013 at 8:04pm
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 Aug 26, 2013 at 8:07pm
Aug 26, 2013 at 8:46pm
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 Aug 26, 2013 at 8:49pm
Topic archived. No new replies allowed.