1

Whenever I attempt to output a line, it outputs the data from the file vertically instead of outputting the full line horizontally. My main goal is to output each line individually and remove commas and repeat till no more lines are in the CSV file.

An example when I run the code:

cout << data[1] << "\t";

Output:

Huggenkizz      Pinzz   White   Dwarf   Dildock Operknockity    DeVille

What I'm trying to get is:

Huggenkizz Amanda 3/18/1997 Sales Associate 2 A A F

My CSV File:

ID,Last Name,First Name,DOB,DtHire,Title,Level,Region,Status,Gender
1,Huggenkizz,Amanda,3/18/1997,,Sales Associate,2,A,A,F
2,Pinzz,Bobby,5/12/1986,,Sales Associate,3,B,A,F
3,White,Snow,12/23/1995,,Sales Associate,2,C,A,F
4,Dwarf,Grumpy,9/8/1977,,Sales Associate,2,C,A,M
5,Dildock,Dopey,4/1/1992,,Sales Associate,1,B,A,M
6,Operknockity,Michael,10/2/1989,,Sales Associate,1,A,S,M
9,DeVille,Cruella,8/23/1960,,Sales Manager,,,A,F

My Code:

vector<string> SplitString(string s, string delimiter)
{
    string section;
    size_t pos = 0;
    vector<string> annualSalesReport;
    while ((pos = s.find(delimiter)) != string::npos) //finds string till, if not returns String::npos
    {
    section = (s.substr(0, pos)); // returns the substring section
    annualSalesReport.push_back(section); // places comma split section into the next array
    s.erase(0, pos + delimiter.length()); // removes the previous string up to the current pos
    }
    annualSalesReport.push_back((s));
 return annualSalesReport;
}
int main() 
{
    vector<string> data;
    string readLine;
    ifstream myIFS;
    myIFS.open("SalesAssociateAnnualReport.csv");
    int lineCounter = 0;
        while (getline(myIFS, readLine))
        {
            lineCounter++;
            if (lineCounter > 1)
            {
                data = SplitString(readLine, ",");
                if (data.size() > 1) //removes top line
                {
                    cout << data[1]<< "\t";
                }
            }
        }
        myIFS.close();
    

    return 0;
}
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
DuckyPGRMR
  • 23
  • 3

1 Answers1

0

Please change your main function as follows

int main() 
{
    vector<vector<string>> data;
    string readLine;
    ifstream myIFS;
    myIFS.open("SalesAssociateAnnualReport.csv");
    int lineCounter = 0;
    while (getline(myIFS, readLine))
    {
        lineCounter++;
        if (lineCounter > 1)
        {
            vector<string> dataLine = SplitString(readLine, ",");
            data.push_back(dataLine);
        }
    }
    myIFS.close();
    // output the first data line of csv file without delimiter and without first column
    for (size_t i = 1; i < data[0].size(); i++)
    {
        cout << data[0][i] << '\t';
    }
    return 0;
}

to get your desired output of

Huggenkizz      Amanda  3/18/1997               Sales Associate 2       A      AF

without having to change your SplitString function.

Please be aware that C++ first array index is always 0 instead of 1.

I've separated the CSV file input processing and the output generation, just to follow the simple programming model IPO:

Input -> Process -> Output

Therefore I've introduced the matrix of strings vector<vector<string>> to store the whole desired CSV file data.

As mentioned in the comments, the SplitString function may be refactored and it should also be fixed to split the last two columns properly.

Hope it helps?

CKE
  • 1,533
  • 19
  • 18
  • 29