I am trying to make a comparison search function that can compare two strings together based on chars. For example:
Input: "ca" Struct vector contents under last name fields(sorted vector)(index/value): 0/"cars", 1/"roads" Result: "Name Found at index 0 - cars"
I want users to be able to use any number of chars. The program should then compare the provided search criteria to the content of the vector and return the index of any matches.
So far I have tried to implement this alogorithm without any success, here is my code so far. Also I am pretty new to C++.
// Function for searching through an array for a string value.
int searchArray(std::vector<playerdata> (&people), std::string name) {
int loc = -1;
int counter = 0;
int index = 0;
//when loc is no longer -1, that means the person has been found
for (int i = 0; i < people.size(); i++)
for(int k = 0; k < name.length(); k++) {
std::cout << name[k-1] << std::endl;
std::cout << people[i].lastname[k-1] << std::endl;
std::cout << counter << std::endl;
std::cout << "" << std::endl;
if(name[k-1] == people[i].lastname[k-1]) {
counter++;
}
if(counter == name.length()) {
loc = i;
break;
}
}
//if (people[i].lastname.compare(name) == 0)
//loc = i;
return loc;
}
Here is what I get in my console, I am using cout to debug:
What is player 1 information (F/L/DOB (DD/MM/YYY)), Seperate using a space):
hello sunshine
What is player 2 information (F/L/DOB (DD/MM/YYY)), Seperate using a space):
good bye
Pick from the available options:
1 - Input Data:
2 - Display Original Data:
3 - Sort Data:
4 - Display Sorted Data:
5 - Search By Last Name:
6 - Exit The Program;
3
Array Sorted!!!
Pick from the available options:
1 - Input Data:
2 - Display Original Data:
3 - Sort Data:
4 - Display Sorted Data:
5 - Search By Last Name:
6 - Exit The Program;
4
Player 1: good bye
Player 2: hello sunshine
Pick from the available options:
1 - Input Data:
2 - Display Original Data:
3 - Sort Data:
4 - Display Sorted Data:
5 - Search By Last Name:
6 - Exit The Program;
5
Enter the name to search:
bye
b
b
0
y
y
1
e
e
2
b
s
3
y
u
4
e
n
4
Player Found: good bye
Enter the name to search:
by
y
b
0
y
s
1
Player Found: good bye
Enter the name to search:
b
The player was not found, try again.
Enter the name to search:
sun
u
b
0
n
y
1
u
s
1
Player Found: hello sunshine
Enter the name to search:
sunshine
u
b
0
n
y
1
s
e
1
h
1
i
h
1
n
i
1
e
n
1
u
s
1
n
u
2
s
n
2
h
s
2
i
h
2
n
i
2
e
n
2
The player was not found, try again.
Enter the name to search:
EDIT: As you can see from my console output the code is returning true comparisons when it shouldn't be doing so. An example is the last comparison of is (e == n). The answer should be false but it keeps returning true.
Aucun commentaire:
Enregistrer un commentaire