1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

how to clear screen?

Discussion in 'C' started by hoda, Jun 25, 2009.

  1. hoda

    hoda New Member

    Joined:
    Feb 17, 2009
    Messages:
    17
    Likes Received:
    0
    Trophy Points:
    0
    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.
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,276
    Likes Received:
    364
    Trophy Points:
    83
    clrscr is one of the easier option.
     
  3. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Joined:
    May 5, 2009
    Messages:
    1,343
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
    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:
    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>.
     
  4. hoda

    hoda New Member

    Joined:
    Feb 17, 2009
    Messages:
    17
    Likes Received:
    0
    Trophy Points:
    0
    excuse me but it's not working and has a error like this:
    error C2601: 'ClearScreen' : local function definitions are illegal
     
  5. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Joined:
    May 5, 2009
    Messages:
    1,343
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
    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();
     

Share This Page