1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

clear screen function

Discussion in 'Mac Programming' started by lights, Dec 15, 2011.

  1. macrumors newbie

    Hello guys,

    I have a questions, I'm using Xcode to write my my console program and if i include the windows.h header files, I get an error.

    I've looked around online and there is something called MacWindows.h

    I put that on, and it's still giving me problems. Does anyone know how to properly do it?

  2. macrumors 6502a

    <windows.h> is particular to Microsoft Windows.

    From your other threads my impression is that you are doing console programming, is that correct?

    What functions do you seem to be missing?
  3. macrumors 6502a

    If you are doing console programming it might be useful to know that Terminal.app handles ANSI escape sequences as documented at <http://en.wikipedia.org/wiki/ANSI_escape_code>

    See the Table titled: "Some ANSI escape sequences"

    With this in mind the following will clear the screen and place the cursor at x:0 y:0

    std::cout << "\033[2J\033[0;0H";	// CSI n J and CSI n; m H
  4. macrumors newbie

    Yes I am doing console programming.

    I am using this, to do a clear screen on my banking program. Basically it's a menu driven program and when the user goes into an option, I clear the menu screen and execute the function the user entered.

    What Xcode is doing is, it's telling me that they can't find some of the header files. Well I looked online and some people said that I can use the MacWindows.h to reference it, but its still giving me problems.

    I'm not looking for a escape sequences that I can press to exit the program.

    I hate having to use Visual studio 2010, then when i work mobile, I use my MBP and I have to comment out all the clearScreen calls. But I do love the debugging window of VS2010!

    Thanks again Llyoddean!

    void clearScreen()
      HANDLE                     hStdOut;
      DWORD                      count;
      DWORD                      cellCount;
      COORD                      homeCoords = { 0, 0 };
      hStdOut = GetStdHandle( STD_OUTPUT_HANDLE );
      if (hStdOut == INVALID_HANDLE_VALUE) return;
      /* Get the number of cells in the current buffer */
      if (!GetConsoleScreenBufferInfo( hStdOut, &csbi )) return;
      cellCount = csbi.dwSize.X *csbi.dwSize.Y;
      /* Fill the entire buffer with spaces */
      if (!FillConsoleOutputCharacter(
        (TCHAR) ' ',
        )) return;
      /* Fill the entire buffer with the current colors and attributes */
      if (!FillConsoleOutputAttribute(
        )) return;
      /* Move the cursor home */
      SetConsoleCursorPosition( hStdOut, homeCoords );
  5. macrumors 6502a

    I think you're misunderstanding the term "escape sequences".

    Escape sequences, as used here, refer to a series of characters that the terminal interprets as commands rather then simply placing on the display.

    I'll look at you code shortly.


    Using ANSI escape code sequence clear screen can be implemented as:

    void clearScreen()
    	// See Table titled: "Some ANSI escape sequences"
    	// <http://en.wikipedia.org/wiki/ANSI_escape_code>
    	std::cout << "\033[2J";				// Clear Screen:				CSI n J
    	std::cout << "\033[0;0H";			// Place cursor at row, column:	CSI n; m H
  6. Moderator


    Staff Member

    Is this not what the curses library was basically invented for?
  7. macrumors 6502a

    In order for some compatibility of his homework assignments with the windows.h and console header files he's required to use?

    Not at his level I would think! Quick and simple seems more right to me although feel free to suggest something fast and simple for a beginner if you so wish.
  8. macrumors 68040

    You could use conditional compiling as a quick hack, and just define clearScreen() as system("clear"). :D

    #ifndef __APPLE__
    #include <windows.h>
    #define clearScreen() system("clear");
  9. macrumors 603

    Exactly where did you look online, and exactly who said that MacWindows.h is the replacement? Post a specific URL where you read this.

    When I google MacWindows.h, I see this in the top hits:

    It's from 2008. And it pretty plainly says that MacWindows.h has no relationship to windows.h. It also links to a Wikipedia article for windows.h.

    Saying that you read something "online" without pointing to it is pretty much worthless in programming. Programming is about details. The exact location where you read something is a crucial detail.

    In general, the way to fix this is to isolate the functionality you want (say, screen clearing), and then make platform-specific functions. In particular, write your own clearscreen() function (or whatever it should be called in order to match "windows.h"), and then implement the ANSI escape-code output inside that function.

    This is a very common development pattern in the real world. You first define an API. Put it in a header. Then you define an implementation. Then you link the API, the implementation, and your program together.

    If part of the implementation is platform-specific, such as the emitting of escape sequences, then inside the implementation you write conditionally compiled code (#if, #else, etc.). The implementation cares about the platform. The rest of your code doesn't.

    In the long run, robbieduncan is exactly right: this is what curses was invented for. Don't reinvent the wheel, especially not for a well-known and well-solved problem like this.
  10. macrumors newbie

    I'm sorry if I did not include the link where I read that, or so I thought I read it. Maybe I interpreted it the wrong way because I really wanted that function to work.

    I just started programming a couple of months ago and still learning the ins and out.

    I will try that a bit later tonight. Thanks! =)

    I've not heard of curses library until you mentioned it, I'm looking around online and it's hard for me to understand exactly how some of those functions work. I will try to read it some more.

    Thanks for clarifying that for me, I thought escape sequences where a combination of key strokes that lets the user exit out. I remembered my professor saying something about ctrl ^ z or something in that nation and he called it escape sequence, I could have heard him wrong though. I'm wondering if that escape sequence that you posted works for both windows and mac? I will try it when I get home from work.

    Thanks for the help everyone, I've turned in my final project last week and I think I got the highest programming grade in the class plus a bunch of extra credit points. :D Sorry for the late response, I was recuperating from last week.

    So far, everyone in MacRumors has been helpful and offer a bunch of help to a beginner like myself.
  11. macrumors 6502a

    Astronauts have escape sequences.

    Airline passengers have escape sequences.

    You probably have a home fire escape sequence.

    It's overloaded terminology making what the professor said also correct. Almost nothing in programming does not make use of overloaded terminology. Don't assume the meaning of a word, or phrase, means what you think it does within a unfamiliar language, programming, industry, etc.
  12. macrumors 603

    Even the words "programming" and "software" are overloaded. I once did some work with some folks who helped produce TV shows (it was a game tie-in to a TV show under development). Their first thought on hearing "programming", "software", or "development" was markedly different from mine.

Share This Page