0

当我添加一个人时,它会正确添加,但是当我去删除名称时,它说找不到该名称。该程序允许用户创建电话簿并编辑电话簿中的信息

#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();
}
4

3 回答 3

2

看起来您在找到匹配项后忘记在删除中“中断”。

于 2011-04-14T04:07:58.343 回答
1

我觉得你用strcmp错了。0如果两个字符串都匹配,它会返回,所以你需要strcmp() == 0. 在这里查看可能的返回值。
接下来,既然你已经在使用std::strings了,直接比较一下,他们支持它:

if(Contact == ContactNumber[Counter].NameEntry)

此外,在找到要删除的条目后,您不会跳出 for 循环。


另一个建议:您的电话簿可以包含重复的条目,因为您没有检查此人是否已经存在于您的AddEntry函数中。
最后,在您经历了维护基本上是从一个字符串到另一个字符串的映射的所有这些痛苦之后,为了进一步使用这种数据结构,请考虑使用std::map. :)

#include <map>

int main(){
  map<string /*name*/, string /*number*/> phonebook;
  phonebook["Meyers"] = "03024233";
  string number = phonebook.find("Meyers");
}
于 2011-04-14T04:18:41.637 回答
0
contact ContactNumber [GROUPSIZE];

对于当前的实现,我认为它不会从对象数组中删除条目。您有GROUPSIZE许多contact对象(即对象数组)。数组的大小不能修改。如果你需要从数组中移除一个元素,首先你需要有一个 type 的引用contact

contact *ContactNumber ;  // new it with GROUPSIZE number of objects in PhoneBook constructor.

如果您需要删除某个m位置的元素,则需要GROUPSIZE - m使用复制交换规则进行翻译。即,m+1位置对象应该m就位,m+2位置对象应该m+1就位,....是正确的实现方式,我认为。并记住要delete[] ContactNumber;在析构函数中PhoneBook避免内存泄漏。

为了避免所有痛苦,请改用a std::vector

于 2011-04-14T04:18:53.157 回答