6

I have some old program source code that I wrote for Turbo C compiler. I made changes to them and want to recompile them in newer compilers for Linux and Windows. So please tell me what are the best alternative functions to

getch(), delay() / sleep (), clrscr(), gotoxy()

for C and C++.

user438383
  • 5,716
  • 8
  • 28
  • 43

4 Answers4

15

Take a look at the ncurses library, for Unix compatible systems.

Alnitak
  • 334,560
  • 70
  • 407
  • 495
9

For Windows systems:

Best is to compile the program as a console application for windows.

You can directly use the Windows API for console windows and console output. Take a look at the MSDN: Windows Console functions

Here are possible replacements for the given functions:

  • getch(): use _getch() from conio.h
  • delay()/sleep(): use the windows Sleep() function
  • clrscr(): write your own clrscr() function by using FillConsoleOutputCharacter() and FillConsoleOutputAttribute()
  • gotoxy(): use SetConsoleCursorPosition()
Dolphin
  • 299
  • 1
  • 2
2

On Unix-like systems you can use VT100 escape codes for replacing clrscr() and gotoxy(). clrscr():

std::cout << "\033[2J" << std::flush;

See http://www.termsys.demon.co.uk/vtansi.htm for gotoxy() and more.

Janus Troelsen
  • 20,267
  • 14
  • 135
  • 196
-1
  1. To replace clrscr(), you can use system("cls"); (available with #include<stdlib.h>).
  2. To replace getch(), you can use system("pause"); (also available with #include<stdlib.h>).
  • Hello, welcome to SO ! Please also add substitutes candidates for gotoxy() and delay(). –  Jan 07 '22 at 12:44
  • `system("pause")` is a *very* limited substitution for `getch`. And OP wants Linux compatibility too, and both are windows-only... – HolyBlackCat Jan 09 '22 at 11:49