2016-05-09 11 views
-1

私はPersonというクラスを含むクラスAddressBookを作ることになっています。私のプログラムはほとんど、動作します。人を追加する場合を除いて、コマンドメニューの次の繰り返しでそれを覚えておらず、「すべて表示」が表示されます。私のコードで何が間違っていますか?入力したエントリを保存しないプログラムですか? (C++)

この

AddressBook Address_Book; 

は、新しいを作成します。

#include <iostream> 
#include <string> 

using namespace std; 

class AddressBook { 

public: 
    class Person 
    { 
    public: 
     char firstName[15]; 
     char lastName[15]; 
     char personID[15]; 
    }; 

    Person entries[100]; 

    unsigned int total; 

    AddressBook() 
    { 
     total = 0; 
    } 

    void AddPerson() 
    { 
     cout << "This is entry number " << (total + 1) << " in your address book. " << endl; 

     cout << "What shall we put for the first and last name? Limit both to under 15 characters. Example: Bob Smith" << endl; 
     cin >> entries[total].firstName >> entries[total].lastName; 

     cout << "What is " << entries[total].firstName << " " << entries[total].lastName << "'s ID code?" << endl; 
     cin >> entries[total].personID; 

     ++total; 
     cout << "..." << endl << "Successfully Added." << endl; 
    }; 

    void DisplayPerson(int i) 
    { 
     cout << "Entry " << i + 1 << ": " << endl; 
     cout << "FIRST NAME: " << entries[i].firstName << endl; 
     cout << "LAST NAME: " << entries[i].lastName << endl; 
     cout << "ID: " << entries[i].personID << endl; 
    }; 

    void DisplayEveryone() 
    { 
     cout << "You have " << total << " People in your address book." << endl; 

     for (int i = 0; i < total; ++i) 
      DisplayPerson(i); 
    }; 


    void SearchPerson() 
    { 
     char lastname[32]; 
     cout << "Please enter the last name of the person you wish to find." << endl; 
     cin >> lastname; 

     for (int i = 0; i < total; ++i) 
     { 
      if (strcmp(lastname, entries[i].lastName) == 0) 
      { 
       cout << "Person Found. " << endl; 
       DisplayPerson(i); 
       cout << endl; 
      } 
     } 
    }; 




}; 


int main() { 
    char command; 
    bool Exit = false; 

    while (Exit == false) 
    { 
     AddressBook Address_Book; 
     cout << "---------------COMMANDS---------------" << endl; 
     cout << "A: Add Person To Address Book" << endl; 
     cout << "S: Search for Person in Address Book" << endl; 
     cout << "D: Display Everyone In Address Book" << endl << endl; 

     cout << "Type the letter of your command: "; 
     cin >> command; 
     cout << endl; 
     switch (command) { 
     case 'A': 
     case 'a': 
      Address_Book.AddPerson(); 
      break; 
     case 'S': 
     case 's': 
      Address_Book.SearchPerson(); 
      break; 
     case 'D': 
     case 'd': 
      Address_Book.DisplayEveryone(); 
      break; 
     default: 
      cout << "That is not a valid command. Closing Address Book." << endl; 

      cout << endl; 
     } 
    } 
} 
+0

[MCVE]を提供してください。 –

+0

OPの質問 - 100以上のエントリがあるとどうなりますか? – PaulMcKenzie

答えて

2

理由は、whileループの各反復で新しいアドレス帳を作成し、イテレーションの終わりにそれを捨てるということですあなたが範囲の終わりに達すると(つまり、ループの終わりに) "放棄"されるアドレス帳。

実際には、新しいエントリを作成するときはいつでも新しいアドレス帳を購入しますか?いいえ、最初に本を購入してから(おそらくwhileループで)エントリを追加します。上記の行をループの外側に移動します。

+0

詳しくはhttps://en.wikipedia.org/wiki/Scope_(computer_science)をご覧ください。 – user4581301

1

問題はあなたのアドレス帳の宣言にあります。次へ

変更を:

AddressBook Address_Book; 

while (Exit == false) { 
    //Ask for input and respond. 
} 

バージョンAddress_Bookではwhileループの開始時に宣言されています。これは、ループの反復が完了し、実行がブロックの開始に戻るたびに、以前のオブジェクトデータを知らない新しいローカルAddress_Bookオブジェクトが作成されることを意味します。

関連する問題