skinny d
Feb 9 2004, 07:42 PM
OK, I'm trying to write a program and I'm stuck. When I try to output, I get a segmentation fault. The program is supposed to use pointers and linked lists. I have 2 files that I need to read in (the data is some names and ages), then I need to compare the 2 lists and output the differences. Since the data is heterogenous, I need to create structs to fill the array or whatever. I'm VERY confused so any help would be fantastic. Feel free to change/manipulate any of the code. THANK YOU... here is the source (incomplete):
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct listnode;
typedef listnode * nodeptr;
struct listnode
{
string name;
int age;
nodeptr next;
};
ifstream fin1;
ifstream fin2;
const int size = 0;
void fillstruct1(ifstream& fin1, nodeptr& temp1);
void fillstruct2(ifstream& fin2, nodeptr& temp2);
void add2end(nodeptr& head, nodeptr newptr);
int main()
{
nodeptr headptr1 = NULL;
nodeptr headptr2 = NULL;
nodeptr temp1 = NULL;
nodeptr temp2 = NULL;
nodeptr last1 = NULL;
nodeptr last2 = NULL;
fin1.open("myfile1prog2.dat");
if(!fin1.fail())
{
cout << "Reading file one...complete." << endl;
}
fin2.open("myfile2prog2.dat");
if(!fin2.fail())
{
cout << "Reading file two...complete." << endl;
}
fillstruct1(fin1, temp1);
add2end(headptr1, temp1);
while(!fin1.eof())
{
fillstruct1(fin1, temp1);
add2end(headptr1, temp1);
}
cout << "Structure one filled successfully!" << endl;
fillstruct2(fin2, temp2);
add2end(headptr2, temp2);
while(!fin2.eof())
{
fillstruct2(fin2, temp2);
add2end(headptr2, temp2);
}
cout << "Structure two filled successfully!" << endl;
}
// Functions to fill structures
void fillstruct1(ifstream& fin1, nodeptr& temp1)
{
if(!fin1.eof())
{
temp1 = new listnode;
fin1 >> temp1->name;
fin1 >> temp1->age;
temp1 = temp1->next = NULL;
}
}
void fillstruct2(ifstream& fin2, nodeptr& temp2)
{
if(!fin2.eof())
{
temp2 = new listnode;
fin2 >> temp2->name;
fin2 >> temp2->age;
temp2 = temp2->next = NULL;
}
}
// Function to link nodes
void add2end(nodeptr& head, nodeptr newptr)
{
nodeptr temp;
if(head == NULL)
{
head = newptr;
}
else
{
temp = head;
while(temp->next != NULL)
{
temp = temp->next;
}
temp->next = newptr;
}
return;
}
skinny d
Feb 9 2004, 07:44 PM
Here are the two files. Myfile1prog2.dat & Myfile2prog2.dat
myfile1prog2.dat:
Bill 29
Damion 666
Sue 27
Beth 11
Sue 41
Mike 55
Al 20
Julie 39
Tom 67
Peter 80
Ellen 53
Alicia 7
Megan 8
Bridget 44
myfile2prog2.dat:
Bill 29
Damion 666
Sue 27
Beth 11
Sue 41
Mike 55
Alex 20
Julie 39
Tom 67
Peter 80
Ellen 56
Alicia 7
Megan 8
The_Preacher
Feb 9 2004, 08:18 PM
Seg faults are ususally memory accesses that are outside of allocated memory. (says the resident g33k). For exaplme accessing element 101 of an array that are only 100 elements big. just wait for the program to try and acess a bit of memory thats allready owned by something else and thats ur seg fault.
Does this help?
Edit: null pointers are classics for this
skinny d
Feb 9 2004, 08:21 PM
pimp, im really an amateur c++ programmer and that helped explain the problem, but i dont know how to fix it
The_Preacher
Feb 9 2004, 09:37 PM
Hey mate,
If you are doin this for fun it strikes me that u might not want a full answer, do you just want a nudge in the right direction?
skinny d
Feb 9 2004, 09:39 PM
i would definitely like some kind of correct algorithm. if you could work the entire thing... FANTASTIC. i would be able to look at it and learn. god help me
The_Preacher
Feb 9 2004, 09:51 PM
The g33k wants to know how far it gets?
skinny d
Feb 9 2004, 09:51 PM
it compiles but anytime i try to output ANYTHING, i get a seg fault.
The_Preacher
Feb 9 2004, 09:54 PM
is that right?
does "Reading file one...complete." never print when you run your program?
skinny d
Feb 9 2004, 09:59 PM
that prints fine... i mean.. when i add more code to output headptr1 or temp1, i get a seg fault
space is deep
Feb 9 2004, 09:59 PM
| QUOTE (skinny d @ Feb 9 2004, 08:42 PM) |
| I have 2 files that I need to read in (the data is some names and ages), then I need to compare the 2 lists and output the differences. |
Why not use diff(1)?
| QUOTE (skinny d @ Feb 9 2004, 08:42 PM) |
fin1.open("myfile1prog2.dat"); if(!fin1.fail()) { cout << "Reading file one...complete." << endl; } fin2.open("myfile2prog2.dat"); if(!fin2.fail()) { cout << "Reading file two...complete." << endl; }
|
You need to add:
else { /* return an error message. */ }
...to each of those blocks (I don't know c++ so can't suggest exact code).
Your code compiled and ran fine here:
| QUOTE (My Computer @ Feb 9 2004, 09:42 PM) |
sid@localhost:~/src/c/test$ gcc -o diff diff.cxx -lstdc++ sid@localhost:~/src/c/test$ ./diff Reading file one...complete. Reading file two...complete. Structure one filled successfully! Structure two filled successfully! sid@localhost:~/src/c/test$ gcc --version gcc (GCC) 3.3.2 (Debian) Copyright © 2003 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
sid@localhost:~/src/c/test$
|
But if one of the files is missing it tries to eat all my memory.

