2010-04-13 71 views
0

我有一个程序可以生成/'滚动'两个骰子。我想将这两个值输出到MessageBox,例如:“Dice Rolled:1 and 3”。将整数连接到LPCWSTR /字符串

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

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

的问题是,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); 
+0

谢谢你,这对我来说很好。有没有什么方法可以在LPCWSTR中使用,因为这是我们已经教过的,并且只允许使用到目前为止。 – 2010-04-13 19:12:43

0

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

sprintf(s, "Dice rolled: %d and %d", dice1, dice2)