Determining whether to concatenate CSV values or not

I am writing a program that needs to read in a CSV file of customers. However, some customers are people names in the format of "LastName, FirstName" and some of them are companies in the format of "Company ABC Name".

My program reads in the data as such:

1
2
3
4
5
6
7
string record;

getline(fin, myRecords[i].custName, ',');
if(i != 0) {
  getline(fin, record, ',');
  myRecords[i].custName = myRecords[i].custName + "," + record; 
}


The reason it checks for i != 0, is because the first line is an instance of the object that holds the column headers for the CSV (as expressed in Excel anyways).

As you can see, I getline to the next comma (since it's a comma separated values file), and when the custName doesn't have a comma in the value, it pulls in the next value, which puts everything in that record off, and when I try to process the data, the program crashes.

Is there any way the program can determine if it's reading in a last name, and should expect a first name, or if the name doesn't have a comma?

¿could you?

Change the delimiter, quote your fields, parse from right to left, etc.
Last edited on
Topic archived. No new replies allowed.