My first time working with Visual C++ (new to the language, too) – experienced C# …so I have my first console app that I started in Visual Studio.
#include "stdafx.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int i;
cin >> i;
return 0;
}
How come the console window – thus the application – doesn’t close when I press enter? No other input – just enter …
More importantly, how can I make the app exit (don’t want to use exit()) properly if I just hit enter?
std::cin awaits one non-empty string from you and then tries to convert this string to integer.
When you press Enter std::cin only gets an empty string and continues to wait for some valid input. This is by design. std::cin is not meant for emulating other interaction.
To terminate the app on keypress, you have to use OS-specific facilities to read keyboard presses.
This is the kbhit() function from “conio.h” on DOS/console Windows and the termio functions on POSIX systems.
From your source I can conclude that you use the MSVC++ compiler, so try replacing
by
Do not forget to add the
and remember this is a Windows-specific solution.