Saturday, November 23, 2019

-- How to clean your screen when coding C++ in a Mac OSx --

While working with a Mac Xcode is the most popular compiler, the problem is that Xcode uses a pseudo-terminal to run your code and the use of system("clear") will use give you an error such as TERM environment variable not set.

There is really no easy way to bypass that issue in Xcode, you could try to have a loop that introduces 250 or so blank endlines (endl) and it could look like the screen was cleared and you are refreshing it, but it could get messy.

The other alternative is to run your program from the OSx Terminal, for this you first navigate to where your .cpp code is, in my case it is in my desktop under the Test/Test so I navigate there using the cd (change directory) command, then I proceed to compile the C++ program using the make command as below. It creates an executable main without the .cpp extension

Next, I proceed to run the program

And below is the result as expected. You should only see one of the cout and that is why the screen was cleared and the first cout is gone.