#include <iostream>
#include <cstring>
#include <fstream>
using namespace std;
标准#includes,定义了原型。
struct housingNode * readIn(housingNode * name);
void displayAll(housingNode * name);
我认为结构是正确构建的;
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;
}
现在,我知道它必须属于这两个功能中的一个,并且由于第二个功能尚未被召唤,我打赌它是这个,但我可以&# 39;找到任何把我的指针设为null的地方,或以这种方式引用它们中的任何一个。
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.
}
}
答案 0 :(得分:2)
这里有一个问题:
struct housingNode* readIn(housingNode * name)
{
housingNode * current;
housingNode * temp;
housingNode * head;
head = current; // <-----
此处未初始化当前电流,稍后您使用的温度也未正确初始化
答案 1 :(得分:1)
以下是其中一个问题(在函数中):
housingNode * current; // and you do not initialize it
housingNode * temp;
housingNode * head;
我认为你想用name
代替当前......
但你也应该在调用函数
之前初始化它head = current;
temp = current;
current = new housingNode; // here the old value went away
...
temp -> next = current; // but tmp still points to an invalid address