Lion King
Lion King

Reputation: 33813

How can I make calculator using (native Win32 API)?

I'm beginner in Win32Api, I tried to make calculator but I failed because of conversion of data types between each other

Example:

int N1 = GetDlgItemText(WID,IDC_N1,NULL,NULL);
int N2 = GetDlgItemText(WID,IDC_N2,NULL,NULL);
int RESULT = N1+N2;
MessageBox(NULL,RESULT,L"Message",MB_OK);

The example in above tell me the following error (cannot convert parameter 2 from 'int' to 'LPCWSTR')

And the reason for this error is conversion of data types between each other

Please anybody help me

Upvotes: 0

Views: 2462

Answers (5)

user2058002
user2058002

Reputation:

There is an API for this. Use GetDlgItemInt.

Upvotes: 1

Sam
Sam

Reputation: 1

Looks like you just need to go to project>properties>configuration properties>and change 'character set' to Multi-Byte. It will probably be at Unicode, I think this will work because that's the error I always get when I try to use the WinAPI MessageBox() before changing the character set. Maybe you're trying to do something different? But this should help...

Upvotes: 0

Mike
Mike

Reputation: 1727

Here is correct code for your task:

wchar_t Str1[100], Str2[100], ResStr[100];  
GetDlgItemText(WID, IDC_N1, Str1, 100);  
GetDlgItemText(WID, IDC_N2, Str2, 100);  
int N1 = _wtoi(Str1);
int N2 = _wtoi(Str2);
int RESULT = N1 + N2; 
_itow(RESULT, ResStr, 10);
MessageBox(NULL, ResStr, L"Message",MB_OK);

Useful links:
http://msdn.microsoft.com/en-us/library/ms645489(v=vs.85).aspx
http://msdn.microsoft.com/en-us/library/ms645505(v=vs.85).aspx

Upvotes: 2

Mike
Mike

Reputation: 1727

You need to pass unicode string instead of int to MessageBox.

wchar_t ResStr[100]; //define string  
_itow(RESULT, ResStr, 10); //convert int result to string  
MessageBox(NULL, ResStr, L"Message",MB_OK); //now display string  

Upvotes: 1

chys
chys

Reputation: 1652

Your project isn't set to use Unicode, but you're passing a wide string to MessageBox. You can:

1) Change your project settings so that it defaults to Unicode; or

2) Explicitly call MessageBoxW; or

3) Remove the L, and use the non-Unicode API.

Upvotes: 0

Related Questions