I'm trying to pause every time he has done 1 of the cases. But it doesn't matter where I put sleep(1), it will sleep 5 seconds and than give all the outputs at once. Or when im asking for 10 outputs it will wait 10 seconds before giving me the output.
What I want is to get a output with 1 second in between the next one, and so on. This is my code and I have made it as small as possible so it won't take forever to read. I have tried to put sleep(1) on almost every spot but it all ended up the same.
//include list
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
// variable list
int i = 0;
int StopNow;
int start = 0;
int main(void)
{
srand(time(0));
while(i<5)
{
switch(rand() % 4 + 1) // picking a random number between 1 and 4.
{
case 1:
printf("\n 1");
break;
case 2:
printf("\n 2");
break;
case 3:
printf("\n 3");
break;
case 4:
printf("\n 4");
break;
}
i++;
sleep(1); // Pause
}
}
I tried to run your code on some online compiler https://www.onlinegdb.com/online_c++_compiler
I assume nothing wrong with your code, maybe compiler issues.
If it still doesn't work, probably there is some issue with your buffer.
Sometimes, characters don't get print until some buffer exceeded or the newline gets printed. There are 2 way you can try this
fflush(stdout)
call before the sleep
method to flush out the buffercout
library as the library automatically flush for you