heyy friends..
i m a beginner in C++ and am currently workin wid a program..

just for clrscr(), i have to include the whole conio.h file in my program inorder to clear the screen..
so is there any alternative to clrscr()??
please throw some light on this topic.. :)

Recommended Answers

All 10 Replies

To my knowledge, there is no standard way to clear the screen in C/C++, because that's not the way the console was designed. If you want to clear the screen then you have to do it using some non-standard way. You can write your own function to do this or use a third party function like clrscr(). I'll be happy to learn along side you if I'm wrong.

To my knowledge, there is no standard way to clear the screen in C/C++, because that's not the way the console was designed. If you want to clear the screen then you have to do it using some non-standard way. You can write your own function to do this or use a third party function like clrscr(). I'll be happy to learn along side you if I'm wrong.

heyy yaa...i encountered the same answer as given by you in different sites...so i agree with you...but then i wud like u to tell me if its good to use system('cls') (or watever it is..) ???
is it risky ??
or shall i create a loop of '\n' so that it will jst bring a new screen in front of me...???

which one is a better option ?? yaa...any other more feasible option is welcome. ... :)

You've lised the most common options I'm aware of. I'd go with the clrscr() if I really had to clear the screen. My least favorite would be the system('cls'), because I always avoid system() calls if possible.

well...i told i m a beginner in C++...so dnt kno much about d stuffs... :) so naturaly i wud prefer the most common ways...
neways...thnx for d help...rite now i wud better go on wid clrscr().... :)

>i wud like u to tell me if its good to use system('cls')
No, it's a big security risk, and the system function is painfully slow.

>or shall i create a loop of '\n' so that it will jst bring a new screen in front of me...???
This is your most portable option, but you can't tell how many newlines to print, and the cursor will be at the bottom of the screen rather than the top as it would be in a true screen clear.

>any other more feasible option is welcome.
Here's a solution that's portable across Windows, but it won't work on other operating systems without emulation:

#include <windows.h>

void clear_screen()
{
  DWORD n;                         /* Number of characters written */
  DWORD size;                      /* number of visible characters */
  COORD coord = {0};               /* Top left screen position */
  CONSOLE_SCREEN_BUFFER_INFO csbi;

  /* Get a handle to the console */
  HANDLE h = GetStdHandle ( STD_OUTPUT_HANDLE );

  GetConsoleScreenBufferInfo ( h, &csbi );

  /* Find the number of characters to overwrite */
  size = csbi.dwSize.X * csbi.dwSize.Y;

  /* Overwrite the screen buffer with whitespace */
  FillConsoleOutputCharacter ( h, TEXT ( ' ' ), size, coord, &n );
  GetConsoleScreenBufferInfo ( h, &csbi );
  FillConsoleOutputAttribute ( h, csbi.wAttributes, size, coord, &n );

  /* Reset the cursor to the top left position */
  SetConsoleCursorPosition ( h, coord );
}
commented: Narue is awesome! +0

>>No, it's a big security risk, and the system function is painfully slow.

thnxx...i wont ever use dis thing... :)

>>This is your most portable option, but you can't tell how many newlines to print, and the cursor will be at the bottom of the screen rather than the top as it would be in a true screen clear.

u r ritee...tho it clears d screen, d cursor will be at the bottom which is undesirable...actualy dat issue went out of my mind while i was replying d post.. :|

>>Here's a solution that's portable across Windows, but it won't work on other operating systems without emulation:

well...thnxx for d solution...but d question of includin another file still remains...(windows.h here)... :)
i had actualy thot of accomplishin d task using only iostream.h (which is d only file used by me other dan conio.h in my program)....
neways....i wud continue wid clrscr() for d time being.... :)
because i dnt need to run d code using a compiler except Borland's... :)

bhoot_jb, could you please spell your words correctly? I'm good at translating script kiddie gibberish, and I was having a hard time understanding your post.

ohkkk narue...
sorry for that...actually i am used to type english in such a rubbish way :(...anyways..i wont do so again... :)

for(int i=0;i<20000000000000000000000000000000000000000000;i++)
cout<<"/b";

dont use int with such a big number .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.