Search code examples
c++tokenizestringstream

comma separated stringstream error. it also separates '-'


The following is a set of data which I want to save in an array, struct using C++.

3110,300,15500,1,2017-11-29,8835,010-9033-1234
3110,396,530,1,2017-11-29,8835,010-9033-1234
3110,401,450,2,2017-11-29,8835,010-9033-1234

I used the following help to do so. How to use stringstream to separate comma separated strings But I encountered two problems. Dates are saved as a:

2017

and phone numbers are saved as:

10

Instead, I wish to save them both as strings.

2017-11-29
010-9033-1234

Below is the code I made:

while (fileIN.good()) {
    while (getline(fileIN, lineA)) {
        cout << lineA << endl;
        istringstream ss(lineA); colA = 0;
        while (getline(ss, token, ',')) {
            if (colA = 0) { Data[rowA].price = stoi(token); cout << Data[rowA].price << endl; }
            else if (colA = 1) { Data[rowA].goods_seq = stoi(token); cout << Data[rowA].goods_seq << endl;}
            else if (colA = 2) { Data[rowA].goods_unit_price = stoi(token); cout << Data[rowA].goods_unit_price << endl;}
            else if (colA = 3) { Data[rowA].ea = stoi(token); cout << Data[rowA].ea << endl;}
            else if (colA = 4) { Data[rowA].want_date = token; cout << Data[rowA].want_date <<endl;}
            else if (colA = 5) { Data[rowA].member_seq = stoi(token); cout << Data[rowA].member_seq << endl;}
            else if (colA = 6) { Data[rowA].shipping_cellphone = token; cout << Data[rowA].shipping_cellphone << endl;}
            colA++;
        }
        rowA++;
    }
}

Solution

  • In order to solve the problem you need to do == this occurs on if (colA = 0) which should be if (colA == 0)

    Hope this helps