2012-03-07 69 views
3

我想在用戶在C++中輸入一些數字後清除屏幕。我在控制檯應用程序模式下編程。在C++中清除屏幕命令

那該怎麼辦呢?我的操作系統是win7,我的IDE是CodeBlocks,編譯器是MingW ...

+1

怎麼樣clrscr();方法???? – 2012-03-07 08:15:02

+1

包括您的操作系統和IDE的問題,因爲答案取決於 – 2012-03-07 08:21:51

+0

它現在被添加。 – Nofuzy 2012-03-07 08:37:51

回答

5

這取決於你的操作系統, 如果你使用Linux:

system("clear"); 

如果您使用Windows:

system("cls"); 

但這讓你的應用程序酒糟便攜,是更好要做

cout << string(50, '\n'); 

這一行將打印行,看起來像終端被「清除」。

有關問題的好文章: http://www.cplusplus.com/articles/4z18T05o/

+3

50?那是可怕的算法! – 2012-03-07 08:27:11

3

您可以使用conio.h中定義的clrscr()。但爲什麼不問你之前谷歌?

ways to clear screen the out put screen

+0

對於使用Clrscr我應該使用哪個頭文件? – Nofuzy 2012-03-07 08:18:01

+1

你在用什麼IDE? – 2012-03-07 08:19:13

+0

我問谷歌之前,來到這裏。谷歌不會創建內容,它可以幫助您通過編制索引來查找內容 – 2012-03-15 07:07:19

2

你可以嘗試系統方法,例如系統( 「CLS」);

0

一種方法是輸出'\ f'(對應於ASCII換頁字符,代碼12,行打印機用來彈出頁面,並被一些常見終端和仿真器識別爲清屏)。

這不適用於Windows。

#ifdef _WIN32 
/* windows hack */ 
#else 
std::cout << '\f' std::flush; 
#endif 
1

在您的編譯器中鏈接conio.h。我忘了怎麼做。如果您將反覆使用清晰屏幕放置此功能。

enter code here 
void clrscr() 
{ 
    system("cls"); 
} 
1

這就是微軟不得不說的清除控制檯:

#include <windows.h> 

void cls(HANDLE hConsole) 
{ 
    COORD coordScreen = { 0, 0 }; // home for the cursor 
    DWORD cCharsWritten; 
    CONSOLE_SCREEN_BUFFER_INFO csbi; 
    DWORD dwConSize; 

    // Get the number of character cells in the current buffer. 

    if(!GetConsoleScreenBufferInfo(hConsole, &csbi)) 
    { 
     return; 
    } 

    dwConSize = csbi.dwSize.X * csbi.dwSize.Y; 

    // Fill the entire screen with blanks. 

    if(!FillConsoleOutputCharacter(hConsole,  // Handle to console screen buffer 
            (TCHAR) ' ',  // Character to write to the buffer 
            dwConSize,  // Number of cells to write 
            coordScreen,  // Coordinates of first cell 
            &cCharsWritten))// Receive number of characters written 
    { 
     return; 
    } 

    // Get the current text attribute. 

    if(!GetConsoleScreenBufferInfo(hConsole, &csbi)) 
    { 
     return; 
    } 

    // Set the buffer's attributes accordingly. 

    if(!FillConsoleOutputAttribute(hConsole,   // Handle to console screen buffer 
            csbi.wAttributes, // Character attributes to use 
            dwConSize,  // Number of cells to set attribute 
            coordScreen,  // Coordinates of first cell 
            &cCharsWritten)) // Receive number of characters written 
    { 
     return; 
    } 

    // Put the cursor at its home coordinates. 

    SetConsoleCursorPosition(hConsole, coordScreen); 
} 

int main() 
{ 
    HANDLE hStdout; 

    hStdout = GetStdHandle(STD_OUTPUT_HANDLE); 

    cls(hStdout); 
    return 0; 
}