2010-04-13 118 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)