Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String type as argument in function.

Status
Not open for further replies.

Arowana

Programmer
Feb 22, 2001
19
PT
Hi,
I want to create a function witch receives a string as a argument and the compares is with another string to see if there are equal (like searching a name in a database)
so i have:

int looking(CString name)
{
if (name==outro.name1)
return 1;
else
return 0;
}

Note: outro is just a object from a CDialog that identifies a edit box with is identified by a variable CString name1.
This edit box receives a input from the user and compare with a string received at the function looking.

thanks a lot
 
What is the problem? Just do it. I think yeasier is to write like following:
int looking(CString name)
{
return name==outro.name1;
}
In this case, I do not understand if you need a different function?
John Fill
 
The problems are:

1º- the operator == is not accepted to compare CString types

2º- The system do not recognize a argument CString type at the function looking(CString name)
 
name1 is type CString (receive the input from a edit box control)
 
1. The operator==(CString&,CString&) is perfect legal and defined.
2. If you don't create a project what use MFC, you will not be able to use CString. You can use it only in MFC projects.
3. Maybe you should write looking(CString& name) instead of looking(CString name)? I never had such problems in MFC projects. John Fill
 
What does it mean the operator & at looking(CString& name)???
 
thanks a lot.........by the way..of course i am using a MFC project.


i´ll give you the feed back

nice weekend to you
 
Coming from a NON VC background i still rely on strcmp. I just convert the strings i am comparing to char* and do a

if(!strcmp(str1,str2)) // they are equal dont let the ! fool you

Matt
 
Zyrenthian,
do you want to pass a CString type in a strcmp function :)? John Fill
 
Yes strcmp is your best option if you are passing pointers (a string is just an array after all). You must remember that if str1 and str2 are equal strcmp(str1,str2) will return zero. The reason for this is to allow for easier sorting. So if you are testing for true you have to use if(!strcmp(str1,str2)) or use if(strcmp(str1,str2)==0) whichever you are more comfortable with.

I think your problem might be that you are passing the CString type and trying to compare to the object.property. One is a pointer and one is what a pointer points to. so the equality fails. Try passing CString& name or you can use if (name==outro->name1). I think either will work (and I might have it backwards) but I am really curious to know how it works out :)
 
yo,

the use of strcnp is a exelent idea.

Thanks
 
TFS,
Please read more attentive my question. How can you use the class CString in functions like strcpy, strcmp, strncpy...? In these functions you may pass only pointers to char. By the way, if you do not know MFC, do not say it to whole list.
Arowanna,
Why you do not use Assembler? I think is a better idea :). If you use C++ I can't understand why are you using pointers, strcmp, memcpy and... :)) If you both are so curious how does it work, I'll send to you by email one short sample MFC project, and you'll see. John Fill
 
How can you use the class CString in functions like strcpy, strcmp, strncpy...? In these functions you may pass only pointers to char.

The pointer-dereferencing operator (*) will allow them to be used in the classic string functions.

Chip H.
 
Since you are using MFC, u can use the class wizard to assign a CString variable to the edit box. U can do this only if you have created the edit box in the resource generator. Then the function would look like:

int looking(CString name)
{
UpdateData();
return (name.Compare(name1)); // where name1 is the
// CString value assigned
// to the edit box
OR
return (name.CompareNoCase(name1));
OR
return (name == name1);

UpdateData(FALSE);
}

Otherwise, if you have created the edit box using the 'Create(...)' function, then you can use the GetWindowText(...) function like shown below:

int looking(CString name)
{
CString name1;
outro.GetWindowText(name1);
return (name.Compare(name1));
OR
return (name.CompareNoCase(name1));
OR
return (name == name1);
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top