【发布时间】:2015-07-24 04:23:06
【问题描述】:
我今天大部分时间都在努力寻找我的代码中的错误:
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include "Header.h"
using namespace std;
void fileConnect(BST &Book)
{
std::fstream inFile;
inFile.open("inFile.txt", std::ios::in);
if (!inFile)
{
std::cout << "Error opening input file!\n";
exit(101);
}
else
{
std::cout << "\nInput file found.\n\n";
do
{
Node* tempHold;
tempHold = new Node;
std::getline(inFile, tempHold->first, '$');
std::getline(inFile, tempHold->last, '$');
std::getline(inFile, tempHold->phone);
tempHold->key = toupper(tempHold->last[0]);
Book.insertBST(tempHold);
} while (!inFile.eof());
}
inFile.close();
}
void menuDisplay(BST *Book)
{
int selection = 0;
bool valid = true, wiped = false;
std::cout << "This Phonebook will allow you to add or delete entries, search for a specific\nentry, list all entries, or delete all entries." << std::endl;
std::cout << std::endl << "To begin, please select an option from the menu below: " << std::endl;
std::cout << "1) Search by last name." << std::endl;
std::cout << "2) Delete an entry from the phonebook." << std::endl;
std::cout << "3) Add an entry to the phonebook." << std::endl;
std::cout << "4) Print all entries in the phonebook." << std::endl;
std::cout << "5) Delete all entries in the phonebook." << std::endl;
std::cout << "6) Exit Program." << std::endl;
do
{
std::cout << "\nChoose an option: ";
std::cin >> selection;
if (selection < 1 || selection >= 7)
{
std::cout << "\nERROR: Please enter a valid choice between 1 and 7." << std::endl;
valid = false;
}
else
{
valid = true;
std::cout << std::endl;
//Search
if(selection == 1)
{
std::string term;
cout << "Please enter the last name of the person you want to search for:" << endl;
cin >> term;
Book->pubSearch(term);
}
//Delete individual
if(selection == 2)
{
std::string dltString;
cout << "Please enter the last name of the individual you would like to delete:" << endl;
cin >> dltString;
int dltKey = dltString[0];
Book->deleteNode(dltKey);
}
//Add individual
if(selection == 3)
{
Node* tempHold;
tempHold = new Node;
std::string first, last, phone;
cout << "First name: ";
first.clear();
cin >> first;
cout << "Last name: ";
last.clear();
cin >> last;
cout << "Phone number: ";
phone.clear();
cin >> phone;
tempHold->key = toupper(tempHold->last[0]);
Book->insertBST(tempHold);
wiped = false;
}
//Print all
if(selection == 4)
{
if(wiped)
{
cout << "The phonebook was previously cleared, please add more entries or exit." << endl;
}
else
{
Book->printTree();
}
}
//Delete all
if(selection == 5)
{
std::string confirm;
std::cout << "Are you sure you want to delete all entries? Y/N" << endl;
cin >> confirm;
if(confirm[0] == 'Y' || confirm[0] == 'y')
{
cout << "Deleting data..." << endl;
Book->pubDestroy();
wiped = true;
}
else
{
cout << "Operation canceled." << endl;
}
}
//Exit
if(selection == 6)
{
std::cout << "Program will now exit, goodbye!" << std::endl;
exit(100);
}
}
} while (valid);
return;
}
int main()
{
///<Local Declarations and initializations of BST
BST Book;
int dltKey = 0, newKey = 0;
fileConnect(Book);
menuDisplay(&Book);
return 0;
}
This is a live demo 所以更容易从输出中看出我的意思。
我知道这是一个指针错误,但我不知道在哪里。当我在 VS2013 中运行它时,它只是挂起并崩溃,然后才能在程序中完成任何操作。在 Linux 中运行时,它会运行,但对我的数据的任何修改都会破坏其余的数据。在初始文件读入后,我也无法向树中添加任何新节点。
如果我不得不猜测它与我将类传递给我的函数的方式有关,但我对类/对象作为函数参数自动处理的方式不够熟悉。
我已经尝试从头开始重写此代码至少 3 次,每次都返回奇怪的指针错误。这太让人抓狂了,我只想看看这件事能奏效。
提前感谢所有帮助。
编辑:另外,我不知道这是否有助于澄清我的数据在非根删除时被丢弃的问题,并且使用手动添加功能根本无法链接到树。
【问题讨论】:
-
你将新创建的节点的第一个、最后一个、电话和密钥复制到哪里?
-
您的意思是手动添加吗?帖子中的链接具有我最初读取的数据文件,否则选择“添加”时,用户将仅键入数据,然后将其读取和复制为字符串。
-
嘿,这就是我要问的。从键盘读取的字符串复制到哪里?请告诉我函数或文件名
-
函数在
Impl.cpp文件中,名为insertBST。当我使用调试器跟踪它时,尽管设置正确,它仍会遵循错误的逻辑。例如:walker是一个 Node* 类型,应该注册为 null 并导致循环在插入期间终止,但是当它变为 null 时,循环继续迭代,直到出现错误的 null 并且不执行任何放置逻辑。
标签: c++ pointers binary-search-tree