Thread: declare a variable in a case/switch?

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    53

    declare a variable in a case/switch?

    Code:
    case IDC_TEST:   
    	std::string dicestring;
    	dicestring = Randomdice();
    	SetDlgItemText(hwndDlg, IDC_NUMBER, dicestring.c_str());
                         break;
    	case IDC_ABOUT:
    	DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
    	    break;



    When i compile i am told that "initialization of 'dicestring' is skipped by 'case' label"

    Now the thing to do here would be to simply move this bit :

    std::string dicestring;
    dicestring = Randomdice();

    out of the case/switch...but I am just wondering why i cant have it within?
    I Hope you understand what I mean


    note - I am new to makeing win applications!
    Last edited by Oluf; 12-13-2003 at 01:07 PM.
    Its all a matter of willpower

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    I always use statement blocks in my case statements for this very reason. Plus I think it adds to code readability.
    Code:
    case IDC_TEST:
    {
        std::string dicestring;
        dicestring = Randomdice();
        SetDlgItemText(hwndDlg, IDC_NUMBER, dicestring.c_str());
        break;
    }//case IDC_TEST
    
    case IDC_about:
    {
        DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
        break;
    }//case IDC_about
    gg

  3. #3
    Registered User
    Join Date
    Nov 2003
    Posts
    53
    ahh!!

    Thanks a lot
    Its all a matter of willpower

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  3. Variable name from another
    By Xinco in forum C++ Programming
    Replies: 5
    Last Post: 05-02-2007, 02:19 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM