HI - A c++ related question. The program\'s goal is to read a file of records(CS
ID: 3884380 • Letter: H
Question
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:
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
Explanation / 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] = '';
number[0] = '';
speciality[0] = '';
address1[0] = '';
address2[0] = '';
city[0] = '';
state[0] = '';
zip[0] = '';
phone[0] = '';
}
char* getName()
{
return name;
}
void setName(const char *s)
{
strcpy(name, s);
}
char* getNumber()
{
return number;
}
void setNumber(const char *s)
{
strcpy(number, s);
}
char* getSpeciality()
{
return speciality;
}
void setSpeciality(const char *s)
{
strcpy(speciality, s);
}
char* getAddress1()
{
return address1;
}
void setAddress1(const char *s)
{
strcpy(address1, s);
}
char* getAddress2()
{
return address2;
}
void setAddress2(const char *s)
{
strcpy(address2, s);
}
char* getCity()
{
return city;
}
void setCity(const char *s)
{
strcpy(city, s);
}
char* getState()
{
return state;
}
void setState(const char *s)
{
strcpy(state, s);
}
char* getZipcode()
{
return zip;
}
void setZipcode(const char *s)
{
strcpy(zip, s);
}
char* getPhone()
{
return phone;
}
void setPhone(const char *s)
{
strcpy(phone, s);
}
};
int loadCSVFile(ifstream &infile, Provider providers[]); //loads from infile into the array and returns the count of records loaded
void display(Provider providers[], int count);
int main()
{
string filename;
cout << "Enter filename: ";
cin >> filename;
ifstream infile(filename.c_str());
if(!infile.is_open())
{
cout << "Error opening input file - " << filename << endl;
return 1;
}
Provider providers[100]; //an array of maximum 100 providers
int count = loadCSVFile(infile, providers);
cout << "Loaded " << count << " records from file " << endl;
display(providers, count);
}
int loadCSVFile(ifstream &infile, Provider providers[])
{
int count = 0;
string value;
string first, last, middle, title;
while(true)
{
getline(infile, value, ','); //use comma as delimiter to get value, get number
if(infile.eof())
break;
providers[count].setNumber(value.c_str());
getline(infile, value, ','); // get speciality
providers[count].setSpeciality(value.c_str());
getline(infile, last, ','); // get lastname
getline(infile, first, ','); // get firstname
getline(infile, middle, ','); // get middle
getline(infile, title, ','); // get title
//concatenate the name parts
value = title + " " + first + " " ;
if(middle != "")
value = value + middle;
value = value + " " + last;
providers[count].setName(value.c_str());
getline(infile, value, ','); // get address1
providers[count].setAddress1(value.c_str());
getline(infile, value, ','); // get address2
providers[count].setAddress2(value.c_str());
getline(infile, value, ','); // get city
providers[count].setCity(value.c_str());
getline(infile, value, ','); // get state
providers[count].setState(value.c_str());
getline(infile, value, ','); // get zip code
providers[count].setZipcode(value.c_str());
getline(infile, value, ' '); // get phone, use as delimiter
providers[count].setPhone(value.c_str());
count++;
}
infile.close();
return count;
}
void display(Provider providers[], int count)
{
for(int i = 0; i < count; i++)
{
cout << providers[i].getNumber() << " " << providers[i].getName() << " " << providers[i].getPhone() << endl;
}
cout << endl;
}
input file : providers.csv
145397,OTOLARYNGOLOGY,SCIANNA,JOSEPH,M.,MD,SOUTHERN IL ENT,2127 MIDLANDS CT SUITE 203,Joliet,IL,60858,(815) 758-8106
123456,OTOLARYNGOLOGY,WILLIAMS,ROBERT,,MD,SOUTHERN IL ENT,2127 MIDLANDS CT SUITE 203,Joliet,IL,60858,(815) 752-3452
555555,OTOLARYNGOLOGY,JACKSON,MICHAEL,S,MD,SOUTHERN IL ENT,2127 MIDLANDS CT SUITE 203,Joliet,IL,60858,(815) 752-4444
output
Enter filename: providers.csv
Loaded 3 records from file
145397 MD JOSEPH M. SCIANNA (815) 758-8106
123456 MD ROBERT WILLIAMS (815) 752-3452
555555 MD MICHAEL S JACKSON (815) 752-4444
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.