tags:

views:

20

answers:

2

Hello,

I have a program that generates/'rolls' two dice. I would like to output these two values to a MessageBox, for example: "Dice Rolled: 1 and 3".

The problem I am having is how to go about concatenating these integers to the string. The code I have so far is as follows:

MessageBox( NULL,                  // hWnd      - window owner (none)
            L"Dice:",              // lpText    - text for message box
            L"Dice rolled:",       // lpCaption - title for message box
            MB_OK |                // uType     - make ok box
            MB_ICONEXCLAMATION);

What would be the best way to go about doing this?

Thanks in advance.

A: 

You should use sprintf to create a string:

sprintf(s, "Dice rolled: %d and %d", dice1, dice2)
compie
+1  A: 

The problem is that C really doesn't support strings as a data type, so you will need to simulate strings using character arrays. For example:

int die1, die2; /* need to be set somehow */
wchar_t dice[100];

wsprintf(dice, L"Dice: %d and %d", die1, die2);
MessageBox(NULL, dice, L"Dice Rolled:", MB_OK | MB_ICONEXCLAMATION);
Ferruccio
Thank you, this worked perfectly for me straight away. Is there any way to use this with LPCWSTR as this is what we've been taught and am only allowed to use that so far.
Charlie Baker