I just started learning C++, i have an intermediate background with Java. Following Tony Gaddis book starting out with c++ i was doing my first program in Visual Studio since i am still a student i got the software for free. and the first example in the book goes like this:
1 2 3 4 5 6 7 8
#include <iostream>
usingnamespace std;
int main()
{
cout << "programming is fun"return 0;
}
but when i try to run it the console exits giving me this in the output (The program '[2468] HelloWorld.exe' has exited with code 0 (0x0).) After looking at a youtube video i was wondering why i have to add cin.get(); for the console to stay open?
also the recommended software for this was MinGW but didn't feel like installing it since i already had the visual studio.
Because, your program code has no instruction to wait. Its instruction is to print "programming is fun" and then exit with code 0. If you add cin.get() it'll wait for the user to type in something and press ENTER. Therefore it doesn't close as it waits for input. Alternatively you can use system("pause"); but its not recommended.