0

I am making a program like this:

void main()
{
  cout << "..."; /*large text*/
  cout << endl << "..."; /*intructions for word game*/
  cout << endl << "press enter to conitnue";
  getch();
}

Now I want to erase all the instructions after a user presses enter and start the game but want to conserve the large text.

Is there any way to clear a specific part of the screen?

Thanks in advance..

RK Tilak
  • 11
  • 1
  • 3

2 Answers2

0

The behavior of the console (a.k.a. cout) is text only, like a typewriter or teletype. This is what the C++ standard language can guarantee. Anything else requires operating system or platform specific functionality.

Ancient Turbo C++ & Borland C++ compilers had a gotoxy function that allowed you to position the cursor (and if my memory serves, the compiler also supported a erase to end of line). These could be used to clear a rectangular area on the console. Read the documentation on their graphics capabilities.

You may want to check out *Cursor Positioning Libraries", such as NCurses.

You could use the ancient art or redrawing (reprinting) the console (not clearing it). For example, if your terminal (console) had 25 lines, you would print 25 lines to make the old stuff go away.

However, you may want to consider dropping the console application and use a GUI system. GUI support is not part of the C++ language, so you will need to search the internet for "C++ GUI Framework" and pick one that you can learn and suits your requirements.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
-1

You can't after it flushes, which it is.

Easiest way to do it would be to write the large text, write the instructions, then when getch() is triggered, clear the screen and re-write the large text.

To clear the screen, you could do something similar to:

#include <cstdlib>
void clearScreen()
{
    #ifdef WINDOWS
        std::system("cls");
    #else
        std::system("clear");
    #endif
}

If you figure some different method and do decide to remove it before flushing, I would check out some similar examples, such as how a console progress bar works.

Treyten Carey
  • 641
  • 7
  • 17
  • 1
    Read the question again: he's using Turbo C++. That's from ms-dos era. (and anyway, incurring the cost of spawning an external process, loading and linking an executable initializing all the libraries, then freeing those resources again, just for the purpose of writing a few bytes to a stream should be a felony). – spectras Sep 01 '17 at 15:33