Searching by Last Name

Hi everyone. I have an assignment to create an address book in c++ where you can enter contact information and then search all entries via last name. I am trouble figuring out how to write a function that will be able to search an entry by last name. Any help would be appreciated.Here is my code so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <iostream>
using namespace::std;

class addBook {
   public:                
      addBook() ; 
      void mainMenu();     
      void addEntry() ;    
   //   void searchEntry ;   
   //   void deleteEntry ;   
    //  void DisplayData ; 

   private:               
      int recordID;
      char firstName[ 15 ] ; 
      char lastName[ 15 ] ;  
      char birthday[ 15 ] ;
      char phone[ 15 ] ;   
      char email[ 15 ] ;  
};
int  mainMenu();
void addEntry();
void searchEntry();
void displayAll();

void displayAll(){
}
void searchEntry (){
}

void addEntry (){
     cout << "Enter First Name: ";
     char firstName [15];
     cin >> firstName;
     cout << "Enter Last Name: ";
     char lastName [15];
     cin >> lastName;
     cout << "Enter Date of Birth: ";
     char birthday [15];
     cin >> birthday;
     cout << "Enter Phone Number: ";
     char phone [15];
     cin >> phone;
     cout << "Enter Email: ";
     char email [15];
     cin >> email;
     mainMenu();
}
int  mainMenu (){
      cout << "+-----------------------------------------+" << endl;
      cout << "|   Address Book Menu                     |" << endl;
      cout << "|                                         |" << endl;
      cout << "| 1- Add an entry                         |" << endl;
      cout << "| 2- Search an entry by last name         |" << endl;
      cout << "| 3- Display all entries                  |" << endl;
      cout << "| 4- Exit                                 |" << endl;
      cout << "|                                         |" << endl;
      cout << "+-----------------------------------------+" << endl;

          cout << " " << endl;
          cout << "Please enter a number for one of the above options." << endl;
	  int num;
	  cin >> num;
            if (num == 1){
		addEntry ();
		}
	    else if (num == 2){
		searchEntry ();
		}
	    else if (num == 3){
		displayAll ();
		}
	    else if (num == 4){
		return 0;
		}
     
}


int main (){
	mainMenu ();
	return 0;
}
This should let you get started :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <iostream>
#include <stdio.h>
#include <conio.h>

using namespace std;

class addBook
{
   public : 
   addBook() // constructor
   {
      count = 0;
   }

   void addEntry();
   void displayAll();

   // Display a specific entry

   void displayEntry(int i);
   void searchEntry();

   // You should wrap all members into a struct

   struct entry_struct
   {
         char firstName[ 15 ] ; 
         char lastName[ 15 ] ;  
         char birthday[ 15 ] ;
         char phone[ 15 ] ;   
         char email[ 15 ] ;  
   };

   // An array is required if you want multiple entries

   entry_struct entries[100];
   unsigned int count;
};


void addBook::addEntry()
{
     cout << "Entry number " << (count + 1) << " : " << endl;
     
     cout << "Enter First Name: ";
     cin >> entries[count].firstName;

     cout << "Enter Last Name: ";
     cin >> entries[count].lastName;

     cout << "Enter Date of Birth: ";
     cin >> entries[count].birthday;

     cout << "Enter Phone Number: ";
     cin >> entries[count].phone;

     cout << "Enter Email: ";
     cin >> entries[count].email;
     
     ++count; // add an entry
}


void addBook::displayEntry(int i)
{
	cout << "Entry[" << i + 1 << "] : " << endl;
	cout << "First name : " << entries[i].firstName << endl;
	cout << "Last name : " << entries[i].lastName << endl;
	cout << "Date of birth : " << entries[i].birthday << endl;
	cout << "Phone number : " << entries[i].phone << endl;
	cout << "Email: " << entries[i].email << endl;
}


void addBook::displayAll()
{
	cout << "Number of entries : " << count << endl;
	for(int i = 0;i < count;++i)displayEntry(i);	
}

void addBook::searchEntry()
{
   char lastname[32];
   cout << "Enter last name : ";
   cin >> lastname;

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


// Your class
addBook my_book;

int mainMenu()
{
   int num;
   bool bQuit = false;

   // Put all your code into a while loop.
   while(bQuit == false)
   {
      system("cls"); // clear screen

      cout << "+-----------------------------------------+" << endl;
      cout << "|   Address Book Menu                     |" << endl;
      cout << "|                                         |" << endl;
      cout << "| 1- Add an entry                         |" << endl;
      cout << "| 2- Search an entry by last name         |" << endl;
      cout << "| 3- Display all entries                  |" << endl;
      cout << "| 4- Exit                                 |" << endl;
      cout << "|                                         |" << endl;
      cout << "+-----------------------------------------+" << endl;

      cout << " " << endl;
      cout << "Please enter a number for one of the above options." << endl;
      cout << "Your choice : ";
      cin >> num;
      cout << endl;


      if (num == 1)my_book.addEntry();
      else if (num == 2)my_book.searchEntry();
      else if (num == 3)my_book.displayAll();
      else if (num == 4)bQuit = true;
      else cout << "Invalid choice. Please try again" << endl;

      cout << endl;
      system("pause");
   }

   return 0;
}

int main (){
	mainMenu();
	return 0;
}
Last edited on
Topic archived. No new replies allowed.