3
\$\begingroup\$

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

\$\endgroup\$
3
  • \$\begingroup\$ As a query - Is this in a windows environment? \$\endgroup\$ Commented Apr 10, 2013 at 10:13
  • \$\begingroup\$ yes this is, I will make an edit to make this more clear \$\endgroup\$ Commented Apr 10, 2013 at 10:16
  • \$\begingroup\$ Got an answer for ya. \$\endgroup\$ Commented Apr 10, 2013 at 10:42

3 Answers 3

6
\$\begingroup\$

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");
}
\$\endgroup\$
2
  • \$\begingroup\$ -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. \$\endgroup\$ Commented Apr 10, 2013 at 14:30
  • \$\begingroup\$ @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 \$\endgroup\$ Commented Apr 30, 2013 at 9:16
4
\$\begingroup\$

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.

\$\endgroup\$
2
1
\$\begingroup\$

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.

\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.