• Advertisement
Sign in to follow this  

very VERY simple problem

This topic is 4252 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

Okay, I'm starting learning c++ again, but having trouble with a simple issue. I'm using dev c++ as my compiler, and my code currently looks like this -
Quote:
#include <iostream.h> main() { cout << "hello, it's a message. "; return(0); }
The screen pops up and disapears in a milisecond. It does display the message but... do I need to add something to make it stay?

Share this post


Link to post
Share on other sites
Advertisement
Run the program from the command prompt, and then it will stay. The code is simply executing too quickly and since (I assume..) you are just hitting the "run" button, it is closing the window.

Share this post


Link to post
Share on other sites
I've read about this but never encountered this problem yet. The problem is finishing too quickly...

I've read that you can avoid the problem by adding a "cin" so the program waits for an input, before proceeding to the end, kind of like Java applications. Maybe there's a better way though, I haven't done much C++ yet...

Share this post


Link to post
Share on other sites
iostream.h is not a header in C++, where have you seen this? iostream (no .h) is a header though. Also cout is in namespace std, not in the global namespace, so it should be std::cout. You can make it stay by waiting for input, like this:
#include <iostream>

main()
{
std::cout << "hello, it's a message. ";
std::cin.get();
return(0);
}


If your book/tutorial showed you that code, then don't look at it again it's either very old or very bad.

Share this post


Link to post
Share on other sites
This is not your problem, but whatever reference you are learning from is outdated. New style C++ would look like this:
#include <iostream>

int main( )
{
std::cout << "hello, it's a message. ";
return 0;
}
Notice that main is declared to return an integer. Also the include file is iostream and not iostream.h. Also cout must be prefixed by std:: because it is now in the std namespace. Also (though it does not matter), you do not need the parenthesis around the 0 in the return statement.

I'd reccommend getting a more up to date book/tutorial to learn from. Good luck!

Share this post


Link to post
Share on other sites
Add the following right before return 0, to make the console window stay.
while(cin) cin.get();

edit:
didn't see that someone already answered that question, but if you only add cin.get() and have a cin before the cin.get() line. The program will exit by itself like it did before. The while loops makes up for that.

Share this post


Link to post
Share on other sites
I think the solution you are looking for is to run your app from Visual Studio using ctrl-f5 instead of f5(for Visual Studio 6 settings). If your settings are different then look under the debug menu for Execute program insteand of Run program. This will make the console window ask you to press a key to exit...

Share this post


Link to post
Share on other sites
Quote:
Original post by BuffaloJ
I think the solution you are looking for is to run your app from Visual Studio using ctrl-f5 instead of f5(for Visual Studio 6 settings). If your settings are different then look under the debug menu for Execute program insteand of Run program. This will make the console window ask you to press a key to exit...


Except he's using Dev-C++

Share this post


Link to post
Share on other sites
Note that cin.get() will NOT block if there is something still left in the buffer. Use this instead:

std::cin.ignore(std::cin.rdbuf()->in_avail() + 1);

Share this post


Link to post
Share on other sites
Quote:
Original post by pascalosti
u can also use

system("pause");


Only on windows and only if you like starting a brand new shell process just to pause your program and only if you like allowing other people to add there own pause.exe into your program folder and have your program call it (and any virus-like code it may contain ) :/


The best way is to run from a command prompt, and using something like cin.get() as a temporary debugging help.

Share this post


Link to post
Share on other sites
Quote:
Original post by bschneid
Run the program from the command prompt, and then it will stay. The code is simply executing too quickly and since (I assume..) you are just hitting the "run" button, it is closing the window.

This is the only way that solves the "problem" and is correct, in a sense that it doesn't alter the actual program behavior.

All those "prompt for input"-solutions are hacks, nothing more, nothing less. You may use them but please, don't get used to them [smile].

Just my 0.02€

Share this post


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

  • Advertisement