Jump to content
  • Advertisement
Sign in to follow this  
eek

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 user
cin.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 do

cin.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 this post


Link to post
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");
}


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!