Member Avatar for JOLO14

Hi, i have problem .I want to load infomrations abour person from text file. There is name, surname and some unlimited poin.We dont know how many points there is. i try to put this informations into vector<struct> person

struct{
string name
string surname
vector<double> points
}

than i want to load it from .txt but i have problem with this cause i dont know how many points there is. If i know for example, there is 4 points i use

struct{
string name
string surname
double point1
double point2
double point3
double point4
}

here is what i have for now. dont work>} please help, sorry for my english

#include "stdafx.h"
#include <string>
#include <vector>
#include <sstream>
#include <iostream>
#include <fstream>
using namespace std;


struct OSOBA {
    string meno;
    string priezvisko;
    vector<double> body;
};

int _tmain(int argc, _TCHAR* argv[])
{
    ifstream ifsSubor;
    ifsSubor.open("vstup.txt");
    string sRiadok;
    vector<OSOBA> osoby;
    double bod;
    OSOBA o;
    while(! ifsSubor.eof())
    {
        getline(ifsSubor,sRiadok);
        istringstream istrRiadok(sRiadok);
        istrRiadok>> o.meno;
        istrRiadok>> o.priezvisko;
        istrRiadok>> bod;
        o.body.push_back(bod);

    osoby.push_back(o);

int i=0;
    vector<OSOBA>::iterator it;

    for(it = osoby.begin(); it != osoby.end(); ++it)
    {
        cout.width(15);
    cout <<left << (*it).priezvisko <<"\t" << (*it).meno<<"\t" << (*it).body<<endl; 

    }
        return 0;


    }
}

What does the data in the file look like?

Member Avatar for JOLO14

Oh sorry i forgot .txt looks like this

Mrkvicka Jozef 2 1.75 2.1 0.4 1 0.49 1.27 
Hrasko Jan 0.1 0.38 1 1.2
Mladek Peter 1.6 0.4 2 1.3 1.8
Petrzlen Robert 0.6 0 1.1 1,1 0.6 0.59 
Horvath Ivan 1.6 0.8 0.7 1.3

for example mrkvicka is surname, jozef is name ...its in my native language

Well with the struct that you have and the way the file is formatted I would use the following approach.

int main()
{
    ifstream ifsSubor;
    ifsSubor.open("vstup.txt");
    string sRiadok;
    vector<OSOBA> osoby;
    double bod;
    OSOBA o;
    stringstream ss;
    while(getline(ifsSubor, sRiadok))  // use getline to control loop instead of eof()
    {
        ss << line;  // load line into strinstream
        ss >> o.meno;  // get surname
        ss. >> o.priezvisko;  // get first name
        while (ss >> bod)  // while there are points left in ss add them to body
        {
            o.body.push_back(bod);
        }
        osoby.push_back(o);
    }
    //...
Member Avatar for JOLO14

I dont know but it dont work properlly
my console output is

Jozef Mrkvicka
Jozef Mrkvicka
Jozef Mrkvicka
Jozef Mrkvicka
Jozef Mrkvicka

and my code is

#include "stdafx.h"
#include <string>
#include <vector>
#include <sstream>
#include <iostream>
#include <fstream>
using namespace std;


struct OSOBA {
    string meno;
    string priezvisko;
    vector<double> body;
};

int _tmain(int argc, _TCHAR* argv[])
{
    vector<double> body;
    ifstream ifsSubor;
    ifsSubor.open("vstup.txt");
    string sRiadok;
    vector<OSOBA> osoby;
    double bod;
    OSOBA o;
    stringstream ss;

while(getline(ifsSubor, sRiadok))
    {
        ss<<sRiadok;
        ss >> o.meno;
        ss >> o.priezvisko;

        while (ss >> bod) 
        {
            o.body.push_back(bod);

        }
    osoby.push_back(o);
    }

     int i=0;
    vector<OSOBA>::iterator it;

for(it = osoby.begin(); it != osoby.end(); ++it)
    {
        cout.width(5);
        cout <<left << (*it).priezvisko <<"\t" << (*it).meno; 

        for (int i=0; i<body.size(); i++)
            cout << body[i] << ", ";
        cout<<endl;
    }
cout<<endl;
        return 0;
}

You need to put OSOBA o; in the scope of the while loop and reset the stringstream.

    while(getline(ifsSubor, sRiadok))
    {
        // scope OSOBA instance
        OSOBA o;

        ss << sRiadok;
        ss >> o.meno;
        ss >> o.priezvisko;

        while (ss >> bod) 
        {
            o.body.push_back(bod);

        }
        // reset ss
        ss.str(std::string());
        ss.clear();

        osoby.push_back(o);
    }

In the iteration of the osoby vector, you're not referencing the instance while iterating the vector of doubles.

for (size_t i=0; i<it->body.size(); i++)
            cout << it->body[i] << ", ";
Member Avatar for JOLO14

ok, ty,it work now

Oops my Bad. Thanks for fixing my mistake nullptr.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.