1

I have just started learning c++ and I want the programme to stay open after the result is displayed. So I used getch(); and c++ is showing that it should have a prototype. What does it mean? and how do I resolve this>

Eugene Sh.
  • 17,802
  • 8
  • 40
  • 61
  • Show the exact error. – Ivan Rubinson Jul 05 '16 at 13:15
  • Are you sure it's `c++` and not `c`? – Eugene Sh. Jul 05 '16 at 13:16
  • 2
    The point Eugene was trying to make is that `getch` is not used in C++, it is only used in C (and then only in old-school Windows programming). In C++, you would use `std::cin`. If your book is purporting to teach you C++, but talks about `getch`, then it is not a good book. [Get a better one.](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Cody Gray - on strike Jul 05 '16 at 13:18
  • @ivanRubinson getch() should have a prototype is the exact error –  Jul 05 '16 at 13:18
  • So how do I make the programme stay? @codyGray –  Jul 05 '16 at 13:19
  • OP wants just to _pause_ the screen. Look at my answer – Khalil Khalaf Jul 05 '16 at 13:20
  • In Visual Studio, start the program with Ctrl+F5. It will remain on the screen until you close it. It only closes automatically when you have the debugger attached. If you're not debugging it, use Ctrl+F5. Or, you can include `` and use `std::getchar` (but beware that is not an ideal solution, because if there are already characters buffered, it will just consume them and continue on). – Cody Gray - on strike Jul 05 '16 at 13:21
  • If it is C++, You can just use cin.ignore() :) –  Jul 05 '16 at 13:38

5 Answers5

3

it means one of the followings:

  1. you are programming under DOS and forgot to include conio.h (https://en.wikipedia.org/wiki/Conio.h). Possibly you copied a source from an older textbook, since conio.h is a very old concept. What sources do you use for learning? I'd recommend one from: The Definitive C++ Book Guide and List

  2. You are programming under Linux and you forgot to include curses.h (http://linux.die.net/man/3/getch)

Community
  • 1
  • 1
Ferenc Deak
  • 34,348
  • 17
  • 99
  • 167
  • You forgot Windows. – Jabberwocky Jul 05 '16 at 13:18
  • 1
    @MichaelWalz I don't think any (self respecting) windows compiler ships with conio.h. It's an old Turbo C header by Borland if I remember correctly. – Ferenc Deak Jul 05 '16 at 13:19
  • conio.h is still deliverd with my Visual Studio 2013. – Jabberwocky Jul 05 '16 at 13:58
  • It is delivered with my "Fancy" Visual studio 15 Preview. –  Jul 05 '16 at 14:25
  • Also, It is delivered to some POSIX machines. –  Jul 05 '16 at 16:16
  • The conio header included with modern Windows compilers is very different than the conio header included with ancient Borland DOS-based compilers. They just have the same name, and both deal with console I/O. The main thing they have in common is that they should no longer be used, *certainly* not in C++. – Cody Gray - on strike Jul 06 '16 at 09:44
1

Include:

1) <conio.h> on Windows.

2) <curses.h> on UNIX

Mattia F.
  • 1,720
  • 11
  • 21
1

Looks like you only want to PAUSE your console application on the screen. Use this#include <stdio.h> and try getchar(); instead of getch(); or simply system("pause"); or cin.ignore() will do the job for you.

Also, "Start without Debugging" using Ctrl-F5 will allow you to Press Any Key To Continue at the end of your program. This way it won't close until you press some key and the console will pause on the display screen.

Khalil Khalaf
  • 9,259
  • 11
  • 62
  • 104
0

If you're on Windows then getch is a function from the Windows-only <conio.h> library. You need to include it (#include <conio.h>). It's possible only on Windows.

Also, getch() is deprecated. Use _getch() instead.


If you're on GNU+Linux, then getch is a function from the <curses.h> library. You need to include it (#include <curses.h>).

Ivan Rubinson
  • 3,001
  • 4
  • 19
  • 48
0

The objective as I understand is :

I want programme to stay open after the result is displayed

Why not do it the typical c++ way?

#include<iostream>

int main(void)
{
    int i;
    char ch;
std::cout<<"Enter any character : ";
std::cin.get(ch);  // For testing enter a string at this step say "String"

/* The input to cin is line-buffered, so after reading 'S' to ch,
 * the remaining "tring" is still in the buffer.
 */

std::cout<<"Entered character : "<<ch<<"\n";
while(std::cin.get()!='\n')
;; 
/* cin.get() is an overloaded function in the istream class.
 * If no arguments are passed to 'get()' this function reads single next character
 * In essence, we wait for the cin.get() to clear the buffer that is
 * read all characters including '\n'
 */

std::cout<<"Press any key to continue..\n";
std::cin.get();
/* Since we have already cleared the buffer using the loop
 * 'get()' expects us to enter a character this time
 */

return 0;
}
sjsam
  • 21,411
  • 5
  • 55
  • 102
  • Why not use `cin.ignore()`? –  Jul 05 '16 at 13:57
  • @MatthewRoh : Well, I haven't used that method before..After reading your comments from the other answer, I'm currently reading the manual for that. Will update in a moment. – sjsam Jul 05 '16 at 13:59
  • 1
    @MatthewRoh : My first impression is that using cin.ignore() is a luxury here.. I may finish the job with more easily understood `while()` loop – sjsam Jul 05 '16 at 14:06