将整数连接到LPCWSTR /字符串

时间:2010-04-13 18:55:48

标签: c

我有一个生成/'滚动'两个骰子的程序。我想将这两个值输出到MessageBox,例如:“Dice Rolled:1和3”。

我遇到的问题是如何将这些整数连接到字符串。我到目前为止的代码如下:

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);

这样做最好的方法是什么?

提前致谢。

2 个答案:

答案 0 :(得分:2)

问题是C确实不支持字符串作为数据类型,因此您需要使用字符数组来模拟字符串。例如:

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);

答案 1 :(得分:0)

您应该使用sprintf创建一个字符串:

sprintf(s, "Dice rolled: %d and %d", dice1, dice2)
相关问题