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];

現在の実装では、オブジェクトの配列からエントリが削除されることはないと思います。オブジェクトのGROUPSIZEcontact(つまり、オブジェクトの配列)があります。配列のサイズは変更できません。配列から要素を削除する必要がある場合は、最初に型の参照が必要ですcontact

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

m位置にある要素を削除する必要がある場合は、GROUPSIZE - mコピースワップルールを使用して変換を行う必要があります。つまり、m+1位置オブジェクトは所定の位置にある必要がありmm+2位置オブジェクトは所定の位置にある必要がm+1あります。....は正しい実装方法だと思います。そして、メモリリークを回避するためdelete[] ContactNumber;にのデストラクタで忘れないでください。PhoneBook

すべての苦痛を防ぐために、std::vector代わりに使用してください。

于 2011-04-14T04:18:53.157 に答える