vector person
May 13, 2014 at 8:03pm UTC
Hi, i rly like to ask u for help with myprogram. my program shoudl load informations about person from .txt file and cout them to console. Txt file has format
1 2 3 4 5
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
So what i want is put name, surname and unnlimited, not-knowing points to vector<struct>
Here is my code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
#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;
}
problem is in concole it write 5 times same name and surname
1 2 3 4 5
Mrkvicka Jozef
Mrkvicka Jozef
Mrkvicka Jozef
Mrkvicka Jozef
Mrkvicka Jozef
Really apreciate any help , ty
May 13, 2014 at 9:07pm UTC
In your text file, line 4, you have 1,1 instead of 1.1
In your source file, before line 29, add:
1 2
ss.clear();
o.body.clear();
This clears out the stringstream from which you are reading and the array of doubles that your are reading to get ready for the next iteration.
In lines 49 and 50, change
body
to
it->body
.
May 13, 2014 at 9:18pm UTC
thank u it works
Last edited on May 14, 2014 at 10:04am UTC
May 13, 2014 at 9:39pm UTC
oh and i have just one little question. i have problem with alignment of first point in each line as u can see, next points are ok, alignment with tabulator
This is code only for cout
1 2 3 4 5 6 7 8 9 10 11 12
for (it = osoby.begin(); it != osoby.end(); ++it)
{
cout.width(5);
cout <<left << (*it).priezvisko <<"\t" << (*it).meno;
for (int i=0; i<it->body.size(); i++)
cout<<it->body[i] <<"\t" ;
cout<<endl;
}
return 0;
}
and this is how looks output
Mrkvicka Jozef2 1.75 2.1 0.4 1 0.49 1.27
Hrasko Jan0.1 0.38 1 1.2
Mladek Peter1.6 0.4 2 1.3 1.8
Petrzlen Robert0.6 0 1.1 1.1 0.6 0.59
Horvath Ivan1.6 0.8 0.7 1.3 1
i try to put "\t" everywhere but dont work
Last edited on May 13, 2014 at 9:44pm UTC
May 14, 2014 at 10:30am UTC
ok i got it
Topic archived. No new replies allowed.