我不能让我的删除功能从列表中删除项目
问题描述:
当我添加一个人它会被正确添加,但是当我去删除它的名称说没有找到该名称。该程序允许用户在电话簿中创建的电话簿和编辑信息对上述程序我不能让我的删除功能从列表中删除项目
#include <iostream>
#include <string>
#include "PhoneBook.h"
using namespace std;
char MenuSelection();
int main() {
char Selection;
PhoneBook myList;
do{
Selection = MenuSelection();
switch (Selection){
case 'a':
myList.AddEntry();
break;
case 'd':
myList.DisplayNamesAndNumbers();
break;
case 's':
myList.FindEntry();
break;
case 'r':
myList.DeleteFunction();
break;
case 'q':
break;
default :
cout << "\n\nNot a command choice\n";
cout << "Press enter to continue";
cin.get();
cin.get();
system ("clear");
}
}while (Selection != 'q');
myList.MakeFile();
cout << "Press enter to continue";
return 0;
}
//This function prints out the opening menu and allws users to enter a command.
char MenuSelection(){
char Response;
cout << "\n MENU\n";
cout << "a - add a name and phone number\n";
cout << "d - display names and phone number\n";
cout << "r - remove a name and phone number\n";
cout << "s - search for a name and return the phone number\n";
cout << "q - quit program\n\n";
cout << "Enter your choice: ";
cin >> Response;
return Response;
}
头文件这是类,其允许用户操纵的电话簿。
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const int GROUPSIZE = 100;
struct contact {string NameEntry; string PhoneNumber;};
class PhoneBook {
private:
int Size;
contact ContactNumber [GROUPSIZE];
public:
PhoneBook();
void MakeFile();
void AddEntry();
void FindEntry();
string DisplayNamesAndNumbers();
string DeleteFunction();
};
//opens the file and adds names and numbers to the text file
PhoneBook::PhoneBook(){
string PersonsName, PhoneNum;
int i = 0;
ifstream infile;
infile.open ("phonebook.txt");
if (!infile){
cout << "File does not exist";
}
else{
while (!infile.eof()){
infile >> PersonsName >> PhoneNum;
ContactNumber [i].NameEntry = PersonsName;
ContactNumber [i++].PhoneNumber = PhoneNum;
}
Size = i;
infile.close();
}
}
//adds name and phone number to the list
void PhoneBook::AddEntry(){
string PersonsName, PhoneNum;
cout << "\n\nEnter the name to be added: ";
cin >> PersonsName;
cout << "Enter the phone number for " << PersonsName << ": ";
cin >> PhoneNum;
ContactNumber [Size].NameEntry = PersonsName;
ContactNumber [Size].PhoneNumber = PhoneNum;
Size++;
cout << "Press enter to continue";
cin.get();
cin.get();
system ("clear");
}
//finds names and numbers in the list
void PhoneBook::FindEntry(){
int Location, Counter;
string Contact;
cout << "\n\nName to find: ";
cin >> Contact;
Counter = 0;
while (Counter < Size){
Counter++;
//for (int i = 0; i < size; i++){
if (strcmp(Contact.c_str(),ContactNumber [Counter].NameEntry.c_str()))
Location = Counter;
else
Location = -1;
}
if (Location != -1)
cout << "The phone number for " << Contact << " is " << ContactNumber [Location].PhoneNumber << endl;
else
cout << Contact << " not in phonebook\n";
cout << "Press enter to continue";
cin.get();
cin.get();
system ("clear");
}
//displays all information in the list
string PhoneBook::DisplayNamesAndNumbers(){
string PersonsName, PhoneNum;
int Check = 1;
cout << "\n\nList is being sorted\n";
while (Check == 1){
cout << "Name\t\tTelephone Number";
for (int i = 0; i < Size; i++){
cout << ContactNumber[i].NameEntry << "\t\t" << ContactNumber[i].PhoneNumber << "\n";
}
break;
}
cout << "Press enter to continue";
cin.get();
cin.get();
system("clear");
return "";
}
//deletes information from the list
string PhoneBook::DeleteFunction(){
char Answer;
string PersonsName;
int Location;
cout << "\n\nName to remove: ";
cin >> PersonsName;
for (int i = 0; i < GROUPSIZE; i++){
if (!strcmp(PersonsName.c_str(),ContactNumber[i].NameEntry.c_str()))
Location = i;
else
Location = -1;
}
if (Location != -1){
ContactNumber[Location].NameEntry = ContactNumber[Size].NameEntry;
ContactNumber[Location].PhoneNumber = ContactNumber[Size].PhoneNumber;
cout << PersonsName <<" removed from phonebook\n";
cout << "Press enter to continue";
cin.get();
cin.get();
system("clear");
return"";
}
cout << "Name not found in phonebook\n";
cout << "Press enter to continue";
cin.get();
cin.get();
system ("clear");
return"";
}
//closes the file at the end of the program
void PhoneBook::MakeFile(){
ofstream outfile;
outfile.open("phonebook.txt");
for (int i = 0; i < GROUPSIZE; i++){
outfile << ContactNumber[i].NameEntry << " " << ContactNumber[i].PhoneNumber<<"\n";
}
outfile.close();
}
答
看起来你忘了在找到匹配项后删除你的删除。
答
我认为你使用的是strcmp
错误。如果两个字符串匹配,它将返回0
,因此您需要strcmp() == 0
。看看here为可能的返回值。
接下来,因为你已经在使用std::string
S,只需直接比较这些,他们支持:
if(Contact == ContactNumber[Counter].NameEntry)
此外,你还没有找到入口后,破坏你的for循环的出要删除。
另一个侧面提醒:您的电话簿可以包含重复的条目,因为你不检查,如果这个人在你AddEntry
功能已经exsists。
最后,在经历了所有这些基本上从一个字符串映射到另一个字符串的痛苦之后,为了进一步使用这样的数据结构,请考虑使用std::map
。 :)
#include <map>
int main(){
map<string /*name*/, string /*number*/> phonebook;
phonebook["Meyers"] = "03024233";
string number = phonebook.find("Meyers");
}
答
contact ContactNumber [GROUPSIZE];
当前实现,我认为它不会删除对象的数组中的条目。你有GROUPSIZE
号码的contact
对象(即一组对象)。数组无法修改大小。如果你需要从数组中删除一个元素,首先你需要一个contact
类型的引用。
contact *ContactNumber ; // new it with GROUPSIZE number of objects in PhoneBook constructor.
如果您需要在m
位置删除元素,你需要使用复制交换规则GROUPSIZE - m
翻译。即m+1
位置对象应该位于m
位置,m+2
位置对象应位于m+1
位置,....是我认为正确的执行方式。并且请记住在PhoneBook
的析构函数中使用delete[] ContactNumber;
以避免内存泄漏。
为了避免所有的痛苦,改用std::vector
代替。
哇,代码太多了。而且格式不好引导。你能分辨出让你困惑的部分吗? – 2011-04-14 03:14:50
string PhoneBook :: DeleteFunction(){ char答案; string PersonsName; int位置; cout > PersonsName;如果(!strcmp(PersonsName.c_str(),ContactNumber [i] .NameEntry)为(012)。c_str())) Location = i; else Location = -1; } – NewProgrammer22 2011-04-14 04:21:07
if(Location!= -1)ContactNumber [Location] .NameEntry = ContactNumber [Size] .NameEntry; ContactNumber [Location] .PhoneNumber = ContactNumber [Size] .PhoneNumber; cout NewProgrammer22 2011-04-14 04:21:49