0

in was like to do display values of a dynamic integer on console application with out scrolling down the application background. the integer was passed in to thread by ref. thread will change the values if the integer.

currently i was doing it like this in the main method/ thread

int data;
thread t1((ProCount()),ref(data));
while(true){
cout<<"data :"<<data<<endl;
system("CLS");
}

but for CLS command will take lot of processing power is there any other decent way to do it??

Evan Carslake
  • 2,267
  • 15
  • 38
  • 56
gamal
  • 1,587
  • 2
  • 21
  • 43
  • 1
    You want to wait for the thread to signal a change before updating the display. A [condition variable](http://en.cppreference.com/w/cpp/thread/condition_variable) is good for that. – Mike Seymour Feb 20 '15 at 18:10
  • 1
    There's no standard c++ way to clear the console. That's OS/Terminal program specific. – πάντα ῥεῖ Feb 20 '15 at 18:10
  • 1
    possible duplicate of [How to change text while running the console](http://stackoverflow.com/questions/14043148/how-to-change-text-while-running-the-console) – GeekRiky Feb 20 '15 at 18:11
  • 1
    And if your application's interface is complex enough, look into [ncurses](http://en.wikipedia.org/wiki/Ncurses), one of the more common ways of creating TUI's – aruisdante Feb 20 '15 at 18:13
  • but i mainly wanted remove the CLS part and stop scrolling the console app.. so that the data will dynamic data integer veritable changes on at the same position of the console panel.. that was the thing i wanted to do – gamal Feb 20 '15 at 18:14
  • And what we're telling you is that there isn't a general way to do this (in C/C++). It's console/OS specific. There are many 3rd party libraries that allow creating of Text User Interfaces, like ncurses. – aruisdante Feb 20 '15 at 18:17
  • @gamal "CLS" is a windows command. If you're not concerned with portability and would accept a windows-specific solution, you should add that information to your question. – Kenster Feb 20 '15 at 21:12

2 Answers2

2

You might consider using ncurses:

http://hughm.cs.ukzn.ac.za/~murrellh/os/notes/ncurses.html

It exists for all cmd-line environments (Windows cmd prompts, Linux, MacOS, etc), and it provides a standard C/C++ interface.

ADDENDUM:

If all you want to do is reposition the cursor to re-write a value, you could also use the Windows API SetConsoleCursorPosition(). For example:

#include <windows.h> 

void gotoxy(int x, int y)
{
  COORD coord;
  coord.X = x;
  coord.Y = y;
  SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
FoggyDay
  • 11,962
  • 4
  • 34
  • 48
0

Try to print "\b" multiple time and add a sleep.

Zoruk
  • 41
  • 3