Aayush
Aayush

Reputation: 3098

Alternative function in iostream.h for getch() of conio.h?

I'm trying to hold the screen on my output using the header file <iostream.h>, but I don't know any equivalent function to the getch() & clrscr() functions of <conio.h> in <iostream.h> or any other C++ library. Are there any such functions?

Upvotes: 28

Views: 109549

Answers (11)

Aurora087
Aurora087

Reputation: 96

For clrscr() I generally use

system("cls");       //Windows
system("clear");     // Linux

And for the getch() function I agree with @iKlsR , just use cin.get() 2 times.

{   ...
    std::cin.get();
    std::cin.get();
}

and if you are looking to pause the system for a moment or specific time interval use:

system("sleep 5s"); //for linux

basically the function system("x") runs the command x (like) in terminal. So you can just exploit it by using the different function in place of x. (be specific to use the commands which are related to your distro or OS)

Upvotes: 2

ManuelAtWork
ManuelAtWork

Reputation: 2458

The platform-specific function getch() from conio.h has two special features:

  • No echoing of characters.
  • Unbuffered reading of characters.

The echoing is done by the terminal outside of the C/C++ environment. It can only be controlled by manipulating the terminal. Also, it is nearly impossible to get unbuffered I/O with the iostream.h header.

Therefore it is not possible to get anywhere near getch() using iostream.h alone.

(There are plenty of getch()implementations around, e.g. using termios.h to disable echoing.)

Upvotes: 1

Stephen Veiss
Stephen Veiss

Reputation: 1384

The conio.h functions are compiler extensions to the language, not part of C or C++. There isn't a direct replacement in standard C++.

For getch(), int ch = std::cin.get(); is probably the closest equivalent -- but bear in mind that this will read from buffered standard input, whereas I think the conio.h getch does an unbuffered read.

Any implementation of clrscr() is going to be very platform-dependent -- not all screens or terminals have a notion of clearing, and those that do have wildly differing ways to access that functionality.

If you need to treat the terminal as something other than a set of character streams, your best bet is probably to look for a library which hides the details of the underlying terminal, screen or console from you. If you're on a UNIXish system, look at the curses or ncurses library; I don't know of any suggestions for other OSes.

Upvotes: 31

Mason Watmough
Mason Watmough

Reputation: 495

I understand that this is an old question but I am going to answer nonetheless because people may be looking for an answer to a similar question.

conio.h is an (ancient) Windows and MS-DOS/PC-DOS C library that was, and still is used for very basic, bare-metal keyboard input and handling in a Windows/DOS environment.

Both getch() and clrscr() are non-standard additions by this header, and should be avoided when possible for the standard C functions. getch() can usually be replaced with scanf(), fread(), in C and std::cin and std::cin.get in C++. As for clrscr(), the closest you can get is:

for(int i = 0; i < 100; i++)
{
    printf("\n");
}

OR:

There is also ncurses.h on *nix environments. Here's a link to some info about that.

Upvotes: 1

CLearner
CLearner

Reputation: 542

This is what I usually use:

#include<iostream>
...
std::getchar();

Upvotes: 4

iKlsR
iKlsR

Reputation: 2672

Late answer, you can use std::cin.get(), this should work with most compilers. If that doesn't work, try adding another.

int main () {

    // ...

    std::cin.get();
    std::cin.get();
    return 0x00;
}

Using system("PAUSE") is only available on Windows and is a bad programming habit. The reason for this is it literally pauses or freezes your program as opposed to just waiting for an input. ie. a keypress to exit.

Upvotes: 2

Balakrishnan
Balakrishnan

Reputation: 2441

Just use these two functions:

fflush(stdin);
getchar();

Visual studio and Dev C++ include this in its iostream header so no need to include extra header file.

Upvotes: 3

rockstar
rockstar

Reputation: 1

You can use system("pause"), which produces the "press any key to continue" message. But it works in the windows environment only. I think all the "system" commands are dos commands. Correct me if I am wrong

Upvotes: -6

anonymous
anonymous

Reputation: 81

just use cin.get();

Upvotes: 8

bmeric
bmeric

Reputation: 1132

if you work on windows you can use system("pause"), this will give you "press any key to continue" message.

Upvotes: -23

KJ Saxena
KJ Saxena

Reputation: 21838

getch() and clrscr() will work with C++. Include conio.h

However, if you CANNOT (for some reason) include conio.h,

how about cin>>dummy_var with a display message asking the user to press enter?

Upvotes: 10

Related Questions