By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,147 Members | 1,466 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,147 IT Pros & Developers. It's quick & easy.

vect of struct ??

P: n/a
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;

}

Dec 10 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

sd2004 wrote:
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 )
{
cout << ii->name; // etc.
}

// 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;

}


Dec 10 '05 #2

P: n/a

"sd2004" <tv****@hotmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
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
Dec 10 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.