vect of struct ??

S

sd2004

Could someone please help ?
I am getting "segmentation fault (core dumped)"
Please explain and show me how to fix the issue.
Thanks in advance for your time.

///////////////// OUTPUT and ERROR MESSAGE ////////////////////
Name : Kait ID: 555 Loan: 2124.8 Principal: 11686.4
Name : Tina ID: 111 Loan: 9184.3 Principal: 56942.7
Name : ID: 0 Loan: 0 Principal: 0
Segmentation fault (core dumped)

////////////////// INPUT FILE : "test6.txt" //////////////////

Kait 555 2124.80 5.5
Tina 111 9184.30 6.2
EOT

/////////////////////// CODE ///////////////////

#include<iostream>
#include <string>
#include<vector>
#include<sstream>
#include<fstream>
using namespace std;

class astruct
{
friend istream &operator>>(istream &,astruct &);

public:
string name;
int id;
float loan;
float interest_rate;
//more ...
// ....
};

istream& operator>>(istream& is, astruct& s){
is >>s.name >> s.id >>s.loan >> s.interest_rate;
return is;
}

int main()
{
vector<astruct> v;
astruct astr;
astruct* astr_ptr;

ifstream in ("test6.txt");
if(!in) {cout << "cannot open file"<<endl;}
string line;
while (in>>astr){
v.push_back(astr);
}

for (astr_ptr=&v[0];astr_ptr->name!="EOT";astr_ptr++){

// I am using for loop so that I can access the data and manipulate
them
//
// ....
// ....
// just an example below:
float total_due = astr_ptr->loan * astr_ptr->interest_rate;

cout<<"Name : "<<astr_ptr->name<<" "<<" ID: "<<astr_ptr->id<<" Loan: "
<<astr_ptr->loan<<" Principal: "<<total_due<< endl;
}
return 0;

}
 
I

int2str

sd2004 said:
Could someone please help ?
I am getting "segmentation fault (core dumped)"
Please explain and show me how to fix the issue.
Thanks in advance for your time.

///////////////// OUTPUT and ERROR MESSAGE ////////////////////
Name : Kait ID: 555 Loan: 2124.8 Principal: 11686.4
Name : Tina ID: 111 Loan: 9184.3 Principal: 56942.7
Name : ID: 0 Loan: 0 Principal: 0
Segmentation fault (core dumped)

////////////////// INPUT FILE : "test6.txt" //////////////////

Kait 555 2124.80 5.5
Tina 111 9184.30 6.2
EOT

/////////////////////// CODE ///////////////////

#include<iostream>
#include <string>
#include<vector>
#include<sstream>
#include<fstream>
using namespace std;

class astruct
{
friend istream &operator>>(istream &,astruct &);

You don't need a friend declaration if your data is all public anyway.
public:
string name;
int id;
float loan;
float interest_rate;
//more ...
// ....
};

istream& operator>>(istream& is, astruct& s){
is >>s.name >> s.id >>s.loan >> s.interest_rate;
return is;
}

int main()
{
vector<astruct> v;
astruct astr;
astruct* astr_ptr;

You should declare variables closer to where they are used.
ifstream in ("test6.txt");
if(!in) {cout << "cannot open file"<<endl;}
string line;
while (in>>astr){

I believe this will fail on the "EOT" line, since you're trying to read
past that in the >> operator.
v.push_back(astr);

Therefore the 'EOT' line never makes it into the array.
}

for (astr_ptr=&v[0];astr_ptr->name!="EOT";astr_ptr++){

Definitely prefer iterating over the vector instead:

vector<astruct>::iterator ii;
for( ii = v.begin(); ii != v.end(); ++ii )
{
 
C

Chris Theis

sd2004 said:
Could someone please help ?
I am getting "segmentation fault (core dumped)"
Please explain and show me how to fix the issue.
Thanks in advance for your time.

///////////////// OUTPUT and ERROR MESSAGE ////////////////////
Name : Kait ID: 555 Loan: 2124.8 Principal: 11686.4
Name : Tina ID: 111 Loan: 9184.3 Principal: 56942.7
Name : ID: 0 Loan: 0 Principal: 0
Segmentation fault (core dumped)

////////////////// INPUT FILE : "test6.txt" //////////////////

Kait 555 2124.80 5.5
Tina 111 9184.30 6.2
EOT
[SNIP]

istream& operator>>(istream& is, astruct& s){
is >>s.name >> s.id >>s.loan >> s.interest_rate;
return is;
}

int main()
{
vector<astruct> v;
astruct astr;
astruct* astr_ptr;

ifstream in ("test6.txt");
if(!in) {cout << "cannot open file"<<endl;}
string line;
while (in>>astr){
v.push_back(astr);
}

First you should start stepping through the loop above with a debugger and
examine closely what happens. In principle you are reading in each line
using the extractor operator that you provide for your astruct structure. If
you look at the implementation you see that it expects a string followed by
a couple of numbers. However, for your last line this is not the case and
thus you'll end up with an invalid stream and consequently exit the loop.
Therefore, EOT is not to be found in your vector and looping over the vector
until EOT is found will fail. In order to avoid such problems you should use
iterators.

for( vector<astruct>::iterator Iter = v.begin(); Iter != v.end(); ++Iter ) {
......
}
for (astr_ptr=&v[0];astr_ptr->name!="EOT";astr_ptr++){

// I am using for loop so that I can access the data and manipulate
them
//
[SNIP]

It is good practice not to rely on more things than absolutely necessary in
your code, if you can avoid it. For example, whoever created the file could
have forgotten to add the EOT flag in the end. But such a minor glitch
should not break your code.

Cheers
Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,995
Messages
2,570,233
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top