title: for(;;) vs do..while() for main program loop
body: <p>I'm using a do..while loop to allow my program to exit if a condition is correct, but I've seen in examples when I was in school where an exception was thrown in order to quit. Those examples used a regular infinite for loop. I'm thinking that I could use an if statement to break from the for loop rather than throwing, but I'm not sure. Code below:</p>
  
  <p>// main.cpp</p>
  
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;cstdlib&gt;
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  
  #include "commands.h"
  
  using namespace std;
  
  int main(int argc, char *argv[]) {
      bool exit = false;
  
      try {
          do {
              try {
                  // Read a line, break at EOF, and echo print the prompt
                  // if one is needed.
                  string line;
                  getline(cin, line);
                  if (cin.eof()) {
                      cout &lt;&lt; endl;
                      break;
                  }
  
                  command_fn fn = find_command_fn(line);
                  fn();
              }
              catch (command_error&amp; error) {
                  // If there is a problem discovered in any function, an
                  // exn is thrown and printed here.
                  cout &lt;&lt; error.what() &lt;&lt; endl;
              }
          } while (!exit);
      }
      catch (exception&amp; e) {
  
      }
  
      return 0;
  }
  </code></pre>
  
  <p>// commands.h</p>
  
  <pre class="lang-cpp prettyprint-override"><code>#ifndef __COMMANDS_H__
  #define __COMMANDS_H__
  
  #include &lt;unordered_map&gt;
  using namespace std;
  
  
  // A couple of convenient usings to avoid verbosity.
  
  using command_fn = void (*)();
  using command_hash = unordered_map&lt;string,command_fn&gt;;
  
  // command_error -
  //    Extend runtime_error for throwing exceptions related to this 
  //    program.
  
  class command_error: public runtime_error {
     public: 
        explicit command_error (const string&amp; what);
  };
  
  // execution functions -
  
  void fn_connect     ();
  void fn_disconnect  ();
  void fn_test        ();
  void fn_exit        ();
  
  command_fn find_command_fn (const string&amp; command);
  
  
  #endif
  </code></pre>
  
  <p>// commands.cpp</p>
  
  <pre class="lang-cpp prettyprint-override"><code>
  #include "commands.h"
  
  command_hash cmd_hash {
     {"c"  , fn_connect   },
     {"d"  , fn_disconnect},
     {"t"  , fn_test      },
     {"e"  , fn_exit      },
  };
  
  command_fn find_command_fn (const string&amp; cmd) {
     // Note: value_type is pair&lt;const key_type, mapped_type&gt;
     // So: iterator-&gt;first is key_type (string)
     // So: iterator-&gt;second is mapped_type (command_fn)
     const auto result = cmd_hash.find (cmd);
     if (result == cmd_hash.end()) {
        throw command_error (cmd + ": no such function");
     }
     return result-&gt;second;
  }
  
  command_error::command_error (const string&amp; what):
              runtime_error (what) {
  }
  
  void fn_connect (){
  }
  
  void fn_disconnect (){
  }
  
  void fn_test (){
  }
  
  void fn_exit (){
  
  }
  </code></pre>
  
  <p>Edit: Have included more source. I have lots of blanks because I'm rewriting a program that sends data via UDP. I'm just looking for some recommendations as to how to quit the program safely.</p>