1

I was wondering if it was possible to replace the text "[1] example1 [2] example2" after keyboard input with "example1extended" that's already on the screen. Something like system("CLS"); but for only a certain line of text.

int main() //just an example
{
   int ans;
   std::cout << "[1] example1 [2] example2" << std::endl;
   std::cout << "enter a choice: "; 
   std::cin >> ans;

   if (ans == 1)
   {
      std::cout << "example1extended" << std::endl;
   }
}
clyp efor
  • 19
  • 1
  • 5
  • Unless you modify the frame buffer directly, or find a package or utility that's intended to do this, no. – roelofs Nov 29 '17 at 06:16
  • Possible duplicate of [Clearing only part of the console output](https://stackoverflow.com/questions/15063076/clearing-only-part-of-the-console-output) – roelofs Nov 29 '17 at 06:22
  • 1
    Look up the curses library https://en.wikipedia.org/wiki/Curses_(programming_library) – Ed Heal Nov 29 '17 at 06:24

2 Answers2

3

At first: There is no "screen" for c++. There is only input and output to "something" which is a typically a terminal. But how this terminal behaves is not part of c++ standard and as this not portable. So the results with different terminals and especially on different OS are different.

If you are working with a terminal which have e.g. VT100 support, you can use the special characters to control the cursor and erase chars on the terminal screen. https://en.wikipedia.org/wiki/VT100 https://www.csie.ntu.edu.tw/~r92094/c++/VT100.html

There are hundreds of libraries arround which are dealing with such kind of terminal ( emulators ).

Klaus
  • 24,205
  • 7
  • 58
  • 113
1

There's no platform independent way of doing that but since you mentioned system('cls') I assume you are on Windows.

Windows has the Console Functions API which is a set of utility functions used for manipulating the console. You have 2 options here:

Set the cursor's position and overwrite it with spaces:

#include <windows.h>
..
auto consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
COORD coordsToDelete = { row, col }; // the coordinates of what you want to delete
// Move the current cursor position back. Writing with std::cout will
// now print on those coordinates
::SetConsoleCursorPosition(consoleHandle, position);
// This will replace the character at (row, col) with space.
// Repeat as many times as you need to clear the line
std::cout << " ";

Alternatively, you can get the entire console buffer and directly modify it:

#include <windows.h>
..
auto consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbi;
// Get a handle to the console buffer
GetConsoleScreenBufferInfo(consoleHandle, &csbi));
DWORD count;
COORD coords = { row, 0 };
DWORD cellCount = /* the length of your row */;
// Write the whitespace character to the coordinates  in cellCount number of cells
// starting from coords. This effectively erases whatever has been written in those cells
FillConsoleOutputCharacter(
        consoleHandle,
        (TCHAR) ' ',
        cellCount,
        coords,
        &count);
Nikola Dimitroff
  • 6,127
  • 2
  • 25
  • 31