Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hello everybody...
I have a program doing a job
I want to wait a little bit to do something(do not care about it) and at the same time I want to print something with a delay,but It doesn't work as I expected.

Code(a part of it):

What I have tried:

C++
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>


void delay(int number_of_seconds) ;
void Loading();

int main(void)
{
    Loading();
}


void delay(int number_of_seconds) 
{ 
    // Converting time into milli_seconds 
    int milli_seconds = 1000 * number_of_seconds; 
  
    // Storing start time 
    clock_t start_time = clock(); 
  
    // looping till required time is not achieved 
    while (clock() < start_time + milli_seconds); 
} 

void Loading()
{
    char *array[4] = {"/" , "--" , "\\" , "|"};
    
    for(int j = 1; j <= 8; j++)
    {
        for(int i = 0; i <= 3; i++)
        {
            printf("%s" , array[i]);
            delay(100);
            printf("\b");
            printf(" ");
            delay(100);
            printf("\b");
        }
    }
}

I want to print "/" and after a delay of 0,1 sec. to print the next one string of the array at the same position(I use backslash + b to do it).
The problem is that in the output after some seconds I receive this: "--------"
Thanks!!!
Posted
Updated 11-Sep-20 7:30am

It works (at least on my Linux box) if you flush the standard out:
C
void Loading()
{
    char *array[4] = {"/" , "--" , "\\" , "|"};

    for(int j = 1; j <= 8; j++)
    {
        for(int i = 0; i <= 3; i++)
        {
            printf("%s" , array[i]);
            fflush(stdout); //<---
            delay(100);
            printf("\b");
            printf(" ");
            fflush(stdout); //<---
            delay(100);
            printf("\b");
        }
    }
}
 
Share this answer
 
Comments
Nick_is_asking 11-Sep-20 6:56am    
Thanks.It works fine...
CPallini 11-Sep-20 7:07am    
You are welcome.
Richard MacCutchan 11-Sep-20 7:11am    
+5; I had forgotten about that.
CPallini 11-Sep-20 7:51am    
Because nowdays you are used to those fancy-flashy scripting languages... :-D
Thank you very much, Richard.
Richard MacCutchan 11-Sep-20 7:57am    
And because the word 'flush' has a different association in my brain. :)
This isnt a good idea, because your while has some workload and to uses energy and cpu time. Better is to use some os specific wait function like sleep() or nanosleep().
 
Share this answer
 
Comments
Rick York 11-Sep-20 17:17pm    
I couldn't agree more. Spin loops without waiting are rarely a good idea.
The problem is that the print statements send some data to the output stream. The code then immediately goes into the delay loop which prevents any output operations from being performed, so they queue up and only get printed at some later time. You can manage things in a Windows application with background processing and timers, but console applications cannot easily use those features.

[edit]
Carlo, as usual, has the answer.
[/edit]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900