Hi everyone!
I have been cracking my head for some hours now trying to figure out how to do one part of my assignment.
Problem: I have a struct of persons who have names, signatures and height. I want to search for the database(struct) by signature. If i see the signature, i print the signature, name and height.
Here is my try
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
|
// Struct
struct Database
{
string firstName;
string lastName;
string signature;
double height;
};
// my vector
vector<Database> people;
// my search function
void searchPerson(vector<Database> &vec)
{
string signSearch;
cout << endl << " Enter the signature of the person to search (xxxyyy00): ";
getline(cin, signSearch);
for (size_t i = 0; i < vec.size(); i++) //loop through vec and search
{
if (vec[i].signature == signSearch)
{
cout << " Signature" << setw(20) << " Name " << setw(25) << right << " Height" << endl;
cout << vec[i].signature << setw(20) << vec[i].firstName + " " + vec[i].lastName << setw(25)
<< right << vec[i].height << endl << endl;
}
else
{
cout << "\n No person in the list has the signature " << signSearch << endl;
}
}
cout << "\n Press \"ENTER\" to go to the menu ";
cin.get();
}
|
I have seen a lot of complicated algorithms online on how to do this stuff. But i have hope that this can work. I have some results like this
// This is a sample list to begin with
**************************** NAME LIST ******************************
Number of persons in the list: 7
Nr Signature Name Height[m]
------------------------------------------------------------------------------
1. liomes01 Lionel Messi 1.7
2. ardtur02 Arda Turan 1.77
3. riymah03 Riyad Mahrez 1.82
4. brifel04 Brian Felix 0.89
5. ekxdon05 Ek Donatus 1.23
6. boxekx06 Bo EK 1.77
7. evakas07 Eva Kask 1.67
Enter the signature of the person to search (xxxyyy00): liomes01
Signature Name Height
liomes01 Lionel Messi 1.7
No person in the list has the signature liomes01
No person in the list has the signature liomes01
No person in the list has the signature liomes01
No person in the list has the signature liomes01
No person in the list has the signature liomes01
No person in the list has the signature liomes01
Press "ENTER" to go to the menu
|
I don't understand why the else option keep evaluating! Meanwhile, when i search the last person
Enter the signature of the person to search (xxxyyy00): evakas07
No person in the list has the signature evakas07
No person in the list has the signature evakas07
No person in the list has the signature evakas07
No person in the list has the signature evakas07
No person in the list has the signature evakas07
No person in the list has the signature evakas07
Signature Name Height
evakas07 Eva Kask 1.67
Press "ENTER" to go to the menu
|
I have tried many options. I even used
Which stopped the repeated printing of
No person in the list has the signature
|
But that does not fullfill the program requirement. i.e. The user must be informed whether the search was ok or not.
Please, i need your advice
Thanks in advance