Results 1 to 8 of 8

Thread: Quick Simple Question!

  1. #1

    Thread Starter
    Hyperactive Member AAG's Avatar
    Join Date
    Aug 2000
    Location
    United States
    Posts
    411

    Question 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. ???

  2. #2

    Thread Starter
    Hyperactive Member AAG's Avatar
    Join Date
    Aug 2000
    Location
    United States
    Posts
    411

    ooh one more thing

    'status' must remain as

    Code:
    long status[128];

    thats it thanx for any input

  3. #3
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    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.
    Harry.

    "From one thing, know ten thousand things."

  4. #4

    Thread Starter
    Hyperactive Member AAG's Avatar
    Join Date
    Aug 2000
    Location
    United States
    Posts
    411

    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

  5. #5

    Thread Starter
    Hyperactive Member AAG's Avatar
    Join Date
    Aug 2000
    Location
    United States
    Posts
    411

    yo

    yo man those string functions aren't working. the string that i am working with is

    Code:
    long status[128];
    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?

  6. #6

    Thread Starter
    Hyperactive Member AAG's Avatar
    Join Date
    Aug 2000
    Location
    United States
    Posts
    411

    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

  7. #7
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    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.
    Harry.

    "From one thing, know ten thousand things."

  8. #8

    Thread Starter
    Hyperactive Member AAG's Avatar
    Join Date
    Aug 2000
    Location
    United States
    Posts
    411

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width