HTH
skinny d
Feb 9 2004, 10:04 PM
hey space.
i can change the if to a while loop... that should take care of that. or ill just add the else... but i dont think that is giving me a seg fault... ill try tho.
I still havent written the code for OUTPUT yet... thats what i needed help on. from what i can tell, the structs are being filled... but yet... SOMETHING IS WRONG.
The_Preacher
Feb 9 2004, 10:08 PM
temp1 = temp1->next = NULL;
You sure this bit (and its friend) are right?
Purple Peace Pipe
Feb 9 2004, 10:08 PM
hey blud.. i dunno whats causing the problem but i find the best way to find these things is to use a debugger to step through the code and then you can see where the error strikes and then you can work out why its happening.
skinny d
Feb 9 2004, 10:11 PM
| QUOTE ([]D [] []v[] []D @ Feb 9 2004, 05:08 PM) |
temp1 = temp1->next = NULL;
You sure this bit (and its friend) are right? |
no im not really sure of any of it.

im asking u guys so if u had some free time, u could maybe rewrite it, or give suggestions. im terrible at this shite.
The_Preacher
Feb 9 2004, 10:12 PM
make
temp1 = temp1->next = NULL;
into
temp1->next = NULL;
that sort it?
and the temp2=blarbalr as well
The_Preacher
Feb 9 2004, 10:13 PM
teehee, my g33k says u r a very messy worker
The_Preacher
Feb 9 2004, 10:17 PM
soz mate i gotta go play games b4 sleep. Ill get the geek to have another soberer look tomorrow if that dont sort it.
skinny d
Feb 9 2004, 10:23 PM
!!! MADE PROGRESS.... made those changes...
then after fillstruct1 call, i stuck a "cout << temp1->name;"
in there and it outputted "Damien"
now what?
space is deep
Feb 9 2004, 10:58 PM
| QUOTE (skinny d @ Feb 9 2004, 11:23 PM) |
!!! MADE PROGRESS.... made those changes... then after fillstruct1 call, i stuck a "cout << temp1->name;" in there and it outputted "Damien"
now what?  |
| CODE |
for each item on list1 { set flag to true for each item on list2 { if item1 == item2 { set flag to false break } }
print item1 if flag == true }
|
then do the same again with the lists the other way round.
Whoever made you use c++ for this is cruel.
skinny d
Feb 10 2004, 12:09 AM
those are bool statements and i dont know how to use those
space is deep
Feb 10 2004, 10:20 PM
| QUOTE (skinny d @ Feb 10 2004, 01:09 AM) |
those are bool statements and i dont know how to use those  |
Experiment!
| CODE |
#include <iostream> #include <string>
using namespace std;
void print_bool(bool b);
int main() { bool b = true; print_bool(b);
b = false; print_bool(b); }
void print_bool(bool b) { if (b) { cout << "b is true" << endl; } else { cout << "b is false" << endl; } }
|
skinny d
Feb 11 2004, 02:34 AM
could u explain that?
mickle
Feb 11 2004, 05:55 AM
Skinny d have you considered processing the data some other way?
I'm not sure why you are using C++ to do it if you dont understand it
Or is it homework
skinny d
Feb 11 2004, 06:42 AM
| QUOTE (mickle @ Feb 11 2004, 12:55 AM) |
Or is it homework |
u guessed it

programming class
mickle
Feb 11 2004, 04:14 PM
Ask your teacher then!
skinny d
Feb 11 2004, 07:10 PM
he wont help
The_Preacher
Feb 11 2004, 07:25 PM
What!!!?!?!?!??!?!!! Go to his supervisor and tell him your teacher isnt giving you help, your teacher will shitabrick

Unless he has some good reason not to tell u???
skinny d
Feb 11 2004, 07:43 PM
doesnt matter when the teacher has 10+ years.
DaLata
Feb 11 2004, 10:05 PM
You're going to fail......
your indentation is non existent and you haven't commented your code

sorry, that was FA help whatsoever...
But I do teach programming..not C++ though

I'll find a motorway......
skinny d
Feb 11 2004, 11:50 PM
| QUOTE (DaLata @ Feb 11 2004, 05:05 PM) |
your indentation is non existent and you haven't commented your code |
i indent very nicely. just didnt show up when i pasted it
mickle
Feb 12 2004, 06:15 AM
| QUOTE (skinny d @ Feb 11 2004, 08:10 PM) |
he wont help |
So the teacher gave you some homework without teaching you the principles you need to know to complete it?
Or did you goof off and are now wishing you'd paid attention?
I guessed it was homework when you hopefully asked if someone could do it all for you.
skinny d
Feb 12 2004, 06:42 AM
well, maybe if you knew some of the professors around here you would understand. really often sad excuses for human beings. not even the book was any help. the ONLY ppl who got the assignment done were the kids who were retaking the class again. i was just asking for a little help
mickle
Feb 12 2004, 07:25 AM
| QUOTE (skinny d @ Feb 12 2004, 07:42 AM) |
| well, maybe if you knew some of the professors around here you would understand |
Professors eh? I learned everything I know about computers either off the internet or by practical experimentation. Of course.. I have gaping gaps in my knowledge probably

I don't know C++ though. I would like to learn but I don't have time at the moment. This thread has almost tempted me since I understand the code to a small degree and managed to compile some (big whoop!).

Can you not find something on the internet to help? I've had a quick look and there are quite a lot of resources out there for c++ such as
http://www.planet-source-code.com/ or maybe you can find someone who has allready done the same homework.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please
click here.