|
-
August 2nd, 2011, 10:24 AM
#1
[RESOLVED] Problem with if
Hi guys. I have one problem with if statement.
Code:
char MorzeLetters[20][6];
for(int i=0; i<letter; i++)
{
char temp[6];
strcpy_s(temp, 6, "-.--");
strcpy_s(MorzeLetters[i], 6, "-.--");
if(MorzeLetters[i] == temp)
strcat_s(result, 256, "y");
}
That if always fails and i cannot understand why? Hope you can help me...
BTW i'm trying to write morze alphabet interpreter and in original code MorzeLetters will be set from string. This is just an example of my problem.
-
August 2nd, 2011, 10:27 AM
#2
Re: Problem with if
if(MorzeLetters[i] == temp)
You're comparing pointers, not data.
-
August 2nd, 2011, 11:06 AM
#3
Re: Problem with if
 Originally Posted by fire_
Hi guys. I have one problem with if statement.
Code:
char MorzeLetters[20][6];
for(int i=0; i<letter; i++)
{
char temp[6];
strcpy_s(temp, 6, "-.--");
strcpy_s(MorzeLetters[i], 6, "-.--");
if(MorzeLetters[i] == temp)
strcat_s(result, 256, "y");
}
That if always fails and i cannot understand why?
If you used string classes, then that code would work. Instead you decided to use low-level NULL terminated arrrays of char. Since arrays cannot be compared using ==, the only way to compare for equality is by comparing each character, one-by-one, in a loop. There is a function, strcmp(), that does this already.
Why aren't you using a string class (this is C++), and instead using C-null terminated strings? Then the comparisons will be "natural" in the sense that you can use ==, <=, >=, !=, etc.
Code:
#include <string>
using namespace std;
string MorzeLetters[20];
for(int i=0; i<letter; i++)
{
string temp = "-.--"
MorzeLetters[i] = "-.--";
if (MorzeLetters[i] == temp) { }
}
Regards,
Paul McKenzie
-
August 2nd, 2011, 11:30 AM
#4
Re: Problem with if
That strcmp function works perfectly, thank you. I wasn't using string, because i have to put result to the edit box and earlier i had problems with putting string to edit box...
-
August 2nd, 2011, 11:32 AM
#5
Re: Problem with if
 Originally Posted by fire_
I wasn't using string, because i have to put result to the edit box and earlier i had problems with putting string to edit box...
What kind of problems?
Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
Convenience and productivity tools for Microsoft Visual Studio:
FeinWindows - replacement windows manager for Visual Studio, and more...
-
August 2nd, 2011, 12:27 PM
#6
Re: Problem with if
 Originally Posted by fire_
That strcmp function works perfectly, thank you. I wasn't using string, because i have to put result to the edit box and earlier i had problems with putting string to edit box...
That is because you did not use the string class properly.
First, there is no string class that would not have a way to convert to a type that is compatible to an array of char (const char*). Luckily, std::string has the c_str() function to return a const char*. Did you use that function, or did you try and use std::string directly?
Code:
#include <string>
void someFunc(const char *s) // this takes a const char *
{
}
int main()
{
std::string s;
someFunc(s.c_str() ); // Line 1 is OK
someFunc( s ); // Line 2 is an error
}
Did you try to do something like line 1 or line 2? I bet it was line 2.
So there is no need to go and use low-level C functions to manipulate string data.
Regards,
Paul McKenzie
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|