# How to remove text from a command line box?

I'm currently writting a text based game and am currently having a slight issue where all of the text on the screen keeps going up and have had a couple of the testers complaining that it makes the game appear messy and somewhat confusing.

The game is written in C++ as a command line game and I was wondering if anyone knows of anyway that I could clear the screen up without putting a lot of white space into the game as I personally think that isnt a very nice way of doing it. Just to note that this is in a windows environment.

Thanks for any response

• As a query - Is this in a windows environment? – Tom 'Blue' Piddock Apr 10 '13 at 10:13
• yes this is, I will make an edit to make this more clear – Elliott Apr 10 '13 at 10:16
• Got an answer for ya. – Tom 'Blue' Piddock Apr 10 '13 at 10:42

## 3 Answers

EDIT: After a comment by @ChristianIvicevic I felt compelled to reword my answer to emphasise that the Article link I provided is a far better alternative to using a system call as it is more secure and does not risk producing false positives with anti-virus software.

Try and use this Microsoft solution:

Performing Clear Screen (CLS) in a Console Application

A system function to call clear screen on your application can work too but carries the risk of producing errors with anti-virus software as it is a low-level system call.

#include <stdlib.h>

void main()
{
system("cls");
}

• -1. Using system is resource heavy, insecure and can be recognized as a false positive by anti virus programs. Never ever use it when there is another alternative like the linked article. – Christian Ivicevic Apr 10 '13 at 14:30
• @ChristianIvicevic I did not realise that system calls could produce false positives. My C++ experience isn't the best so thank you for the comment, I've edited the answer to match – Tom 'Blue' Piddock Apr 30 '13 at 9:16

In case you ever need it in the future: For even more control of the display in consoles and cross platform support take a look at the ncurses library: https://en.wikipedia.org/wiki/Ncurses

It is a bit of an overkill for just clearing the screen, though to my knowledge it is currently the only portable way, but it also allows for colors, menus etc. Essentially it allows you to use the console like a screen.

• By making it more portable, do you mean that it would be usable on different platforms? – Elliott Apr 10 '13 at 13:12
• – gordonk Apr 11 '13 at 8:09

I would start the game from a Windows bat file. Before the bat file starts executing the program, configure the command prompt to the desired number of columns and lines. See these links for examples: http://www.computerhope.com/movehlp.htm or https://stackoverflow.com/questions/8688846/resize-command-prompt-through-commands
The relevant command is "mode con:cols=80 lines=100" You can pass the number of columns and lines into your C++ program as command line parameters and can use this information inside your program to help control how text is displayed, paginated, cleared, etc.