473,385 Members | 1,292 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

vect of struct ??

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
2 1230

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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Josiah Carlson | last post by:
Good day everyone, I have produced a patch against the latest CVS to add support for two new formatting characters in the struct module. It is currently an RFE, which I include a link to at the...
5
by: Roy Hills | last post by:
When I'm reading from or writing to a network socket, I want to use a struct to represent the structured data, but must use an unsigned char buffer for the call to sendto() or recvfrom(). I have...
5
by: PCHOME | last post by:
Hello! I am working on dividing a single C file into several files. Now I encounter a problem about the global variables and can not find a way to solve it. All global variables and codes used...
19
by: Russell Shaw | last post by:
Hi, I have two structs in a header file, and they reference each other, causing a compile error. Is there a standard way to deal with this? typedef struct { ... RtAction *actions; }...
16
by: burn | last post by:
Hello, i am writing a program under linux in c and compile my code with make and gcc. Now i have 4 files: init.c/h and packets.c/h. Each header-file contains some: init.h: struct xyz {
5
by: Johs32 | last post by:
I have a struct "my_struct" and a function that as argument takes a pointer to this struct: struct my_struct{ struct my_struct *new; }; void my_func(struct my_struct *new); I have read...
7
by: Alex | last post by:
If I have two struct. See below: struct s1 { int type; int (*destroy)(struct s1* p); } struct s2 { struct s1 base;
4
by: hobbes992 | last post by:
Howdy folks, I've been working on a c project, compiling using gcc, and I've reached a problem. The assignment requires creation of a two-level directory file system. No files have to be added or...
4
by: hugo.arregui | last post by:
Hi! I have two struts like that: struct { int num; int num2; struct b arrayOfB; } a;
4
by: Sheldon | last post by:
Hi, I have a unique case where I need an array of structs that grows and within this array is another struct that grows in some cases. I'm having trouble allocating memory. Since I have never...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.