Jump to content
  • Advertisement
Sign in to follow this  
Joshuad

Simple question (c++)

This topic is 3379 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 wasn't entirely sure if I should put this in For Beginners or not, but right now I'm focusing my learning of C++ into a more specific book, Beginning C++ Game Programming. In the previous books that I've read, I have noticed that all console applications that I had made would close the console window before I even have a chance to see what I've done. Most books that I've read (even the ones from c++'s main site) do NOT go over how to correct this, until this book. Now at first I went online and looked independently on ways to correct this. The solution that I had come up with was this: system("PAUSE"); Simple, straight forward, but now that I am using this book, I'm not sure if it is the 'best' solution to this incredibly simple (yet stupid) problem. The book says that I should go about it as such (which is far more difficult than the simple system pause): std::cout << "Pres the enter key to exit"; std::cin.ignore(std::cin.rdbuf()->in_avail() +1); This of which brings in syntax that I haven't even learned yet! I know what cin is, but I don't know what these sub-functions rdbuf or in_avail are. I have yet to use '->' in a program, and it just seems fishy. Can anyone do me a favor and explain some of this to me? Sorry to sound like a noob, but I'm one of those masochistic idiots who want to understand every little thing that they can really retain it. So the questions are this: Can you explain cin.ignore, cin.rdbuf, ->, in_avail() to me? Is this a more logical solution to system("PAUSE")? The only benefits that I can see is that using cin.ignore could allow more customization to what I want the screen to say or program to do.

Share this post


Link to post
Share on other sites
Advertisement
The only correct way is to start console programs from the console, not by double-clicking on them.

Windows Start Menu/Execute/cmd/OK

cd \path\to\your\project
nameof.exe

Share this post


Link to post
Share on other sites
Quote:
Original post by Joshuad
So the questions are this: Can you explain cin.ignore, cin.rdbuf, ->, in_avail() to me? Is this a more logical solution to system("PAUSE")?
Roughly speaking, it ignores any input that has been buffered, plus one additional character. The inner workings of rdbuf are far beyond where you should be concentrating at this stage of learning, so just use it and ignore the inner workings.

This approach is a little more portable than system("pause"), but in either case, it is a hack to work around Window's users being unfamiliar with the command line. If you were to run the program from the command prompt, the window will not close.

Share this post


Link to post
Share on other sites
Quote:
Original post by Joshuad
Sorry to sound like a noob, but I'm one of those masochistic idiots who want to understand every little thing that they can really retain it.


You'll quickly run out of brain space that way, but it's your call (and a common desire. It's probably a good way to learn that there's only so much that you can be aware of at a time).

Quote:

So the questions are this: Can you explain cin.ignore, cin.rdbuf, ->, in_avail() to me? Is this a more logical solution to system("PAUSE")? The only benefits that I can see is that using cin.ignore could allow more customization to what I want the screen to say or program to do.


cin.ignore is a function that discards characters.
cin.rdbuf is a pointer to the read buffer of that stream.
-> is the C++ arrow. It is a combination pointer dereference and member access. In this case it turns the pointer to the read buffer into the read buffer itself, and then accesses in_avail
in_avail is a function that represents the size of the stuff waiting in the buffer.

So, all together, it discards N characters where N is the number of characters in the buffer (plus 1).

It is a less logical solution to system("PAUSE"); but system("PAUSE"); actually invokes that program. If some nefarious person replaces 'pause' with something that deletes all your files... badness. Plus 'pause' isn't guaranteed to be available on all systems.

In general, if you just run your app from the command line this problem is moot. And running your app without debugging in MSVS also does this for you (last I checked).

Share this post


Link to post
Share on other sites
Well, I knew that if I were to run it from console it would remain open, but I was curious on how to get it to retain the window when it is ran in windows via double clicking the linked .exe.

Thanks everyone who explained everything to me. So from what I gathered is that system("PAUSE"); would be fine while I'm testing things on my own computer, but not with something that I'm sharing because it might not be available on other platforms?

Share this post


Link to post
Share on other sites
Quote:
Original post by Joshuad
Sorry to sound like a noob, but I'm one of those masochistic idiots who want to understand every little thing that they can really retain it.

I assume, then, that you've already gone out of your way to learn exactly how the << operator is implemented?
Quote:
Thanks everyone who explained everything to me. So from what I gathered is that system("PAUSE"); would be fine while I'm testing things on my own computer, but not with something that I'm sharing because it might not be available on other platforms?
It'll produce an error on UNIX machines. But honestly, I wouldn't worry about it. You're unlikely to be working with "put something on the screen and then exit" programs for very long.

Share this post


Link to post
Share on other sites
I don't remember if I had mentioned this, Sneftel, but this is the 3rd C++ book that I have worked through.

I understand quite a bit of the syntax, actually, the only reason I bothered asking this question is because I found a new method to something that I had already deemed as feasible. That being said, it is good that I finally decided to ask a question, because the information that has been provided to me in this thread alone cleared up misconceptions about the system("PAUSE"); statement. From what I've learned, the << operator is used to provide an output, and the >> operator is used to obtain an input, used with the iostream library. Am I correct with this assumption?

Share this post


Link to post
Share on other sites
Quote:
Original post by Joshuad
Am I correct with this assumption?


Based on what you've learned, it's probably a good assumption. [edit: and certainly a good enough basis to go on for a while]

As for being 'correct'...

Share this post


Link to post
Share on other sites
Quote:
Original post by Joshuad
I understand quite a bit of the syntax, actually, the only reason I bothered asking this question is because I found a new method to something that I had already deemed as feasible. That being said, it is good that I finally decided to ask a question, because the information that has been provided to me in this thread alone cleared up misconceptions about the system("PAUSE"); statement. From what I've learned, the << operator is used to provide an output, and the >> operator is used to obtain an input, used with the iostream library. Am I correct with this assumption?
Oh, you're right about the syntax, more or less. But I was referring to understanding how the operator is implemented, not what it does. At some level, for quite some time, certain portions of your code are going to be "magical". That's not ideal, but in an inherently low-level language like C++ it's just something you'll have to deal with.

Share this post


Link to post
Share on other sites
Quote:
Original post by Telastyn
Based on what you've learned, it's probably a good assumption. [edit: and certainly a good enough basis to go on for a while]

As for being 'correct'...


Ok...what is 'correct' then? My purpose in asking questions is to go from my educated assumptions to having a clean and proper understanding. Please enlighten me.

Quote:
Original post by Sneftel
Oh, you're right about the syntax, more or less. But I was referring to understanding how the operator is implemented, not what it does. At some level, for quite some time, certain portions of your code are going to be "magical". That's not ideal, but in an inherently low-level language like C++ it's just something you'll have to deal with.


I suppose I still don't understand what you are asking.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!