title: Why does the console stays open although the program is finished?
body: <p>I'm currently working on a web crawler with the ability to abort at any time when the 'ESC'-button is pressed.<br>
  To check whether 'ESC' is pressed, I'm using a thread which sets a flag to signal the 'main'-function to terminate itself. Although this is working fine, the console window doesn't close after the program is finished. The only circumstance under which the console window is closing, is when 'ESC' is pressed immediatly after the thread starts (so before the while loop is starting; see comment in the code).<br></p>
  
  <pre><code>#include &lt;curl/curl.h&gt;
  #include &lt;ctime&gt;
  #include &lt;fstream&gt;
  #include &lt;iostream&gt;
  #include &lt;regex&gt;
  #include &lt;queue&gt;
  #include &lt;stdio.h&gt;
  #include &lt;string&gt;
  #include &lt;thread&gt;
  #include &lt;vector&gt;
  #include &lt;windows.h&gt;
  
  boolean exitCrawler{ false };
  char URL[512];
  CURL *curl;
  int i{ 0 }, start{ 0 }, stop{ 0 }, urlCount{ 0 };
  std::ofstream write;
  std::deque &lt;const char*&gt; urls;
  std::regex rgx("(?:http)[:](?://)[A-Za-z0-9]+.[A-Za-z0-9]+.[a-z]+/");
  std::smatch sm;
  std::string answer, content;
  std::vector &lt;std::string&gt; detectedURLs;
  unsigned short keyState;
  
  auto analyzeContent() -&gt; void;
  auto crawlSite() -&gt; int;
  auto indexSite() -&gt; void;
  auto quitCrawler() -&gt; void;
  auto writeContent(char*, size_t, size_t)-&gt;size_t;
  
  auto main() -&gt; void {
      SetConsoleTitle(L"WebCrawler");
      write.open("C:\\Users\\Daniel\\Desktop\\urls.txt", std::ios::app);
      std::cout &lt;&lt; "Welcome to the WebCrawler!" &lt;&lt; std::endl &lt;&lt; "URL (http://www.*.*/): ";
      std::cin &gt;&gt; URL;
      std::cout &lt;&lt; "Starting to crawl the surface web. Press \"ESC\" at any time to cancel the crawler." &lt;&lt; std::endl;
      urls.push_back(URL);
      std::thread qC(quitCrawler);
      qC.detach();
      //Press 'ESC' here to get the console window to close itself after the program finishes. If you press 'ESC' after the while loop starts, the console window will stay open.
      while (true) {
          std::string s(urls.front(), strlen(urls.front()));
          if (s.find("http://") == -1) {
              urls.pop_front();
              content = "";
              continue;
          }
          if (crawlSite() == 1) {
              urls.pop_front();
              content = "";
              continue;
          }
          analyzeContent();
          std::cout &lt;&lt; "\rCrawled " &lt;&lt; urlCount &lt;&lt; " sites of the surface web.";
          urls.pop_front();
          if ((urls.size() == 0) || exitCrawler) {
              break;
          }
      }
      std::cout &lt;&lt; std::endl &lt;&lt; "Crawler terminating..." &lt;&lt; std::endl;
      write.close();
  }
  
  auto analyzeContent() -&gt; void {
      memset(&amp;sm, 0, sizeof(sm));
      i = 0;
      std::string::const_iterator beginningIterator = content.cbegin();
      while (std::regex_search(beginningIterator, content.cend(), sm, rgx)) {
          beginningIterator = sm[0].second;
          if ((std::find(detectedURLs.begin(), detectedURLs.end(), sm[0])) == detectedURLs.end()) {
              urls.push_back(sm[0].str().c_str());
              i++;
          }
          detectedURLs.push_back(sm[0]);
      }
  }
  
  auto crawlSite() -&gt; int {
      curl = curl_easy_init();
      curl_easy_setopt(curl, CURLOPT_TIMEOUT, 30);
      if (curl_easy_setopt(curl, CURLOPT_URL, urls.front()) != CURLE_OK) {
          return 1;
      }
      curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &amp;writeContent);
      if (curl_easy_perform(curl) != CURLE_OK) {
          return 1;
      }
      curl_easy_cleanup(curl);
      curl_global_cleanup();
      indexSite();
      return 0;
  }
  
  auto indexSite() -&gt; void {
      urlCount++;
      write &lt;&lt; "&amp;emsp;#" &lt;&lt; urlCount &lt;&lt; "&amp;emsp;&amp;emsp;&amp;emsp;&amp;emsp;&amp;emsp;[" &lt;&lt; urls.front() &lt;&lt; "]/[...] --- {[...]} --- " &lt;&lt; std::time(0) &lt;&lt; "&lt;br&gt;" &lt;&lt; std::endl;
  }
  
  auto quitCrawler() -&gt; void {
      keyState = GetAsyncKeyState(VK_ESCAPE);
      while (true) {
          if (keyState != 0) {
              exitCrawler = true;
              break;
          }
          keyState = GetAsyncKeyState(VK_ESCAPE);
      }
  }
  
  auto writeContent(char* buffer, size_t size, size_t nmemb) -&gt; size_t {
      content.append(buffer);
      return size * nmemb;
  }
  </code></pre>
  
  <p>To be honest, I do not have any clue why the program is staying open after the code is finished.<br><strong>Does anybody of you have any idea?</strong></p>