954,506 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

alternative of clrscr() in C++

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.. :)

bhoot_jb
Junior Poster in Training
89 posts since Mar 2008
Reputation Points: 57
Solved Threads: 2
 

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.

Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
 
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. ... :)

bhoot_jb
Junior Poster in Training
89 posts since Mar 2008
Reputation Points: 57
Solved Threads: 2
 

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.

Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
 

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().... :)

bhoot_jb
Junior Poster in Training
89 posts since Mar 2008
Reputation Points: 57
Solved Threads: 2
 

>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 );
}
Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

>>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
Junior Poster in Training
89 posts since Mar 2008
Reputation Points: 57
Solved Threads: 2
 

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.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

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

bhoot_jb
Junior Poster in Training
89 posts since Mar 2008
Reputation Points: 57
Solved Threads: 2
 

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

novice420
Newbie Poster
6 posts since Mar 2008
Reputation Points: 10
Solved Threads: 0
 

dont use int with such a big number .

Majestics
Practically a Master Poster
621 posts since Jul 2007
Reputation Points: 199
Solved Threads: 49
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You