Is it Possible!!!!

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!!
Last edited on
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.

Does your compiler allow void main()? int main is standard practice and it is wise to follow that. Please use code tags. :D http://www.cplusplus.com/search.do?q=using+code+tags
Last edited on
Yes, it does!!

Here's the code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#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();
}



And also thanks for the code article! :)
Help!! Plz.... require it for my project
???
Please help...
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:

http://msdn.microsoft.com/de-de/library/kdzttdcb%28v=vs.90%29.aspx

there's an example for the console


You might be interested in pdcurses:

http://pdcurses.sourceforge.net/
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.. :)
????
Follow the link. Everything you need [for windows] is there.

Here is the solution for windows (I cannot compile the ancient borland stuff hence removed it):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include<iostream>
#include <windows.h>
#include <process.h>    /* _beginthread, _endthread */
#include <stddef.h>
#include <stdlib.h>
#include <conio.h>
#include<ctime>

using namespace std;

bool is_running = true;
bool is_done = false;
void TIME(void *)
{
  COORD c;
  c.X = 1;
  c.Y = 1;
      char buffer [1024];
     time_t t;
     while(is_running)
    {
      time (&t);
      strftime (buffer,sizeof(buffer),"The current time is: %X",localtime(&t));

      DWORD   result;
      WriteConsoleOutputCharacter( GetStdHandle( STD_OUTPUT_HANDLE ), buffer, strlen(buffer), c, &result );

      Sleep(500);
    }
    is_done = true;
    _endthread();
}

int main()
{
    int n;
    _beginthread(TIME, 0, NULL);

    COORD c;
    c.X = 5;
    c.Y = 6;
    SetConsoleCursorPosition(GetStdHandle( STD_OUTPUT_HANDLE ), c);

    cout<<"Enter a number: ";
    cin>>n;
    cout<<"Thank you";
    cout<<"\nEntered no.: "<<n;

    is_running = false;
    while(!is_done)
      Sleep(100);

  getch();
  return 0;
}
Thanks... and which compiler do u use??
GCC with code::blocks (IDE)

and

Visual C++ (Express)
:)
Topic archived. No new replies allowed.