简体   繁体   中英

Printing in c program with delay?

I'm trying to print something like:
Exiting...
into a terminal when the program ends, but the way I want it to be outputted to the terminal is each dot after "Exiting" needs to be printed with a slight delay after each other in the same line. Like it is loading to put it that way. But I have no idea how to make it print each of them separately without the whole line to be printed all at the same time. I'm mot sure if that is possible in c in the first place. I've tried adding delay between each printf using time but that did not work.

You can use it like this -

cout<<"Exiting";

cout.flush();
for (int j=0;j<1;j++) {
    for (int i = 0; i < 3; i++) {
        cout << ".";
        cout.flush();
        sleep(1);
    }
    cout << "\b\b\b   \b\b\b";
}
cout<<"\n\n";
sleep(1);

I was stuck with the same problem where I had to print something with delay. This thing works flawlessly.

PS - Use the required Header Files.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM