title: C++ Using getline() inside loop to read in CSV file
body: <p>I'm trying to read in a CSV file that contains rows of 3 people/patients, where col 1 is userid, col 2 is fname, col 3 is lname, col 4 is insurance, and col 5 is version that looks something like below.  </p>
  
  <p>Edit: Apologies, I simply copy/pasted my CSV spreadsheet in here, so it didn't show the commas before.  Wouldn't it look something more like below?  John below also pointed out that there are no commas after the version, and this seemed to fix the issue! Thanks so much John! ( trying to figure out how I can accept your answer :) )</p>
  
  <pre><code>nm92,Nate,Matthews,Aetna,1
  sc91,Steve,Combs,Cigna,2
  ml94,Morgan,Lands,BCBS,3
  </code></pre>
  
  <p>I'm trying to use getline() inside of a loop to read everything in, and it works fine for the first iteration, but getline() seems to be causing it to skip a value on the next iterations.  Any idea how I can solve this?</p>
  
  <p>I'm also not sure why the output looks like below, because I'm not seeing where the lines w/ "sc91" and "ml94" are being printed in the code.  This is what the output of the current code looks like.</p>
  
  <pre><code>userid is: nm92
  fname is: Nate
  lname is: Matthews
  insurance is: Aetna
  version is: 1
  sc91
  userid is: Steve
  fname is: Combs
  lname is: Cigna
  insurance is: 2
  ml94
  version is: Morgan
  userid is: Lands
  fname is: BCBS
  lname is: 3
  
  insurance is:
  version is:
  </code></pre>
  
  <p>I've done a ton of research on differences between getline() and the >> stream operator, but most of the getline() materials seem to revolve around getting input from cin rather than reading from a file like here, so I'm thinking there's something going on w/ getline() and how it's reading the file that I'm not understanding. Unfortunately when I tried >> operator, that forces me to use the strtok() function, and I was struggling a lot with c strings and assigning them to an array of C++ strings.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;                               // for strings
  #include &lt;cstring&gt;                              // for strtok()
  #include &lt;fstream&gt;                              // for file streams
  
  using namespace std;
  
  struct enrollee
  {
      string userid = "";
      string fname = "";
      string lname = "";
      string insurance = "";
      string version = "";
  };
  
  int main()
  {
      const int ENROLL_SIZE = 1000;               // used const instead of #define since the performance diff is negligible,
      const int numCols = 5;                    // while const allows for greater utility/debugging bc it is known to the compiler ,
                                                  // while #define is a preprocessor directive
      ifstream inputFile;                         // create input file stream for reading only
      struct enrollee enrollArray[ENROLL_SIZE];   // array of structs to store each enrollee and their respective data
      int arrayPos = 0;
  
      // open the input file to read
      inputFile.open("input.csv");
      // read the file until we reach the end
      while(!inputFile.eof())
      {
          //string inputBuffer;                         // buffer to store input, which will hold an entire excel row w/ cells delimited by commas
                                                      // must be a c string since strtok() only takes c string as input
          string tokensArray[numCols];
          string userid = "";
          string fname = "";
          string lname = "";
          string insurance = "";
          string sversion = "";
          //int version = -1;
  
          //getline(inputFile,inputBuffer,',');
          //cout &lt;&lt; inputBuffer &lt;&lt; endl;
  
          getline(inputFile,userid,',');
          getline(inputFile,fname,',');
          getline(inputFile,lname,',');
          getline(inputFile,insurance,',');
          getline(inputFile,sversion,',');
  
          enrollArray[0].userid = userid;
          enrollArray[0].fname = fname;
          enrollArray[0].lname = lname;
          enrollArray[0].insurance = insurance;
          enrollArray[0].version = sversion;
  
          cout &lt;&lt; "userid is: " &lt;&lt; enrollArray[0].userid &lt;&lt; endl;
          cout &lt;&lt; "fname is: " &lt;&lt; enrollArray[0].fname &lt;&lt; endl;
          cout &lt;&lt; "lname is: " &lt;&lt; enrollArray[0].lname &lt;&lt; endl;
          cout &lt;&lt; "insurance is: " &lt;&lt; enrollArray[0].insurance &lt;&lt; endl;
          cout &lt;&lt; "version is: " &lt;&lt; enrollArray[0].version &lt;&lt; endl;
      }
  }
  </code></pre>