Have a look on this code
#include<stdio.h>
#include<iostream.h>
#include<conio.h>
#include<dos.h>
void TIME()
{
struct time t;
for(; ;)
{
gotoxy(1,1);
gettime(&t);
printf("The current time is: %2d:%02d:%02d\n", t.ti_hour, t.ti_min, t.ti_sec);
}
}
void main()
{
int n;
TIME();
gotoxy(5,6);
cout<<"Enter a number: ";
cin>>n;
cout<<"Thank you";
cout<<"\nEntered no.: "<<n;
getch();
}
My problem is that I want to show the current time(like a clock) and simultaneously run the code in main()... I mean to say that is it possible that TIME() can be run alongwith and allowing to enter data also on other functions like multi-tasking...
By the way I've heard about some thing called multi-threading... Will I have to go that way. Please help and suggest the required codes!!
Hello, vivekkrs. You can do what you want. You have to use threads, probably std::thread.
Read up on them: http://www.cplusplus.com/reference/thread/thread/
You need to have a C++11 compliant compiler though.
the problem is that you have a single screen. Whenever you say gotoxy() that's the position where the next output will appear.
when you make a thread you need to preserve the current position before the output of the time and restore it afterwards. Even then input and outup at the same time doesn't seem to be a good idea.
this is one option how you start a thread for windows:
Actually, I was using gotoxy() to fix a particular display position for my time and I've used the for loop to make it run continuously like a clock.....If u have a another code then u can suggest me. What I want is that to run the clock (by some means) and at the same time allow the user to work their normal task.. :)