Search function only works for the first object
I am having a hard time with my search function. It only finds the first created object. Any ideas where my problem is?
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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
|
#include <cstdlib>
#include <iostream>
#include <istream>
#include <string>
#include <iomanip>
#include "Contact.h"
#include <vector>
using namespace std;
void displayMenu();
void printContact(vector<Contact>&Addressbook);
void sortAscending(vector<Contact>&Addressbook);
void searchContact(vector<Contact>&Addressbook);
void getContactInfo (vector<Contact>&Addressbook);
void editContact(vector<Contact>&Addressbook);
void deleteContact(vector<Contact>&Addressbook);
/*
*
*/
int main(int argc, char** argv)
{//Declare variables
int menuChoice = 0;
vector<Contact>Addressbook;
//get menu choice
displayMenu();
cout << "Enter 1, 2, 3, 4, 5, or 6: ";
cin >> menuChoice;
while (menuChoice > 0 && menuChoice < 6)
{
//assign menu functions
switch (menuChoice)
{
case 1:
{
getContactInfo(Addressbook);
break;
}
case 2:
{
editContact(Addressbook);
break;
}
case 3:
{
deleteContact(Addressbook);
break;
}
case 4:
{
printContact(Addressbook);
break;
}
case 5:
{
searchContact(Addressbook);
break;
}
case 0:
{
break;
}
}
//get menu choice
displayMenu();
cout << "Enter 1, 2, 3, 4, 5, or 6: ";
cin >> menuChoice;
} //end while
//system("pause");
return 0;
//end of main function
}
//*****function definitions*****
void displayMenu()
{
cout << "1 Create Contact" << endl;
cout << "2 Edit Contact" << endl;
cout << "3 Delete Contact" << endl;
cout << "4 Display Contacts" << endl;
cout << "5 Search Contact" << endl;
cout << "6 Exit program" << endl;
} //end of displayMenu function
void getContactInfo (vector<Contact>&Addressbook)
{
string firstName;
string lastName;
string address;
string city;
string state;
long int zipCode;
string organization;
string jobTitle;
long int homePhoneNumber;
long int cellPhoneNumber;
long int workPhoneNumber;
string emailAddress;
string nickname;
string birthday;
string notes;
for (int index=0; index< 2; index++)
{
Contact contact;
cout << "Please enter the first name and press enter: ";
cin >>firstName;
cout << "Please enter the last name and press enter: ";
cin >> lastName;
cout << "Enter the address without spaces and press enter: ";
cin >> address;
cout << "Enter the city without spaces and press enter: ";
cin >> city;
cout << "Enter the state without spaces and press enter: ";
cin >> state;
cout << "Enter the zip code and press enter: ";
cin >> zipCode;
cout << "Enter the organization without spaces and press enter: ";
cin >> organization;
cout << "Enter the job title without spaces and press enter: ";
cin >> jobTitle;
cout << "Please enter the phone number without hyphens and press enter: ";
cin >> homePhoneNumber;
cout << "Enter the cell phone number without hyphens and press enter: ";
cin >> cellPhoneNumber;
cout << "Enter the work phone number without hyphens and press enter: ";
cin >> workPhoneNumber;
cout << "Enter the e-mail address and press enter: ";
cin >> emailAddress;
cout << "Enter the nickname without spaces and press enter: ";
cin >> nickname;
cout << "Enter the birthday without spaces and press enter: ";
cin >> birthday;
cout << "Enter the notes without spaces and press enter: ";
cin >> notes;
cout << endl;
contact.setFirstName(firstName);
contact.setLastName(lastName);
contact.setAddress(address);
contact.setCity(city);
contact.setState(state);
contact.setZipCode(zipCode);
contact.setOrganization(organization);
contact.setJobTitle(jobTitle);
contact.setHomePhoneNumber(homePhoneNumber);
contact.setCellPhoneNumber(cellPhoneNumber);
contact.setWorkPhoneNumber(workPhoneNumber);
contact.setEmailAddress(emailAddress);
contact.setNickname(nickname);
contact.setBirthday(birthday);
contact.setNotes(notes);
Addressbook.push_back(contact);
}
}
void printContact(vector<Contact>&Addressbook)
{
for (int index = 0; index < Addressbook.size(); index++)
{
cout << "First Name: " << Addressbook.at(index).getFirstName() << endl;
cout << "Last Name: " << Addressbook.at(index).getLastName() << endl;
cout << "Address: " << Addressbook.at(index).getAddress() << endl;
cout << "City: " << Addressbook.at(index).getCity() << endl;
cout << "State: " << Addressbook.at(index).getState() << endl;
cout << "Zip Code: " << Addressbook.at(index).getZipCode() << endl;
cout << "Organization: " << Addressbook.at(index).getOrganization() << endl;
cout << "Job Title: " << Addressbook.at(index).getJobTitle() << endl;
cout << "Home Phone Number: " << Addressbook.at(index).getHomePhoneNumber() << endl;
cout << "Cell Phone Number: " << Addressbook.at(index).getCellPhoneNumber() << endl;
cout << "Work Phone Number: " << Addressbook.at(index).getWorkPhoneNumber() << endl;
cout << "Email Address: " << Addressbook.at(index).getEmailAddress() << endl;
cout << "Nickname: " << Addressbook.at(index).getNickname() << endl;
cout << "Birthday: " << Addressbook.at(index).getBirthday() << endl;
cout << "Notes: " << Addressbook.at(index).getNotes() << endl;
cout << endl;
}
}
void searchContact(vector<Contact>&Addressbook)
{
bool found = false;
string userInput;
int index = 0;
cout << "Enter Name to Search" << endl;
cin >> userInput;
if (Addressbook.at(index).getFirstName() == userInput || Addressbook.at(index).getLastName() == userInput)
{
cout << Addressbook.at(index).getFirstName() << endl;
cout << Addressbook.at(index).getLastName() << endl;
cout << Addressbook.at(index).getAddress() << endl;
cout << Addressbook.at(index).getCity() << endl;
cout << Addressbook.at(index).getState() << endl;
cout << Addressbook.at(index).getZipCode() << endl;
cout << Addressbook.at(index).getOrganization() << endl;
cout << Addressbook.at(index).getJobTitle() << endl;
cout << Addressbook.at(index).getHomePhoneNumber() << endl;
cout << Addressbook.at(index).getCellPhoneNumber() << endl;
cout << Addressbook.at(index).getWorkPhoneNumber() << endl;
cout << Addressbook.at(index).getEmailAddress() << endl;
cout << Addressbook.at(index).getNickname() << endl;
cout << Addressbook.at(index).getBirthday() << endl;
cout << Addressbook.at(index).getNotes() << endl;
cout << endl;
found == true;
} //End if statement
if (found==false) //If searched target not found
{
cout << "Contact not found." << endl;
}
}
|
It only finds the first created object. Any ideas where my problem is? |
You compare only the first item in the Addressbook with the userInput.
You either need to loop through the whole vector or maybe even better use std::find_if
http://www.cplusplus.com/reference/algorithm/find_if/
ah yes, That fixed it thank you!
Topic archived. No new replies allowed.