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!

checking character strings with an if statement

Status
Not open for further replies.

StarMechLX

Programmer
Mar 22, 2003
2
US
ok... this is what i want my code do to:

if the variable 'name' is "the same as(i'm not sure if there is the identical operator in cpp)" this string: 'StarMech'

and this is what i have:

if (name == "StarMech")
{
cout << &quot;Verification Success!&quot; << endl;
return 0;
} else {
cout << &quot;Verification Failed!&quot; << endl;
return 0;
}

i already have the character declaration and everything worked out, i just need this one peice of the code to work... whyyyy!!!!????!?!?!?!?
 
hi starmechLx,
why u need to write this complex code?. u can very well go for strcmp() funtion know?

S.Senthil Kumar.
 
Can you explain please? :) i'm a newb when it comes to programming period. I just wanna have it to where it says like, if his input is 'look' like in a MUD, it posts the details of the room, instead of having to cout a bunch of lines saying &quot;1 - Look around, 2 - blah blah blah ect. ect.&quot;
 
It is not possible to write name == &quot;some string&quot; in C/C++. Thus you need to use functions such as strcmp (string compare).

From MSDN (very useful reference, msdn.microsoft.com)

int strcmp( const char *string1, const char *string2 );

Return Value

The return value for each of these functions indicates the lexicographic relation of string1 to string2.

Value Relationship of string1 to string2
< 0 string1 less than string2
0 string1 identical to string2
> 0 string1 greater than string2


For what you want to do:

if( strcmp(name, &quot;My string&quot;) == 0 )
{
// identical strings
}
else
{
// different
}


Vincent
 
>> It is not possible to write name == &quot;some string&quot;

Well that's not entirely accurate. If the variable “name” is a class type that defines an equality operator where the second parameter is defined as &quot;const char*&quot;, it is entirely possible.

-pete
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top