2

I would like to write a C++ program on linux gcc, such a way that time should be displayed on the right top (keeps changing ) and also make other processes to go on.

For Example:

I want time to be displayed on right top and also want to perform some operations like basic calcs on the same screen...

. I knew to display time continously using this snippet

#include<iostream.h>
int main()
{
while(1)
{
system("clear");
system("date +%r&");
sleep(1); 
}
return 0;
}

but each time , 1) it clears the screen , so the other instructions on screen also gets cleared 2) i also like to know how to make both the processes run at the same time??

Using bg etc would help?

deep
  • 29
  • 1
  • 2
  • To do other stuff in your program at the same time, split the two tasks into separate threads. You can use pthreads, or the builtin threading functionality of C++11 if you have an up-to-date compiler. You should be able to find plenty of information and guides for both of these on the 'net. – Michael Aug 02 '12 at 15:11

4 Answers4

2

There are two parts to your question.

First part: how to output time at a fixed location without disrupting other output on the screen.

Low-level approach:

High-level approach: use a text-based UI library, such as curses/ncurses.


Second part: how to update the time display in parallel to other activities.

In the simple case, you can just call time update function periodically from some places in your code you know will be executed regularly enough.

In the more complicated case, you will need to update time from a separate thread of execution. There is a lot said about multithreading, including on this site; unfortunately I can't recommend any specific introductory material offhand, but there are many.

[EDIT] In case you just want to run another program in the background, as @ecatmur suggests, you don't need threads; just use system("program &"), or fork+exec on Unix-ish systems and _spawn on Windows.

Community
  • 1
  • 1
atzz
  • 17,507
  • 3
  • 35
  • 35
1

Here is one that displays the time.

#include <windows.h>
#include <conio.h>
#include <stdio.h>
#include<time.h>



int ch=0;
time_t now;

void gotoxy(int x, int y)
{
    COORD coord;
    coord.X = x; coord.Y = y;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
    return;
}

void setcolor(WORD color)
{
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),color);
    return;
}

void clrscr()
{
    COORD coordScreen = { 0, 0 };
    DWORD cCharsWritten;
    CONSOLE_SCREEN_BUFFER_INFO csbi;
    DWORD dwConSize;
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

    GetConsoleScreenBufferInfo(hConsole, &csbi);
    dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
    FillConsoleOutputCharacter(hConsole, TEXT(' '), dwConSize, coordScreen, &cCharsWritten);
    GetConsoleScreenBufferInfo(hConsole, &csbi);
    FillConsoleOutputAttribute(hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten);
    SetConsoleCursorPosition(hConsole, coordScreen);
    return;
}

void getkey(void)
{
  if (kbhit())
  {
    ch=getch();

  }
}



int main(void)
{



    while (ch!=27)
    {
        getkey();   
        time(&now);

        gotoxy(50,1) ;
        setcolor(31);
        printf("%s", ctime(&now));
        setcolor(0);
    }

    setcolor(7);
    clrscr();

    gotoxy(2,23) ;
    return 0;

}
Software_Designer
  • 8,490
  • 3
  • 24
  • 28
0

here is my question and answer to write things on some location of the screen in c++, changing cursor location without using the windows handle. Qbasic imitation is slow

here is @pumpkins' question for threads Running a function in a thread

Community
  • 1
  • 1
huseyin tugrul buyukisik
  • 11,469
  • 4
  • 45
  • 97
0

That is not a trivial problem, because under Unix, programs take complete control of the terminal while they are running, which means that any program you run after yours would assume that it can use the space where you are displaying the clock, and would furthermore assume that when it positions the cursor at a specific spot, it will remain there.

The only way to have other programs avoid a specific area is to not give them access to the terminal in the first place, but rather hand them a pseudo terminal and interpret everything they write to it (this is how both xterm and screen work); this is non-trivial as there are also lots of control sequences for setting foreground and background color, repositioning the cursor, changing the area that is scrolled automatically etc.).

Simon Richter
  • 28,572
  • 1
  • 42
  • 64