| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
A simple String compare
I have a simple question for the board. I'm new here and to c++ and I'm having major issuses writing a string compare function that returns -1 if the compare fails and a struct number (lets say j) if the string is he same.
Can anyone help me with this? |
|
#2
|
|||
|
|||
|
Code:
int cmpstr(str1, str2)
{
if(str1 == str2)
{
return struct_number;
}
else
return -1;
}
so; string stringer = "HELLO!" string stringerer = "goodbye" cmpstr(stringer, stringerer); would return -1 This only works on c++ strings and not c strings (e.g. char * ok[100]). |
|
#3
|
|||
|
|||
|
Code:
void FindRecord()
{// Finds and displays specified record on screen
char CallNo[cMaxChars];
int i;
cout << " Enter call number: ";
cin.getline(CallNo, cMaxChars);
i = SearchRecords(CallNo);
if (i != -1)
{
PrintRecord(i);
}
else
{
cout << "Record Not Found" <<endl;
};
}
int SearchRecords(char CallNo[])
{// returns index of matching record or -1 if not found
int i = 0;
if(CallNo == gRecs[i].CallNumber)
{
return i;
}
else
{
return -1;
};
}
//My display records function works like a charm but I can't get these two functions //to communicate on the same wave-length. Can somebody tell me how to get the //void function calling the int function effectively? |
|
#4
|
|||
|
|||
|
What type is gRecs[i].CallNumber? If it is an integer, you can't compare a string to an integer and it will fail. If it is a char[], you can't compare char*/char[] strings (c-strings) with the == operator. You need to use strcmp().
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > A simple String compare |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|