我一直在努力寻找今天更好的部分来查找我的代码中的错误:
#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次,并且每次它都会回到奇数指针错误。它令人发狂,我只是想看到这件事有用。
提前感谢您的所有帮助。
编辑:另外,我不知道它是否有助于澄清我的数据在非root删除时被删除的问题,并且使用手动添加功能根本不会链接到树。
答案 0 :(得分:2)
我认为当从选项3手动插入时,您错过了复制用户键入的第一个,最后一个和电话。所以请修改menuDisplay()
函数,如下所示:
//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;
//You missed out these lines:
tempHold->first= first;
tempHold->last=last;
tempHold->phone= phone;
tempHold->key = toupper(tempHold->last[0]);
Book->insertBST(tempHold);
wiped = false;
}
希望有所帮助