title: How do I write a message in the Protocol Buffer C++ tutorial?
body: <p>I am new to Protocol Buffers and inexperienced with C++, I am trying to complete the tutorial at <a href="https://developers.google.com/protocol-buffers/docs/cpptutorial" rel="nofollow">https://developers.google.com/protocol-buffers/docs/cpptutorial</a> </p>
  
  <p>I've created the proto file mentioned in the tutorial and gotten <code>addressbook.pb.h</code> and <code>addressbook.pb.cc</code> from this proto. I am trying to follow the segment "Writing A Message", so I copied and pasted the following code from the tutorial. I immediately run into an issue in the main function, which I'll explain below:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;string&gt;
  #include "addressbook.pb.h"
  using namespace std;
  
  // This function fills in a Person message based on user input.
  void PromptForAddress(tutorial::Person* person) {
    cout &lt;&lt; "Enter person ID number: ";
    int id;
    cin &gt;&gt; id;
    person-&gt;set_id(id);
    cin.ignore(256, '\n');
  
    cout &lt;&lt; "Enter name: ";
    getline(cin, *person-&gt;mutable_name());
  
    cout &lt;&lt; "Enter email address (blank for none): ";
    string email;
    getline(cin, email);
    if (!email.empty()) {
      person-&gt;set_email(email);
    }
  
    while (true) {
      cout &lt;&lt; "Enter a phone number (or leave blank to finish): ";
      string number;
      getline(cin, number);
      if (number.empty()) {
        break;
      }
  
      tutorial::Person::PhoneNumber* phone_number = person-&gt;add_phone();
      phone_number-&gt;set_number(number);
  
      cout &lt;&lt; "Is this a mobile, home, or work phone? ";
      string type;
      getline(cin, type);
      if (type == "mobile") {
        phone_number-&gt;set_type(tutorial::Person::MOBILE);
      } else if (type == "home") {
        phone_number-&gt;set_type(tutorial::Person::HOME);
      } else if (type == "work") {
        phone_number-&gt;set_type(tutorial::Person::WORK);
      } else {
        cout &lt;&lt; "Unknown phone type.  Using default." &lt;&lt; endl;
      }
    }
  }
  
  // Main function:  Reads the entire address book from a file,
  //   adds one person based on user input, then writes it back out to the same
  //   file.
  int main(int argc, char* argv[]) {
    // Verify that the version of the library that we linked against is
    // compatible with the version of the headers we compiled against.
    GOOGLE_PROTOBUF_VERIFY_VERSION;
  
    if (argc != 2) {
      cerr &lt;&lt; "Usage:  " &lt;&lt; argv[0] &lt;&lt; " ADDRESS_BOOK_FILE" &lt;&lt; endl;
      return -1;
    }
  
    tutorial::AddressBook address_book;
  
    {
      // Read the existing address book.
      fstream input(argv[1], ios::in | ios::binary);
      if (!input) {
        cout &lt;&lt; argv[1] &lt;&lt; ": File not found.  Creating a new file." &lt;&lt; endl;
      } else if (!address_book.ParseFromIstream(&amp;input)) {
        cerr &lt;&lt; "Failed to parse address book." &lt;&lt; endl;
        return -1;
      }
    }
  
    // Add an address.
    PromptForAddress(address_book.add_person());
  
    {
      // Write the new address book back to disk.
      fstream output(argv[1], ios::out | ios::trunc | ios::binary);
      if (!address_book.SerializeToOstream(&amp;output)) {
        cerr &lt;&lt; "Failed to write address book." &lt;&lt; endl;
        return -1;
      }
    }
  
    // Optional:  Delete all global objects allocated by libprotobuf.
    google::protobuf::ShutdownProtobufLibrary();
  
    return 0;
  }
  </code></pre>
  
  <p>In the main function the code exits without prompting for any inputs due to this portion:</p>
  
  <pre><code>  if (argc != 2) {
      cerr &lt;&lt; "Usage:  " &lt;&lt; argv[0] &lt;&lt; " ADDRESS_BOOK_FILE" &lt;&lt; endl;
      return -1;
    }
  </code></pre>
  
  <p>which appears as one of the first few lines in the function, before any inputs are requested. I read that <code>argc</code> will be the number of inputs. I'm confused because I copied and pasted exactly what the tutorial wrote, but it doesn't appear to be running correctly.</p>