Searching with char-s.

Hi. I have a struct where there's a firstname, a surname, and a custom number - from a file. Now, I need to create a function, where the user enters the firstname, then the surname, and the function couts the name, if it's in the struct, and the number.

I tired something like this:

1
2
3
4
5
6
7
8
9
10
11
12
char tempFirst[20];
char tempSur[20];
cout << "Please type in the firstname of the client!" << endl;
cin >> tempFirst;
cout << "Now the surname!" << endl;
cin >> tempSur;

for(int i = 0; i<sum; i++){
    if(results[i].firstName == tempFirst && results[i].surName == tempSur){
       cout << "I found him/her. The number is: " << results[i].identNumb << endl;
    }
}


It's not working, guess because some names are longer than the others, and there's obviously less than 20 character lenght names. Anyway, I tired it with strings, but I got the same result.
you cant compare character arrays like this. you are just comparing the pointers.

looking at cin/cout you are using c++ so switch back to strings and try to get that working.

alternatively you can compare character arrays using the lib functions strcmp() etc.


eg;
if(strcmp(results[i].firstName,tempFirst)==0 && strcmp(results[i].surName,tempSur)==0){

show us the declaration of results[] if you need more info.
Last edited on
Topic archived. No new replies allowed.