Press enter to exit

Started by
8 comments, last by Jingo 19 years, 3 months ago
I'm new to C++ and I have the book Beginning C++ game programming by Michael Dawson. He tells of the problem that C++ works so fast that on any given program it will pop up and disapear really fast depending on whats going on. He says to put: cout << "Press the enter key to exit"; cin.ignore(std::cin.rdbuf()->in_avail() + 1); This code before the end of all the programs to run it. This only works for some of the programs. If you already have to push enter then it thinks you want to exit already. I solved the problem of making whatever it is display for long enough by changing 1 to 100 so you have to push enter 100 times before it exits. I need to know how to fix this if I want it to work well in programs how I want it to work. Mostly, how would I change it to another key like escape or something? Then I would never have to worry about it. Sorry if this has been posted before.
Advertisement
Try using the getch() function which is in conio.h

Old but works real good ;)

void main(){  getch();}
______________________________________________________________________________________________________
[AirBash.com]
A little more explanation? Remember I'm a noob... Whats conio.h and where do I put getch. I tried putting conio.h as an include file and getch where press enter used to be but I got compile errors.
It seems to me that the book you choose isn’t a very good book for the beginners. I suggest you find a better book to start off. But meanwhile here is a code you can try to halt your program until a user hits a key:

#include <iostream>#include <conio.h> // includes the command getch()using namespace std;int main(){	cout << "Hello World!";	getch(); // gets a single char from user	return 0;}
So is getch like Press any key to exit? It seems to be working but what exactly does it do. The book seems to work all except for that one thing, I've learned quite a bit from it and I'm only on chapter 3.
I'm don't know much about your book, but it sounds to me it’s meant for people familiar with the C++ basics. Aside from that, the getch() command gets a single char from the user in the form of an int. Its prototype is as follows:

int getch(void);
Yea something like that.

getch() returns the anscii value of the key pressed.You will learn what are anscii values soon.Ever character you see has an ancii value for it.

Anscii value for A = 65
______________________________________________________________________________________________________
[AirBash.com]
Ug ug ug ug ug. getch() is non-standard. Don't get into that habit.

Let's take a look at the line from the book and understand how it works:

cin.ignore(cin.rdbuf()->in_avail() + 1);

The 'ignore' method of cin will try to read the specified number of bytes, waiting until it is possible to do so. So we try to read the current number of bytes (the "in_available" number in the "read buffer" of cin), plus one. Clearly this is more than what's available (by one), so it will wait for one more byte to appear.

However, it's a little more complicated than that. Since the standard input is line-buffered, that extra byte won't be seen until enter is pressed.

I don't see why you would have problems with previous enter key presses registering, though. They should be eaten up by the .ignore() call. And I don't know what you mean by "if you already have to push enter" - why would you, at that point?

Anyway, there is another natural way to handle the end of the program: since cin is a stream object, you can treat it just like a file, and wait for it to register "end-of-file". Then the user would input control-D (Unix) or control-Z (Windows) and a return in order to exit. I think this is right:

cout << "Press Ctrl-Z and return to exit" << endl;while(!cin.eof()) { cin.read(); }
Can't he just put cin.ignore() followed by cin.get()
for example if he had to cin something...

#include <iostream>using namespace std;int main(){int someVar;cin >> someVar; // Get the value from the usercin.ignore();   // They have to hit enter to send it in, so ignore enter. If you                // didn't then it would take "enter" as the next input...cout << "\n\n" << someVar; // Do whatever your program is supposed to docin.get();      // ...Which would make this thingy fire off right away. Since we                // ignore the "enter" that it takes to give a value to someVar this                // requires a second "enter" that you can make at your leisure.return 0;}


Of course I'm a complete noob as well and I don't know if that is the best way or the right way or the ideal way but it works.
skulldrudgery--A tricky bit of toil
All the code from Beginning C++ game programming by Michael Dawson does is clear the std::cin buffer, it does not guarentee that it will freeze the program to wait for input. There could be another buffer underneath the cin streambuf, which would mean the program would not freeze. Basically there is no portable way to freeze the program like this, you will need to use a non-portable method. I suggest:

#include<cstdlib>int main(){std::system("PAUSE");}

This topic is closed to new replies.

Advertisement