-
Quick Simple Question!
whats wrong with this code
Code:
long status [128];
strcpy(status, "Equal");
if (status == "Equal")
{
MessageBox(0, "EQUAL", "Status On MultiMedia", MB_ICONEXCLAMATION | MB_OK | MB_SYSTEMMODAL);
}
else
{
MessageBox(0, "Not EQUAL", "Status On MultiMedia", MB_ICONEXCLAMATION | MB_OK | MB_SYSTEMMODAL);
}
it doesn't seem to detect that status contains the data "Equal". how would i go about doing this correctly. ???
-
ooh one more thing
'status' must remain as
thats it :) thanx for any input :)
-
Strings made from arrays of chars are just that - arrays. If you remember anything about arrays in C, you'll remember that an array without any subscripts (the square brackets) is just a pointer to the beginning of the string, a pointer to the first element. arrays are also constant if declared as arrays and not allocated at runtime. So an array of characters is of type const char *. String literals (anything you put in quotes as a constant string, "Equal" in this example is a string literal) are just the same, they are arrays (so they're pointers) and are of type const char *.
So, if you look at your code again, what is that actually saying? It's checking whether status has the same value as "Equal". You have two string literals in there, both with the string "Equal" in them, but they are two seperate arrays in memory at runtime, so you cannot check string equality in this way.
What you need is the strcmp() function, which will check each character in a string against the corresponding character in the string you are comparing it to. strcmp() takes notice of case, there's another function called stricmp() that ignores case if you want.
This is something you really need to get to grips with in order to become proficient in C/C++ - the relationship between arrays, pointers and strings. Once you understand it problems like this will be no problem to resolve.
-
Thanx Man.
Yo I really appreciate how you explain things rather than just giving me the full out code. i just got ahold of a nice c book, and i will start reading up on strings and arrays and stuff :) Thanx again :)
-
yo
yo man those string functions aren't working. the string that i am working with is
here is what i'm doing.
Code:
sprintf(szCmdToDo, "status %s %s", "movie","mode");
mciSendString(szCmdToDo, status, 128, 0);
mciSendString returns one of the following values.
"not ready", "paused", "playing", and "stopped".
I need to check the returned value and perform operations according to what the value returned is. The problem is that the string i must use to assign the return value to must be long. The strcmp function does not work since it requires char *. Can you help me?
-
hehe
yo this c++ book I got is really helping me out, i understand what you mean now! suddenly its so clear!! LOL thanx once again harry :)
-
Cool =)
You can get a string version of a long variable (ie if your long had the value 1234, you would get a string "1234") using the ltoa() function (long to ascii), or you can convert the string to a long using the atol() function (ascii to long).
Look them up in your help files, they will explain the functions in detail. You will need to pass in a properly allocated buffer to ltoa() if you use that.
-
yea.
yea, i've used itoa before, so i know how to use it properly, i mainly use it when i'm using SetWindowText, when i have a long and need to return a string. thanx again