Hello everyone! I have lately started learning multi-tasking to use it in my project, but I have a small problem while printing messages. I have 3 tasks that I want to run at the same time.
1-) Countdown
2-) Printing time
3-) Whatever function, printing a message and asking for a value.
Here is my code:
Code:
#include <stdlib.h>#include <stdio.h>
#include <windows.h>
#include <ctype.h>
#define TRUE 1
int minutes = 0;
int seconds = 0;
int value;
DWORD countdown() {
while(TRUE) {
printf("\r \t\t %2dm : %2ds",minutes,seconds);
Sleep(1000);
seconds++;
if(seconds == 60) {
minutes++;
seconds = 0;
}
}
}
DWORD print_time() {
while (TRUE) {
printf("\r \t\t %2dm : %2ds", minutes, seconds);
}
}
DWORD whatever() {
printf("Please enter a value:");
scanf("%d",&value);
}
int main() {
HANDLE thread1,thread2,thread3;
thread1 = CreateThread(NULL, 0, countdown, "", 0, NULL);
thread2 = CreateThread(NULL, 0, print_time, "", 0, NULL);
thread3 = CreateThread(NULL, 0, whatever, "", 0, NULL);
WaitForSingleObject(thread1, INFINITE);
WaitForSingleObject(thread2, INFINITE);
WaitForSingleObject(thread3, INFINITE);
}
Expected results:
0m:10s Please enter a value: 123
All on the same line.
However, the results that I get are:
0m:10s3 Please enter a value:
So as you can see, it writes the last character of the value after printing the time and not after the message. So the text is being overwritten. I think the problem is from \r but I am not sure though.
I hope someone could help me. Thanks and happy new year!