How to compare two structure strings in C++ -
ok, week in class we're working arrays. i've got assignment wanted me create structure employee containing employee id, first name, last name, , wages. has me ask users input 5 different employees stored in array of structure, ask them search field type, search value. lastly, display information positive search results.
i'm still new, i'm sure isn't terribly elegant program, i'm trying figure out how compare user entered string string stored in structure... i'll try give pertinent code below.
struct employee { int empid, string firstname, string lastname, float wage }; employee emparray[] = {}; employee value[] = {}; //code populating emparray , structure, determine search field etc. cout << "enter search value: "; cin >> value.lastname; for(i = 0; < 5; i++) { if(strcmp(value.lastname.c_str, emparray[i].lastname.c_str) == 0) { output(); } } which... thought work, it's giving me following error..
error 1 error c3867: 'std::basic_string<_elem,_traits,_alloc>::c_str': function call missing argument list; use '&std::basic_string<_elem,_traits,_alloc>::c_str' create pointer member d:\myfile any thoughts on what's going on? there way compare 2 .name notated strings without totally revamping program? if want drill me on best practices, please feel free, please try solve particular problem.
it should be:
strcmp(value.lastname.c_str(), emparray[i].lastname.c_str()) == 0)
also note don't have using strcmp used c-style strings. since you're using c++, can use overloaded == operator comparing string objects.
that is:
value.last_name == emparray[i].lastname
note overloaded == operator strings doesn't case insensitive string comparisons - but, can implement such functionality on own.
other suggestions: use vectors instead of arrays and, should specify size arrays.
Comments
Post a Comment