0

I want to store the data from a file include string and float into vectors and print all of them. I need to How do I do?

I have declared these string and float.

std::string id; //e.g u1234
std::string name; 
std::string postcode;
std::string city;
float purchases; //Format with decimal point and comma separators for 1000’s
float returns;//Format with decimal point and comma separators for 1000’s
std::string cIDs;

A example of data: 123,Frodo inc,2006, lyons,"1,021,000.16",0.0, U2123,Sam Inc,2006, lyons,"21,600.00",13.10,123

A example of result:

123 Frodo inc 2006 lyons 1,021,000,16 0.0 U2123 Sam Inc 2006 lyons 21,600.00 13.10

I need to parse the comma and double quote.

This is what I have done.

#include <string>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include<fstream>
#include<vector>
#include<sstream>
 
#ifdef _WIN32
#include <Windows.h>
#else
#include <unistd.h>
#endif

using namespace std;

vector <int>_id;
vector<std::string>_name;
vector<std::string>_postcode;
vector<std::string>_city;
vector<float>_purchases;
vector<float>_returns;
vector<std::string>_cIDs;




bool readFile()
{

    std::ifstream myFile("Data1.txt");
    int i = 0;

    string id, name, postcode, city, purchases, returns, cIDs;

    string line;

    if (myFile.is_open())
    {

        getline(myFile, line);

        while (!myFile.eof())
        {
            getline(myFile, id, ',');
            _id.push_back(stof(id));

            getline(myFile, name, ',');
            _name.push_back(name);

            getline(myFile, postcode, ',');
            _postcode.push_back(postcode);

            getline(myFile, city, ',');
            _city.push_back(city);

            //getline(myFile, purchases, ',');
            //_purchases.push_back(stof(purchases));

            //getline(myFile, returns, ',');
            //_returns.push_back(stof(returns));

            getline(myFile, cIDs, ',');
            _cIDs.push_back(cIDs);

            i += 1;
        }
        myFile.close();
        cout << "Number of entries: " << i - 1 << endl;
    }
    else cout << "Unable to open file";


    float purchases0, returns0;
    string name_, postcode_, city_, cIDs_;
    for (int j = 0; j < i; j++)
    {
        if (_id[j] == 0) //this part does not work at all.
       {
            name = _name[j];
            postcode = _postcode[j];
            city = _city[j];
            purchases0 = _purchases[j];
            returns0 = _returns[j];
            cIDs = _cIDs[j];
        }
    }
    cout << endl;
    return false;
    
}

Cui
  • 1
  • 1
  • 3
    [Please do not post images of texts because they are hard to use.](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-errors-when-asking-a-question) Texts should be posted directly **as text** in your question. – MikeCAT Apr 05 '21 at 13:17
  • 1
    Best idea is probably to combine all these variables into a `struct` or `class` and create a `std::vector` of this one instead of having many separate `std::vector` and `std::vector` variables. – πάντα ῥεῖ Apr 05 '21 at 14:14

0 Answers0