HI – A c++ related question. The program’s goal is to read a file of records(CSV) into an array of objects, then loop through the array and print the objects.
an example line from the CSV file is this:
145397,OTOLARYNGOLOGY,SCIANNA,JOSEPH,M.,MD,SOUTHERN IL ENT,2127 MIDLANDS CT SUITE 203,Joliet,IL,60858,(815) 758-8106
the fields are seperated related to this:
The fields for a record (from left to right) are as follows:
Provider number
Provider specialty
Provider last name
Provider first name
Provider middle initial (optional)
Provider title
Provider address (part 1)
Provider address (part 2)
Provider city
Provider state
Provider zip code
Provider phone number
Read each of the fields of the record except the phone number as C++ strings using the getline() function with a comma delimiter
Read the phone number as a C++ string using the getline() function with the default (newline) delimiter
Create a new C++ string for the name by concatenating the last name, first name, middle initial (if present), and title using C++ string concatenation
Use the c_str() method of the string class to convert the C++ string objects to C strings
I am stuck on making a function definition, such as makeProviderArray, to read the CSV file and store each field of data as an object of the Provider class.
Any suggestions would be appreciated
Expert Answer
The question does not provide more information on whether Provider class should consist of only C strings or C++ strings can also be used. Since the question specifies concatenation of name parts and get a C string using c_str, I am assumed, we need to use c-strings in desigining the Provider class. If c++ strings are allowed , then you can replace all char decalrations with string. For output , I have displayed just 3 values- number, name and phone. If you wish, you could display all fields using get…() functions.
I have tested with a CSV file containing 3 dummy records.
Hope the answer helped. If it did, please don’t forget to rate the answer. Thank you very much
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
class Provider
{
private:
char name[30];
char number[10];
char speciality[20];
char address1[30];
char address2[30];
char city[15];
char state[5];
char zip[7];
char phone[15];
public:
Provider()
{
name[0] = ‘