Sign in to follow this  
Joshuad

Simple question (c++)

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
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
Quote:
Original post by Joshuad
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.


I'm not sure I can provide clean and proper understanding for Sneftel's original quote:
Quote:

learn exactly how the << operator is implemented?


Since it can be interpreted a few ways.

Most basically though, the << and >> operators are the C bitshift operators. C++ allows operator overloading. Simply put, it allows programmers to redefine the behavior of operators like the bitshift operators for classes (like the stream classes that define cout/cin) for which the operation is meaningless.

There's then a lot of minutiae regarding how C++ deals with binary operators, how overloading is done, the semantics behind the chaining trick to make cout << "foo" << a << b; work... But if you've not covered the arrow (->), then that's perhaps stuff left until later.

Share this post


Link to post
Share on other sites
Well, I haven't learned very much in terms of pointers, which is probably why -> wasn't covered. I have a bad habit of starting books, getting to points where it no longer makes sense, and then jumping to another book (still keeping the beginners setting) hoping that it will cover the same concept in a cleaner, more organized way in hopes that I will understand it. Therefor, it is taking me longer than I had hoped, but I feel that I am getting a better understanding of the language itself. I've only been learning C++ for a little under 2 weeks if that makes you have a larger understanding. I'm not sure if my progress is good, or if it is incredibly horrible, heh.

Share this post


Link to post
Share on other sites
Quote:
Original post by smitty1276
Debug->"Run without Debugging". If you don't have this, you should upgrade your IDE to VS Express 2008. :-)

Yeah this is the method I talked my college C++ professor into using so I wouldn't have to put system("PAUSE"); or some other kludge in all my C++ programs for that class [lol]

Share this post


Link to post
Share on other sites
Quote:
Original post by daviangel
Yeah this is the method I talked my college C++ professor into using so I wouldn't have to put system("PAUSE"); or some other kludge in all my C++ programs for that class [lol]
Thank your lucky stars they use anything as modern as Visual Studio - half of mine still think ASNI C is pretty neat (my apologies for the thoroughly transparent Douglas Adams reference).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this