DGnana DGnana - 20 days ago 5
C++ Question

How to: Read and splitt the contents of a .csv file into pre-setup vector arrays [C++]

I would like to know how to read the contents of this .csv file into an array of vectors that I created below:

#include <vector>
#include <fstream>
#include <iostream>
#include <string>
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<int> amount; // Vector holding amount of donated items

ifstream inFile; // Input file
ofstream outFile; // Output file


}


I intend to be able to use these vectors to create a program that allows the user to sort via the following options on a GUI:

1: All by Natural Order of Input File

2: All by Company name, Ascending

3: All by Company name, Descending

4: All by Category, Ascending

5: All by Gift ID, Ascending

6: One Donor only by Value, Descending

Answer

You can read the CSV file line by line like this:

std::string line;
while (std::getline(inFile, line))
{

Then you can split the line with one of the methods mentioned here: Split a string in C++?

And finally you can insert the parts into the vectors:

GID.push_back(splitted[0]);
DName.push_back(splitted[1]);
...

For amount you first Need to convert it to int using atoi or better, strtol

Using These Information you should be able to figure out the final solution by yourself