Results 1 to 5 of 5

Thread: switch

  1. #1

    Thread Starter
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    I know how to do this in VB but not in VC++

    Code:
    Select Case BookName
    Case "VB"
        '//Do what ever I need here
    Case "VC"
        '//Do what ever I need here
    Case "Java"
        '//Do what ever I need here
    Case "Others"
        '//Do what ever I need here
    End Select
    I try this in VC++, & it seem not work as what I expected.
    Code:
    switch (BookName)
    {
        case 'VB':
            '//Do what ever I need here
            break;
        case 'VC':
            '//Do what ever I need here
            break;
        case 'Java':
            '//Do what ever I need here
            break;
        case 'Others':
            '//Do what ever I need here
            break;
    }

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    You can't. Since strings in C are arrays, you can't compare them like that. You need a long string of else ifs.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3
    Addicted Member substring's Avatar
    Join Date
    Feb 2001
    Posts
    148

    Smile

    The Switch statement can evaluate integer, long or char, but NOT variables (unlike the case statement in VB). Also, keep in mind that whichever case is evaluated to be true, the program will "jump" to that point AND CONTINUE TO EXECUTE ALL THE SUBSEQUENT CODES within the Switch statement (i.e. all the subsequent cases). If you don't want that to happen, you have to put a break after each case statement.

    Hope this helps.

    substring.

  4. #4
    Addicted Member substring's Avatar
    Join Date
    Feb 2001
    Posts
    148

    Smile

    Oops, I see that you know how to use break. =) But again, you cannot use Switch on variables.

    substring.

  5. #5
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    Apart from what's been said already, you don't use the apostrophe ( ' ) character to denote strings; they should be surrounded by speech marks - "this is a string". The apostrophes should be used when you want the code for a single character, which you are entering as a literal. So you might have

    char continue = 'y';

    or

    char newline = '\n';
    Harry.

    "From one thing, know ten thousand things."

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