Hello! i've been working on a addressbook program and there's function to find a contact, and i've used the function '.compare()' which is found in string header file
1 2 3 4 5 6 7 8 9 10 11 12 13 14
void FindContact()
{
char c[20]; int i;
cout<<"enter the name of the contact to find\n";
cin>>c;
for(i=0;i<n;i++)
{
if(c.compare(GetName(Rec[i]))==0) //error
cout<<"contact found";
else
cout<<"contact not found";
}
}
note:
1) GetName(Rec[i]) returns a string i.e name of the contact
2) the error i get is:
1 2 3 4
D:\C++ Project\StartOver.cpp||In member function `void AddressBook::FindContact()':|
D:\C++ Project\StartOver.cpp|82|error: `compare' has not been declared|
D:\C++ Project\StartOver.cpp|82|error: request for member of non-aggregate type before '(' token|
||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
Can't see the rest of your code but c is just a single character which would not be the full name of many people.
If you are comparing c-strings the function is strcmp().
The string taliban will tell you it's probably better to use <string> strings, in which case it would be just if( string1 == string2 ) to perform the test if absolute equality is the requirement ( or compare() but only for std::strings
I'd strongly suggest you use strings and single chars. Don't mix types, so c at line 82 should be declared as a string and not char[20]. That way you avoid all your confusion and the comparison is string1 == string2 as I said previously.
Scrap strcmp() it's getting you nowhere because of the mixed variable types and the C++ taliban prefer strings anyway. (Actually strings are much better than c-strings. ;) ) So that means just have char and string variable types, not char, char[] and strings.
BTW use sensible names for your variables - just 'c' for a string is very poor programming. Use longer names and you'll find it almost reads like a book. Names like 'tem' are meaningless too.
And what's line 94 all about sitting there in the wilderness? :-)
And what's line 94 all about sitting there in the wilderness?
Line 94 is the definition of a class variable. It's a bit unusual putting it in the middle of the file, rather than at the top, but it's perfectly legal.