Waiting for User Input to exit, in AND out of IDE.

Bear with me. Skip to "NOW THE ANOMALY" if my story doesn't help.

This doesn't seem to be a code problem so much as a problem with the nuances of how IDEs work.

I'm a beginner with some barely-mentionable (self-taught) java experience, actually let's put that in quotes: "experience."

I'm diddling around in a book by Stroustrup himself: Programming principles and Practice using C++ because I don't just wanna learn, I wanna learn the right way.

With java I kept learning that I was being taught poor programming practice by the well-meaning youtubers whose advice I was consulting, so with C++ I thought I'd find myself a pdf and go straight to the man himself for my knowledge this time.

So I'm on chapter 4, and I just finished making my first program which uses a vector. Yay!

It builds, compiles, runs perfectly OK in code::blocks.

Now, Stroussy's book is set up in such a way that, so far, I prefer modifying my "hello_world" project over and over again as he shows me new examples, instead of making a new project each time.

However, I've decided that I want to copy-paste my compiled .exe file found in the bin folder of my hello_world project into a separate folder outside of the IDE project, each time I complete a new example. My idea is that I'll just rename them to whatever the example in the book is, and I'll be able to go back and see the little programs I've made in action without having necessarily to keep all the project folders and source code as well.

NOW THE ANOMALY:

When I run these .exe files I've made outside of the IDE, they just perform their functions and then immediately close after they're finished. No "process returned 0: Press any key to continue" or any such prompt. I've reckoned that the IDE generates this code for me and that once I'm outside of that environment, the extra code to wait for the user input is deleted, so the console closes immediately.

I've searched for the solution to this, and some have (sadly) suggested using some system pause function that everyone else decried as a horrible idea on account of it being OS-specific and nooby.

Others have recommended this:

1
2
3
4
  std::cout << "Press ENTER to continue...";
  std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );

  return 0;


Sadly, this doesn't work and I get an

error: expected primary-expression before '>' token|


Also, I'm still not sure

A) if this would even be a proper fix for every situation in which I'm dealing with a simple console program

B) if (OR WHEN) I really have to do the std:: thing

(background info: Stroustrup has me #include some "std_lib_facilities.h" file that I got off his website so that using std:: seems unnecessary half the time, and he tells me it will continue to be until later in the book.)

C) I don't like that I'm copy-pasting that huge parameter on the cin.ignore without understanding it.

Hope I've made my problem clear enough. Any help is enormously appreciated. Sorry for the huge tl;dr textwall.
A) Off the top of my head, I can see no situation where this would not work. Perhaps if the user has hit enter many times while the program is processing and then it gets around to reading input and there are already newlines there, but in that case I would actually expect the result anyway.

B) I'm not familiar with what's in Stroustrup's header, but if you haven't needed to use std:: to refer an identifier before (cout, cin, etc.) then you won't need to use it here either. I imagine eventually your tutorial will discuss namespaces and such, at which point you'll understand more about its use.

C) The idea of the ignore line is to go through all input and discard characters until a newline character is found. The parameter is simply a way to get the maximum number of characters that are allowed to be in cin at any time, allowing us to guarantee we will only finish the ignore() once we've hit a newline.
Hi NullInfinity,

Thanks for your excellent post.

Sadly, this doesn't work and I get an

error: expected primary-expression before '>' token|


Did you include the limits header file?

B) Use the std:: whenever referencing something in the std namespace. It is a good idea not to have using namespace std; use std:: instead. Also it is a good idea to put your own stuff in it's own namespace.

C) Have a look at the reference material at the top left of this page, also google C++ std::numeric_limits<std::streamsize>::max()

http://www.cplusplus.com/reference/istream/istream/ignore/


std::numeric_limits has the size or limits of various types. Have a look at this:

http://en.cppreference.com/w/cpp/types/numeric_limits/max


Hope all goes well.
Yes, the problem is that you are missing a header somewhere. The message you are getting means, in the compiler's obtuse way, that it doesn't know what a std::streamsize thing is.

Including <iostream> should have done it for you...
Thank you, all three who replied. Your information has been helpful.

I am really grateful for how swift and friendly this forum has been, so late at night (at least where I live).

The world never sleeps, now.

Alas, I still get the token error even when including "limits.h"

(or should it be <climits>?)

I also tried including <iostream>.

Is it possible that I might be missing a necessary header file, or that my IDE is looking in the wrong place for them?

I ended up uninstalling/reinstalling code::blocks many times a few weeks back when I was getting way ahead of myself and trying out different GUIs like SDL (which I ended up compiling successfully with) and SFML (which I didn't).

In the example code on

http://en.cppreference.com/w/cpp/types/numeric_limits/max


it has :

#include <limits>

Using quotes around an include file usually means it will look in the current directory first for the file - you normally do this for your own header files not system ones.

Ah, thank you IdeasMan.

I head read that before but forgotten it.

EDIT: And now it compiles!
Last edited on
Topic archived. No new replies allowed.