Help - Search - Members - Calendar
Full Version: C++ Help Needed Bad!
UK420 > Cannabis Culture > Smokers Lounge > Computers, Games and Internet
skinny d
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
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
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
pimp, im really an amateur c++ programmer and that helped explain the problem, but i dont know how to fix it sad.gif
The_Preacher
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
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 cry.gif
The_Preacher
The g33k wants to know how far it gets?
skinny d
it compiles but anytime i try to output ANYTHING, i get a seg fault.
The_Preacher
is that right?

does "Reading file one...complete." never print when you run your program?
skinny d
that prints fine... i mean.. when i add more code to output headptr1 or temp1, i get a seg fault
space is deep
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)? whistling.gif

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. excl.gif

HTH geek.gif
skinny d
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. pinch.gif
The_Preacher

temp1 = temp1->next = NULL;

You sure this bit (and its friend) are right?
Purple Peace Pipe
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. guitar.gif
skinny d
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. lol.gif im asking u guys so if u had some free time, u could maybe rewrite it, or give suggestions. im terrible at this shite. 34.gif
The_Preacher
make

temp1 = temp1->next = NULL;

into

temp1->next = NULL;


that sort it?

and the temp2=blarbalr as well
The_Preacher
teehee, my g33k says u r a very messy worker whistling.gif
The_Preacher
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
!!! MADE PROGRESS.... made those changes...
then after fillstruct1 call, i stuck a "cout << temp1->name;"
in there and it outputted "Damien"


now what? unsure.gif
space is deep
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? unsure.gif


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. no.gif
skinny d
those are bool statements and i dont know how to use those sad.gif
space is deep
QUOTE (skinny d @ Feb 10 2004, 01:09 AM)
those are bool statements and i dont know how to use those sad.gif

Experiment! artist.gif

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
could u explain that? stoned.gif
mickle
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 unsure.gif

Or is it homework wink.gif
skinny d
QUOTE (mickle @ Feb 11 2004, 12:55 AM)
Or is it homework wink.gif

u guessed it wink.gif programming class 34.gif
mickle
Ask your teacher then! disguise2.gif
skinny d
he wont help sad.gif
The_Preacher
What!!!?!?!?!??!?!!! Go to his supervisor and tell him your teacher isnt giving you help, your teacher will shitabrick smile.gif


Unless he has some good reason not to tell u???
skinny d
doesnt matter when the teacher has 10+ years.
DaLata
You're going to fail......

your indentation is non existent and you haven't commented your code lol.gif

sorry, that was FA help whatsoever... 20.gif

But I do teach programming..not C++ though tongue.gif

I'll find a motorway......
skinny d
QUOTE (DaLata @ Feb 11 2004, 05:05 PM)
your indentation is non existent and you haven't commented your code lol.gif

i indent very nicely. just didnt show up when i pasted it wink.gif
mickle
QUOTE (skinny d @ Feb 11 2004, 08:10 PM)
he wont help sad.gif

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
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 cry.gif
mickle
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 smile.gif

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!). lol.gif

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. whistling.gif
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2010 Invision Power Services, Inc.