Hi, I wrote a simple program in Visual Studio 2012 to add 2 and 3. The program will not run without "conio.h" and _getch();, the exe window disppears as soon as the program is compiled. Can someone explain to me why?
#include <iostream>
#include <cmath>
#include "conio.h"
usingnamespace std;
int main ()
{
int x = 1;
int y = 2;
int z = x + y;
cout << z;
_getch();
return 0;
}
#include <iostream>
usingnamespace std;
int main ()
{
int x = 1;
int y = 2;
int z = x + y;
cout << z;
return 0;
}
and run it by pressing Ctrl + F5.
As for your question then the windows application that is MS IDE runs your code creating a console. As soon as your code finished executiong the IDE closes the console.
Function _getch checks keyboard buffer and if the user will pressed a key the function returns the control to the main and executing of the program will finished.
Header conio.h contains declaration of that function.
Before you get a ton of usefull replies, know just this - conio.h is not part of C++ as such, and in modern IDE, such as visual studio 2012 it is simply deprecated, and all examples of its use are getting a bit outdated.
Thanks, so if I want the console to remain up I will add conio in my code. Lastly, is there a place visual studio stores the value of "z", which is 3, that I can see it without using the console. Like in MATLAB where you can type in the variable in the command line and see the result after the program ran.