window closing

I am using microsoft visual c++ and when I compile it, the window closes and I cant tell if it worked or not. Can someone tell me how to stop it from doing that? This is my code:

#include <iostream>
using namespace std;
int a = 5;
int b = 2;


int main ()
{
cout << a - b;
return 0;
}
before return 0; put
std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
^
|

bleh.

1
2
3
cout << a - b;
cin.get(); // Wait for return/enter to be pressed
cin.ignore(); // Use this after getting input from the console, it deletes the last character (generated from the enter key). It's completely optional when simply waiting for a keypress. 
Instead of return 0; try one of these:

system("pause"); //This might only work on Windows OS's, I've heard that from some people but idk

or this:

return main(); //This will return to the main function.

It happens with me too, Idk if it is the compiler, some compiler settings or what but one of those should work for you.
You should always return 0.
Woah woah WOAH!!!!

return main();

Don't EVER do this!!! EVER! Infinite recursion for the loss...

And anyway, std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' ); >
1
2
cin.get();
cin.ignore();
Recursion is one of the many evil features of C++: Provides awesome functionality, but it's easy to mess up.

Don't use system() either. That's a function that's also evil. Use firedraco's solution.

Or read the stickied thread at the top of this forum.

-Albatross
There's a reason why this is the second stickied thread:

http://cplusplus.com/forum/beginner/1988/

Let's NOT rehash this again. For MS VS: Just use cntl-F5 (Start without debugging).
Wow, wow, wow!!!!!!!!!!!!

I feel like in the 'inquisition' here; It seems that ppl here like to call evil to everything...

First off: Recursion is not a feature of C++!!!!
Recursion, in mathematics and computer science, is a method of defining functions in which the function being defined is applied within its own definition; specifically it is defining an infinite statement using finite components. The term is also used more generally to describe a process of repeating objects in a self-similar way. For instance, when the surfaces of two mirrors are exactly parallel with each other the nested images that occur are a form of infinite recursion.

- The thing with system("pause"); for one thing is that it's not part of your programme and it's a "feature" of the operating system... using that is not a good idea from your programme.... Those "feature" to call it a name would have been created private in modern days, so no everyone mess with it!!!

If u wanna keep the window open, try this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14

//: Usage: Keep_Window_Open keep; in main funtion, so when main terminates "without any exceptions" it'll assure u not to close the console window.... 

class Keep_Window_Open
{
   public:
      ~ Keep_Window_Open()
        {
             char c(0);
             std::cout << "Press any key to EXIT!!!" << std::endl;
             while( std::cin >> c && c != '\n') continue;
        }

};



Return main() is just pointless... see main as the driver of your programme; It's what executes your code and it's called by the operating system and from within it it returns control to it....

"If I may be so brash, it has been my humble experience that there are two things traditionally taught in universities as a part of a computer science curriculum which many people just never really fully comprehend: pointers and recursion". Joel Spolsky; "The Perils of JavaSchools.



Last edited on
You should never make the user hit a key to terminate the program.
Instead you should use a proper IDE that keeps the window open after the program has terminated. Surely VC++ must provide such a feature.
Console programs are generally run from well, the console and having to hit a key everytime to return to the prompt will make the users curse your name a hundred times over. Not to mention that it will make it impossible to use your program to do any automated tasks.
Again!!

You should never make the user hit a key to terminate the program.
This quote should be revised, consider it!

I'm pointing a solution for a given problem: the windows close before I see the output... That maybe in debugging but u sure must retire all debugging elements from ur programme for release; in this case if they're not gonna interact with a human user... But if it does then it's a good idea to inform the user how to terminate the program if designed in that way.

One of the reason to make console programmes is for testing them right away.

Not to mention that it will make it impossible to use your program to do any automated tasks.
I dont see the point here....

If you are worried some users might not see any output due to trying to run it in a separate console, see
http://support.microsoft.com/kb/99115
for a solution.

I dont see the point here....

A console program generally performs a certain task. Now scripts that run a number of programs to accomplish a greater task would not be possible if these programs always kept waiting for user input before terminating.
I said it in such cases that code should be removed for release...

Still i don't see the point of your posts, sorry...
Topic archived. No new replies allowed.