error while comparing char[] and string

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)) ===|

3) i have used '#include<string>'
Last edited on
closed account (48T7M4Gy)
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

http://www.cplusplus.com/reference/cstring/strcmp/?kw=strcmp

http://www.cplusplus.com/reference/string/string/operators/
http://www.cplusplus.com/reference/string/string/compare/

i've changed character to a character array,
tried using only if, it gives me the following error
1
2
3
4
D:\C++ Project\StartOver.cpp||In member function `void AddressBook::FindContact()':|
D:\C++ Project\StartOver.cpp|86|error: cannot convert `bool' to `const char*' for argument `1' to `int strcmp(const char*, const char*)'|
D:\C++ Project\StartOver.cpp|87|error: expected `)' before "cout"|
||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 1 second(s)) ===|

here's my entire code:
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include<iostream>
#include<string>
using namespace std;

class Contact
{
    private:

     char Name[50], Num[10];

    public:
    //operator overloading
        friend istream& operator >> (istream &in, Contact &str)
        {
            cout<<"\n\nenter contact information\n\n"<<endl;
            in>>str.Name>>str.Num;
            return in;
        }

        friend ostream& operator << (ostream &out, Contact &str)
        {
            cout<<"\n\ncontact details are:\n\n";
            out<<str.Name<<"\n"<<str.Num<<endl;
            return out;
        }
        friend string GetName(Contact);
        friend string GetNum(Contact);
};
string GetName(Contact obj)
{
    return obj.Name;
}
string GetNum(Contact obj)
{
    return obj.Num;
}

class AddressBook
{
    private:
        static int n;
        Contact Rec[];
    public:
        void Choice()
        {
            int c;
            for(;;)
            {
               cout<<"1. Add\n2. Display\n3. Find contact\n4. exit\n";
               cout<<"enter your choice\n";
               cin>>c;
               switch(c)
               {
                   case 1 : Add();
                            n++;
                            break;
                   case 2 : Display();
                            break;
                   case 3 : FindContact();
                            break;
                   case 4 : exit(0);

                   default : exit(0);

               }
            }
        }
        void Add()
        {
            cin>>Rec[n];
        }
        void Display()
        {
            for(int i=0;i<n;i++)
            cout<<Rec[i]<<endl;
        }

        void FindContact()
        {
            char c[20],tem[20]; int i;
            cout<<"enter the name of the contact to find\n";
            cin>>c;

            for(i=0;i<n;i++)
            {
              if(strcmp(c==GetName(Rec[i]))
               cout<<"contact found";
              else
                cout<<"contact not found";
            }

        }
};
int AddressBook::n=0;

int main()
{
    AddressBook a;
    a.Choice();
}
closed account (48T7M4Gy)
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? :-)
C++ taliban

The what, now?

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.
Last edited on
closed account (48T7M4Gy)
Yeah the C++ taliban are an offshoot of the Array Police (Vector Protectorate)
I appreciate you're being funny, but the joke's in pretty poor taste.
closed account (48T7M4Gy)
Not to forget the Smart Pointer Riot Squad.
lol thanks. comparing a sting and a character array works! and yes, as
mikey
said, 94 is the initialization of the index of the Contact array of objects :P
Topic archived. No new replies allowed.