Member Avatar for nana_1

Hi, I need help reading this .csv file into an array of vectors i created below:

`vector<string> GID;                // Vector holding Gift ID
vector<string> DName;           // Vector holding Donor Name
vector<string> Phone;           // Vector holding Phone Number
vector<string> POC;             // Vector holding point of contact
vector<string> Item;            // Vector holding item donated 
vector<string> Category;        // Vector holding type of item donated
vector<double> amount;          // Vector holding amount of donated items`

and also how do i remove the commas so i can use it do mathematical operations

Basic programming stuff. Show your code and I'll show you where you are in error.

Member Avatar for Nana_4

@rubberman
second account here.
That's the thing, I do not know how, I've searched high and low but I cannot find anything that applies to mutliple vectors. All examples, read the line, parse for the commas then place the entire line into one vector. I need to read the line, parse it to remove commas then take each string in the indiviudual cell and place it into separate distinct vectors that I created above. So basically how to split the decomma line into segements or tokens as they say into the vectors.
So for example lets thake the first two lines of the csv file:

GIFT-01,Fresch Foundation,(703) 555-0054,Lopez,T-Shirts,Clothing,25

GIFT-03,AAA Marketing Inc,(703) 555-0071,Sinclair,Tickets,Theater,20

I want to read the file for this line above for example, remove the commas, then split the line and place it into vectors that I created so:
after parse for commas:

GIFT-01Fresch Foundation(703) 555-0054LopezT-ShirtsClothing25

GIFT-03AAA Marketing Inc(703) 555-0071SinclairTicketsTheater20

then split the line so I place specific parts of the line into the vectors so:

vector<string> GID; = GIFT-01, GITF-03,(and more rows) // Vector holding Gift ID
vector<string> DName; = Fresch Foundation, AAA Marketing Inc, (and more rows) // Vector holding Donor Name
vector<string> Phone; = (703) 555-0054, (703) 555-0071, (and more rows) // Vector holding Phone Number
vector<string> POC; = Lopez, Sinclair, (and more rows) // Vector holding point of contact
vector<string> Item; = T-Shirts, Tickets, (and more rows) // Vector holding item donated
vector<string> Category; = Clothing, Theater, (and more rows) // Vector holding type of item donated
vector<double> amount; = 25, 20, (and more rows) // Vector holding amount of donated items`

This is the closest thing I've found but I have seven straight cells of data not two so I do not know how to implement it into mine.

Seems like a job for getline using a comma as the delimiter.

http://www.cplusplus.com/reference/string/string/getline/

Another way to do it is to read a line at a time, then use the one of the find functions in the string library along with the substr function and split the line into strings delimited by the comma.

http://www.cplusplus.com/reference/string/string/

For the file you posted, something like this perhaps. Change "object" and "str" to meaningful names.

#include <string>
#include <iostream>
#include <fstream>
using namespace std;

struct object
{
    string str1;
    string str2;
    string str3;
    string str4;
    string str5;
    string str6;
    string str7;
};

int main()
{
    ifstream ins;
    ins.open("inputfile.txt");
    object anObject;
    for(int i = 0; i < 2; i++)
    {
        getline(ins, anObject.str1, ',');
        getline(ins, anObject.str2, ',');
        getline(ins, anObject.str3, ',');
        getline(ins, anObject.str4, ',');
        getline(ins, anObject.str5, ',');
        getline(ins, anObject.str6, ',');
        getline(ins, anObject.str7, '\n');

        cout << anObject.str1 << "\t";
        cout << anObject.str2 << "\t";
        cout << anObject.str3 << "\t";
        cout << anObject.str4 << "\t";
        cout << anObject.str5 << "\t";
        cout << anObject.str6 << "\t";
        cout << anObject.str7 << "\n\n";
    }
    ins.close();
    return 0;
}
Member Avatar for Nana_4

@AssertNull
Thank you!

