Function clrscr in C and C++
clrscr()
is a nonstandard function (neither mentioned in ISO C99
nor in ISO C++-98
) defined in <conio.h>
(which is not standard compliant itself). However some compilers (like Turbo C/C++) support it as an extension.
clrscr() not working, getch() working. Why?
conio.h
is dead!
Some background: conio.h
defines an API that was once created to control the (text!) screen of an IBM PC. It was originally just a wrapper around MS-DOS functions, so you didn't have to write your own assembly creating an int 21h
to call them. The exact API of conio.h
was never standardized and varies from implementation to implementation.
I assume you're using a compiler targeting Windows, these typically still provide some variation of conio.h
. But as you can see, there's no guarantee what's really available and works as you would expect.
Nowadays, you'd even have to ask what is a screen? The content of your console window? But what if your controlling terminal is e.g. a remote shell (telnet, ssh, ...)? And even different console window implementations will differ in features and how you control them. C only knows input and output streams, they will work with any kind of terminal / console because they don't know anything about a screen, just input and output of characters.
For actually controlling the "screen", Windows provides the Console API, you could use that directly, but then your program is "hard-wired" to Windows only. Most other consoles / terminals understand some sort of escape codes, often the ANSI escape codes. Windows starting with Windows 10 has optional support for them as well. But there's a wide variety of terminals understanding different codes (and different subsets of them), so using them directly isn't a good idea either.
Nowadays, the de facto standard for controlling a terminal/console is the Curses API which has its roots in BSD Unix, but implementations exist for a large variety of systems and consoles. Most notably, ncurses is available for many systems, even including Windows, but for Windows, you also have pdcurses. There's even an extended pdcurses for Windows that implements its own console window, so you can use features the native Windows console doesn't have. Of course, you won't need this for just "clearing the screen" and reading some input from the keyboard.
When you use curses
, you have to do all console/terminal input and output using curses
functions (you can't use stdio
functions like printf()
for that). Here's a tiny example program:
#include <curses.h>
// don't include `ncurses.h` here, so this program works with
// different curses implementations
#include <ctype.h> // for `isalnum()`
int main(void)
{
initscr(); // initialize curses, this also "clears" the screen
cbreak(); // among other things, disable buffering
noecho(); // disable "echo" of characters from input
addstr("Hello, press a key!\n"); // output a constant string, like puts/fputs
refresh(); // output might be buffered, this forces copy to "screen"
int c;
do
{
c = getch(); // read a single character from keyboard
} while (!isalnum(c)); // ignore any input that's not alphanumeric
printw("You entered '%c'.\n", c); // formatted output, like printf
addstr("press key to exit.\n");
refresh();
c = getch();
endwin(); // exit curses
}
You can compile it e.g. with gcc like this for using ncurses
:
gcc -std=c11 -Wall -Wextra -pedantic -ocursestest cursestest.c -lncurses
Or with pdcurses
:
gcc -std=c11 -Wall -Wextra -pedantic -ocursestest cursestest.c -lpdcurses
To learn more about curses
, I recommend the NCURSES Programming HOWTO.
Is clrscr(); a function in C++?
It used to be a function in <conio.h>, in old Borland C compilers.
It's not a C++ standard function.
UNDEFINED REFRENCE TO clrscr();
Your classmate is programming under DOS, obviously you don't ... conio.h
comes with Turbo C and DOS ... So, remove the lines
#include<conio.h>
and
clrscr();
and
getch();
to make your program compile ...
... and do not use %.1f
to print an int.
... and main()
must return int
* and do not copy from your classmate ... he seems to be stuck in the stone age*
clrscr(); equivalent in Code::Blocks
The easiest most straightforward way is to just do it through system
function call:
#include <stdlib.h>
int main()
{
system("cls");
}
If you want to do it programmatically MSDN shows how here.
Note that there is no standard function provided by C++ for clearing the console. Some compilers, like borland, provides it as a non-standard function for convenience but it's not portable between different compilers.
clrscr error in main program debug
- Put the function prototype
void sort(int N);
outsidemain()
- You don't have (but you may) execute
clrscr()
beforefflush(stdin)
. In this case contents of your screen (which you want to clear) have nothing to do withstdin
.
You can read more about fflush()
and the motivation to use it, here.
Related Topics
Base Pointer to Array of Derived Objects
Qt3D. Draw Transparent Qspheremesh Over Triangles
Ostream Chaining, Output Order
Can a Single Member of a Class Template Be Partially Specialized
Infinite Loops - Top or Bottom
Using Bts Assembly Instruction with Gcc Compiler
Union for Uint32_T and Uint8_T[4] Undefined Behavior
How Performing Multiple Matrix Multiplications in Cuda
What Is C++ Version of Realloc(), to Allocate the New Buffer and Copy the Contents from the Old One
Can Raw Pointers Be Used Instead of Iterators with Stl Algorithms for Containers with Linear Storage
Stopping the Debugger When a Nan Floating Point Number Is Produced
Execute C++ from String Variable