Hi, I am having trouble with a function where I am trying to search a contact by last name.
I have atruct with all the contacts info first name, last name, etc. I have a vector where I have the struct in. And all the contacts data are coming from an outside file.
So, I am able to add the outside file in one funtion, sort the contacts by last name in anohter function,but I cant get done the function where I'm suppose to search the contact by last name.
It gives me a "debug assertin failed!!!"
I press ignore and return to the program and type the last name so that it can be searched, but it says that my contact cannot be found.
Here is my code for that function.
Everything works fine except that one function.
Any Help please.
void searchData(vector<addressBookType>& contacts)
{
string lname;
unsigned int loc = int();
bool found = false;
cout << "tttSearch Contact by Last Name " << endl;
cout << "" << endl;
cout << " *******************************************" << endl;
cout << "" << endl;
cout << "" <<endl;
cout << "Please Enter The Last Name Of The Employee to be Searched: "<< endl;
cin >> lname[loc];
system("pause");
system("cls");
for(loc = 0; loc < contacts.size(); loc++)
{
if (contacts[loc].lName == lname)
{
found = true;
break;
}
}
Your line string lname; calls the default string constructor and hence creates an empty string. Consequently, even lname[ 0 ] does not exist. The line:
cin >> lname[loc]; is probably causing the debug assertion fail since no matter what the value of loc is the index will be out of range. Also, even if cin >> lname[loc]; succeeded it would only read in the first letter of the name and not the full name.
Try CreativeMFS's suggestion to just read the last name into lname. Your initialization of loc can then be moved down inside the for loop:
for(std::string::size_type loc = 0; loc < contacts.size(); loc++)
An index for the string type is actually of type std::string::size_type which is an unsigned integral type.