title: Clearing a field buffer in ncurses
body: <p>I've built a generic wrapper for the curses form object to use with a large
  simulation project that I'm working on.</p>
  
  <p>Basically, I instantiate an generic_form object, add some fields and their
  descriptions to it, and then give it focus and get user input with a <code>fill_form()</code> routine.</p>
  
  <p>Because each <code>generic_form</code> object can be used more than once, that is, that fill_form() may be called more than once per instantiation, I need to clear the field buffers at the beginning of the fill_form() routine. Currently, I'm using this at the beginning of the routine:</p>
  
  <pre><code>//clear the field buffers in case there's junk in them
      for (std::vector&lt;FIELD*&gt;::iterator clear_iter = fields.begin();
              clear_iter != fields.end(); clear_iter++)
      {
          if (*clear_iter != nullptr)
          {
              set_field_buffer(*clear_iter, 0, " ");
          }
      }
  </code></pre>
  
  <p>However, it throws a floating point exception at the <code>set_field_buffer</code> line on the <em>second</em> call to the fill_form() routine. Moreover, the <code>set_field_buffer</code> line doesn't seem to actually be doing anything, at least not clearing the buffer, because without calling free_field at the end of the routine, and instead in the object destructor, the fields buffers remain the same on each subsequent call.</p>
  
  <p>Here's the entirety of the (ugly, in-development) fill_form() routine for brevity:</p>
  
  <pre><code>void generic_form::fill_form()
  {
      //fields.push_back(NULL);
      if (fields.size() == 1)
      {
          fields.push_back(NULL);
          form = new_form(static_cast&lt;FIELD**&gt;(fields.data()));
          fields.erase((fields.end() - 1));
          assert(fields.size() == 1);
      }
      else
      {
          form = new_form(static_cast&lt;FIELD**&gt;(fields.data()));
      }
      WINDOW* form_win = derwin(screen, fields.size() + 1, largest_desc + 6, ypos, 
              xpos);
      set_form_win(form, form_win);
      set_form_sub(form, form_win);
      //clear the field buffers in case there's junk in them
      for (std::vector&lt;FIELD*&gt;::iterator clear_iter = fields.begin();
              clear_iter != fields.end(); clear_iter++)
      {
          if (*clear_iter != nullptr)
          {
              set_field_buffer(*clear_iter, 0, " ");
          }
      }
      post_form(form);
      for (int x = 0; x &lt; descriptions.size(); x++)
      {
          mvwprintw(form_win, x, 0, descriptions.at(x).c_str());
      }
      wmove(form_win, 0, largest_desc + 1);
      touchwin(screen);
      wrefresh(form_win);
      /* Loop through to get user requests */
      int ch;
      while((ch = getch()) != '\n')//KEY_F(1))
      {   switch(ch)
              {       
              case KEY_DOWN:
                      /* Go to next field */
                      form_driver(form, REQ_NEXT_FIELD);
                      /* Go to the end of the present buffer */
                      /* Leaves nicely at the last character */
                      form_driver(form, REQ_END_LINE);
                      break;
              case KEY_UP:
                      /* Go to previous field */
                      form_driver(form, REQ_PREV_FIELD);
                      form_driver(form, REQ_END_LINE);
                      break;
              case KEY_LEFT:
                      form_driver(form, REQ_PREV_CHAR);
                      break;
              case KEY_RIGHT:
                      form_driver(form, REQ_NEXT_CHAR);
                      break;
  
              // Delete the char before cursor
              case KEY_BACKSPACE:
              case 127:
                      form_driver(form, REQ_DEL_PREV);
                                  break;
  
              // Delete the char under the cursor
              case KEY_DC:
                      form_driver(form, REQ_DEL_CHAR);
                      break;
              default:
                      /* If this is a normal character, it gets */
                      /* Printed                */    
                      form_driver(form, ch);
                      break;
              }
      }
      form_driver(form, REQ_VALIDATION);
      for (int x = 0; x &lt; fields.size() &amp;&amp; first_run; x++)
      {
          //store the int_inputs from the forms
          if ((fields.at(x) != nullptr) &amp;&amp; (field_type(fields.at(x)) ==
                  TYPE_INTEGER))
          {
              int_inputs.push_back(std::atoi(field_buffer((fields.at(x)), 0)));
          }
          if ((fields.at(x) != nullptr) &amp;&amp; (field_type(fields.at(x)) ==
                  TYPE_ALPHA))
          {
              str_inputs.push_back(field_buffer((fields.at(x)), 0));
          }
      }
      first_run = false;
      /* Un post form and free the memory */
      unpost_form(form);
      free_form(form);
      for (int x = 0; x &lt; fields.size(); x++)
      {
          free_field(fields.at(x));
      }
      delwin(form_win);
  }
  </code></pre>
  
  <p>Long story short/TLDR:
  <em>How to I clear or reset a field buffer in ncurses, without deleting and re-adding it?</em></p>