Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   how to clear screen? (http://www.go4expert.com/forums/screen-t18189/)

hoda 25Jun2009 16:32

how to clear screen?
 
in C programming ; i printf something then i want to make the screen clear and printf another thing .It is for not making my program AMORPHOUS,how can i do that??
thanks.

shabbir 25Jun2009 16:41

Re: how to clear screen?
 
clrscr is one of the easier option.

SaswatPadhi 25Jun2009 20:10

Re: how to clear screen?
 
Quote:

Originally Posted by shabbir (Post 50474)
clrscr is one of the easier option.

That will NOT work for compilers which conform to ANSI C, 'cuz the clrscr() is no more present.

So, you can use system("CLS"); to clear screen, but that's OS specific.
So it's better to write a user-defined func like :

Code: C

void ClearScreen(void)
{
      HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
      CONSOLE_SCREEN_BUFFER_INFO csbi;
      GetConsoleScreenBufferInfo(hStdout,&csbi);
      const COORD startCoords = {0,0};
      DWORD dummy;

      FillConsoleOutputCharacter(hStdout,' ',csbi.dwSize.X * csbi.dwSize.Y,startCoords,&dummy);
      SetConsoleCursorPosition(hStdout, startCoords);
}


Remember to #include <windows.h>.

hoda 28Jun2009 02:11

Re: how to clear screen?
 
Quote:

Originally Posted by SaswatPadhi (Post 50487)
That will NOT work for compilers which conform to ANSI C, 'cuz the clrscr() is no more present.

So, you can use system("CLS"); to clear screen, but that's OS specific.
So it's better to write a user-defined func like :


Code: C
void ClearScreen(void)</p>
<p>{</p>
<p> HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);</p>
<p> CONSOLE_SCREEN_BUFFER_INFO csbi;</p>
<p> GetConsoleScreenBufferInfo(hStdout,&csbi);</p>
<p> const COORD startCoords = {0,0};</p>
<p> DWORD dummy;</p>
<p>&nbsp;</p>
<p> FillConsoleOutputCharacter(hStdout,' ',csbi.dwSize.X * csbi.dwSize.Y,startCoords,&dummy);</p>
<p> SetConsoleCursorPosition(hStdout, startCoords);</p>
<p>}</p>
<p>



Remember to #include <windows.h>.

excuse me but it's not working and has a error like this:
error C2601: 'ClearScreen' : local function definitions are illegal

SaswatPadhi 28Jun2009 07:44

Re: how to clear screen?
 
Quote:

Originally Posted by hoda (Post 50615)
excuse me but it's not working and has a error like this:
error C2601: 'ClearScreen' : local function definitions are illegal

It compiles perfectly fine on my machine.

Error C260 indicates that you are trying to declare a function inside a function.
So, did you directly paste the above code inside your main() ???

You should not do that. You should paste the above function globally and call it from main() or wherever wherever required, this way : ClearScreen();


All times are GMT +5.5. The time now is 18:01.