0
#include <iostream>
#include <cstring>
#include <fstream>
using namespace std;

Standard #includes, prototypes are defined.

struct housingNode * readIn(housingNode * name); 
void displayAll(housingNode * name);  

I think the structure is built correctly;

struct housingNode
{
  int index; 
  int rent;
  int size;
  int room;
  char currentLocation[101];
  char tFeat1[101];
  char tFeat2[101];
  char tFeat3[101];
  char * location;
  char * Feat1;
  char * Feat2;
  char * Feat3;
  housingNode * next;
};


int main()
{   
  housingNode * head;
  head = NULL;
  readIn(head);
  return 0; 
}

Now, I know it has to be in one of these two functions, and since the second one isn't ever called yet, I'm betting it's this one, but I can't find any where to cuase my pointers to be null, or to reference any of them in that way.

struct housingNode* readIn(housingNode * name)
{
  housingNode * current;
  housingNode * temp;
  housingNode * head;   

  head = current;
  temp = current; 


  //file with housing information
  ifstream file_in;
  file_in.open("housingFile.txt");

  for(int i = 0; i < 12; ++i)
  { 

    //starts the building process for the LLL
    current = new housingNode;

    //reads in the information for the LLL
    file_in >> current -> rent;
    file_in.ignore(100,';');
    file_in >> current -> size;
    file_in.ignore(100,';');
    file_in >> current -> room;
    file_in.ignore(100,';');
    file_in.get(current -> currentLocation,100,';');
    file_in.ignore(100,';');

    //Allocates the needed memory for storing the information into a LLL. 
    current -> location = new char[strlen(current -> currentLocation)+1]; 
    strcpy(current -> location, current -> currentLocation);        
    file_in.get(current -> tFeat1,100,';');
    file_in.ignore(100,';');
    current -> Feat1 = new char[strlen(current -> tFeat1)+1];
    strcpy(current -> Feat1, current -> tFeat1);
    file_in.get(current -> tFeat2,100,';');
    file_in.ignore(100,';');
    current -> Feat2 = new char[strlen(current -> tFeat2)+1];
    strcpy(current -> Feat2, current -> tFeat2);
    file_in.get(current -> tFeat3,100,';');
    file_in.ignore(100,'\n');
    current -> Feat3 = new char[strlen(current -> tFeat3)+1];
    strcpy(current -> Feat3, current -> tFeat3);

    temp -> next = current;
    temp = temp -> next; 
  }

  //resets the node to null
  current -> next = NULL;

  //Closes file after reading in information.
  file_in.close();

  //returns the head pointer
  return head;
}   

void  displayAll(housingNode * name)
{

  housingNode * traverse;
  housingNode * head;
  traverse = head;

  cout << "Listing housing information: " << '\n';

  for(int i = 0; i < 12; ++i)
  { 
    cout <<"Cost" << traverse -> rent <<'\n';
    cout <<"Size: " << traverse -> size <<'\n';
    cout <<"Number of Rooms: " << traverse -> room <<'\n';
    cout <<"Location: " << traverse -> currentLocation <<'\n';
    cout << "Features: " << '\n';
    cout <<"#1: " << traverse -> Feat1 <<'\n';
    cout <<"#2: " << traverse -> Feat2 <<'\n';
    cout <<"#3: " << traverse -> Feat3 << endl; 

    traverse = traverse -> next;
    //Full completed function for displaying all. 
  }
}
5
  • 1
    It's hard to diagnose this kind of problem, just use a debugger to help you. Commented Mar 21, 2014 at 8:17
  • 3
    You don't need to dereference a null pointer to get a segmentation fault. You could simply be dereferencing a pointer to already deleted (freed memory), reading/writing beyond the bounds of an array/C-string... Other than that: what Yu Hao said. Commented Mar 21, 2014 at 8:20
  • You should read ericlippert.com/2014/03/05/how-to-debug-small-programs Commented Mar 21, 2014 at 8:21
  • 2
    If you are going to be writing code like that you should learn to use a debugger. Commented Mar 21, 2014 at 8:23
  • Another one trying to find a problem to a solution. Why are you not using std::string instead of char pointers and C coding? And no, using std::string does not take away from your requirements of coding your own linked list class. You're leaking memory all over the place by going overboard with the new char [] stuff. Commented Mar 21, 2014 at 9:47

2 Answers 2

2

One problem is here:

struct housingNode* readIn(housingNode * name)
{
housingNode * current;
housingNode * temp;
housingNode * head; 

head = current;  // <-----

current is not initialized here, later on you use temp that is also not correctly initialized

Sign up to request clarification or add additional context in comments.

Comments

1

Here is one of the problems (in the function):

housingNode * current;  // and you do not initialize it
housingNode * temp;
housingNode * head; 

I think you want to use name instead of current ...

but you also should initialize that before calling the function

head = current;
temp = current; 

current = new housingNode; // here the old value went away
...
temp -> next = current; // but tmp still points to an invalid address

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.