quick question, I am limited to using vectors not structure so i took your code and tried to 'merge' it to look like this but I got an error. Please help debug

#include <string>
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

int main()
{
    vector<string> GID;                // Vector holding Gift ID
    vector<string> DName;           // Vector holding Donor Name
    vector<string> Phone;           // Vector holding Phone Number
    vector<string> POC;             // Vector holding point of contact
    vector<string> Item;            // Vector holding item donated 
    vector<string> Category;        // Vector holding type of item donated
    vector<double> amount;          // Vector holding amount of donated items

    ifstream ins;
    ins.open("Donations.csv");
    object aLine;
    for(int i = 0; i < 30; i++) // 30 because its thirty rows of data ?
    {
        getline(ins, aLine.gid1, ',');
        getline(ins, aLine.dname2, ',');
        getline(ins, aLine.phone3, ',');
        getline(ins, aLine.poc4, ',');
        getline(ins, aLine.item5, ',');
        getline(ins, aLine.category6, ',');
        getline(ins, aLine.amount7, '\n');

        // place files into vector
        GID.push_back(gid1);
        DName.push_back(dname2);
        Phone.push_back(phone3);
        POC.push_back(poc4);
        Item.push_back(item5);
        Category.push_back(category6);
        amount.push_back(amount7);

    }
    ins.close();
    return 0;
}

Give this a whirl. No structure needed, but since you took it out, take it all the way out. You left remnants of code referring to a structure you removed the definition for, so the compiler/linker couldn't find anything called "object". See code below. I simply defined a string and read string into it, converted it to a double if necessary, then pushed it back onto the correct vector.

Regarding the for-loop, you could certainly use a for-loop if you know how many records you have in advance. If not, the code below works for any number of lines in the file by using the eof() function.

#include <string>
#include <iostream>
#include <fstream>
#include <vector>
#include <cstdlib>
using namespace std;

int main()
{
    vector<string> GID;                // Vector holding Gift ID
    vector<string> DName;           // Vector holding Donor Name
    vector<string> Phone;           // Vector holding Phone Number
    vector<string> POC;             // Vector holding point of contact
    vector<string> Item;            // Vector holding item donated 
    vector<string> Category;        // Vector holding type of item donated
    vector<double> amount;          // Vector holding amount of donated items

    ifstream ins;
    ins.open("Donations.csv");

    // temporary variables
    string str;
    double temp;

    getline(ins, str, ','); // read first GID string outside of loop so that we
                            // won't accidentally reach the end of the file in the
                            // MIDDLE of the loop
    while(!ins.eof()) // end of file not reached
    {
        GID.push_back(str);
        getline(ins, str, ',');
        DName.push_back(str);
        getline(ins, str, ',');
        Phone.push_back(str);
        getline(ins, str, ',');
        POC.push_back(str);
        getline(ins, str, ',');
        Item.push_back(str);
        getline(ins, str, ',');
        Category.push_back(str);

        getline(ins, str, '\n');
        // convert string to double using strtod and c_str
        temp = strtod(str.c_str(), NULL);

        // now push double onto vector
        amount.push_back(temp);

        getline(ins, str, ','); // read in GID here instead of the top.  If we are at the end
                                // of the file, it won't work and end-of-file flag will be set,
                                // which is what we want.
    }
    ins.close();

    // print everything out to make sure it's all good
    // note that number of elements in each vector should be the same,
    // so use GID's size
    int numElements = GID.size();
    for(int i = 0; i < numElements; i++)
    {
        cout << GID[i] << '\t';
        cout << DName[i] << '\t';
        cout << Phone[i] << '\t';
        cout << POC[i] << '\t';
        cout << Item[i] << '\t';
        cout << Category[i] << '\t';
        cout << amount[i] << '\n';
    }

    return 0;
}
Member Avatar for nana_1

@AssertNull

thank you, seriously thank you so much, I've spent the last four days looking for answers.

you sir, are awesome!

You are very welcome. Glad you got it working.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.