I have been using code blocks for c++ i has mingw compiler which is using gcc

it doesn't have conio.h header file

so how do i clear the screen now ................?


thanks

Dani AI

Generated

conio.h is a non‑standard, DOS/Windows-era header and is not provided by gcc/MinGW by default. As pointed out, several approaches exist, and is right to ask whether clearing the screen is actually needed. Practical, portable choices and trade-offs follow.

ANSI escape sequences (portable on Unix-like terminals and many modern Windows consoles):

#include <iostream>

std::cout << "\033[2J\033[H"; // clear screen and move cursor to home

Fast and dependency‑free where the terminal honors ANSI codes. Older Windows cmd.exe does not support this unless virtual‑terminal processing is enabled.

Using the host shell (quick and dirty):

#include <cstdlib>

std::system("cls");   // Windows
std::system("clear"); // POSIX

Works immediately in many simple programs but invokes a new shell, is non‑portable, can be slow, and may be a security risk in code that accepts input.

Windows console API (robust Windows-only solution):

#include <windows.h>

void ClearScreen()
{
    HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
    CONSOLE_SCREEN_BUFFER_INFO csbi;
    GetConsoleScreenBufferInfo(h, &csbi);
    DWORD cells = csbi.dwSize.X * csbi.dwSize.Y;
    DWORD written;
    COORD home = {0, 0};
    FillConsoleOutputCharacter(h, ' ', cells, home, &written);
    FillConsoleOutputAttribute(h, csbi.wAttributes, cells, home, &written);
    SetConsoleCursorPosition(h, home);
}

Guidance: for quick student code, system("cls") is simplest on Code::Blocks + MinGW (Windows). For cross‑platform behavior prefer ANSI sequences (test the target terminal) or use a terminal library (ncurses / PDCurses) for full control. For production Windows apps the Console API is the most reliable. If the goal is to update content rather than wipe everything, moving the cursor and overwriting lines is usually a cleaner solution than clearing the whole screen.

Recommended Answers

All 2 Replies

Moschop's link gives a good comparison of almost all the ways to clear the screen...:)

But @op: why do you need to clear the screen at all? If you are writing console apps you should not be concerned about deleting what's in the console already...
Those fancy deleting stuffs and animating-your-outputs, fall in the domain of GUI programming...:)

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.