Seihyung Oh
Seihyung Oh

Reputation: 13

How can I read the data from text file and load it into the vector?

I want to read a data from text file using structure and load it into vector.

so I wrote a following to do this but I failed to compile. I don't know what's wrong.

< what I did >

  1. text file contains data as follows;

835,5,0,0,1,1,8.994,0

(integer array[3], integer,integer,integer,integer,integer, float, Boolean)

2.I declared a structure contains following data types to load data into vector;

struct unfinished
{
    int ans[3]; // contains answer
    int max;
    int st;
    int ba;
    int outs;
    int tri;
    float elapsed;
    bool fin;
};

3.I wrote a code to read a data as follows;

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

struct unfinished
{
   int ans[3];
   int max;
   int st;
   int ba;
   int outs;
   int tri;
   float elapsed;
   bool fin;
};

vector<unfinished> read_rec(istream & is)
{
  vector<unfinished> rec;
  int ans[3];
  int max, st, ba, outs, tri;
  float elap;
  bool fini;

  while (is >> ans[0] >> ans[1] >> ans[2] >> max >> st >> ba >> outs >> tri    >> elap >> fini)
{
    rec.emplace_back(ans[0], ans[1], ans[2], max, st, ba, outs, tri, elap, fini);
}
return rec;
}

int main(void)
{
ifstream infile("unfin_rec.txt");
auto unfin = read_rec(infile);

vector<unfinished>::const_iterator it;

for (it = unfin.begin(); it != unfin.end(); it += 1)
{
    cout << it->ans[0] << it->ans[1] << it->ans[2] << "," << it->max << "," << it->st << "," << it->ba <<","<<it->outs<<","<<it->tri<<","<<it->elapsed<<","<<it->fin<< endl;
}

system("pause");
return 0;
}

I failed to compile this code. error message was: >c:\program files (x86)\microsoft visual studio 12.0\vc\include\xmemory0(600): error C2661: 'unfinished::unfinished' : no overloaded function takes 10 arguments

again, I couldn't figure out what this message means. Please help!

thanks,

seihyung

Upvotes: 1

Views: 364

Answers (1)

emvee
emvee

Reputation: 4449

You need to add a constructor which takes 10 arguments and fills in the members of the struct, like so:

struct unfinished
{
    unfinished(int a0, int a1, int a2,
               int m, int s, int b, int o, int t,
               float e, bool b):
        max(m), st(s), ba(b), outs(o), tri(t), elap(e), fini(b) {
           ans[0]=a0, ans[1]=a1, ans[2]=a2;
    }
    ....
};

Upvotes: 1

Related Questions