Public Group

Press enter to exit

This topic is 5092 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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.

Share on other sites
Try using the getch() function which is in conio.h

Old but works real good ;)

void main(){  getch();}

Share on other sites
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.

Share on other sites
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;}

Share on other sites
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.

Share on other sites
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);

Share on other sites
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

Share on other sites
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(); }

Share on other sites
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.

Share on other sites
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");}

1. 1
2. 2
3. 3
4. 4
Rutin
13
5. 5

• 26
• 10
• 9
• 9
• 11
• Forum Statistics

• Total Topics
633695
• Total Posts
3013376
×