Click here to Skip to main content
15,895,799 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
void CDeptStore2Dlg::OnBnClickedNewitem()
{
	// TODO: Add your control notification handler code here
	CNewStoreItemDlg dlg;
	srand( (unsigned)time(NULL) );
	char strNumber[20];

	int number1 = rand() % 999;
	int number2 = rand() % 999;

	sprintf(strNumber, "%d-%d", number1, number2);
	dlg.m_ItemNumber = strNumber;

	if( dlg.DoModal() )
	{
		LVITEM lvItem;

		lvItem.mask = LVIF_TEXT;
		lvItem.iItem = 0;
		lvItem.iSubItem = 0;
		lvItem.pszText = strNumber;
		this->m_StoreItems.InsertItem(&lvItem);
	}
}

i had copy of this code in list control from functionx website..but gives an error...
ERROR ::cant convert parameter 2 from ''char [20]' to LPWSTR

[Edit] Included code block, removed some spelling [/Edit]
Posted
Updated 8-Mar-11 0:46am
v2

The problem comes from these lines:
C++
   char strNumber[20];
   ...
   sprintf(strNumber, "%d-%d", number1, number2);
   ...
   dlg.m_ItemNumber = strNumber;
}


You didn't show us the type of m_ItemNumber but I guess it is a CString.
You have two options:

1- Change your project settings so that CString class will use ANSI strings.
To do that, display the project properties, then select General, then change the Character Set field to Not Set. Do that for all configurations (Release and Debug).

2- If you don't want to change your project settings, then you need to use the appropriate types and functions. Change the lines I mentionned into:
C++
    TCHAR strNumber[20];
    ...
    _stprintf(strNumber, _T("%d-%d"), number1, number2);
    ...
    dlg.m_ItemNumber = strNumber;
}

And make sure the type of m_ItemNumber is CString.
This will work wether your project uses wide char strings or not.
 
Share this answer
 
Modify this line
<br />
wsprintfA(strNumber, _T("%d-%d"), number1, number2);<br />
 
Share this answer
 
v3
Again it gave 2 error ..
ist is b4 i wrote..
2nd is sprintf:cant convert parameter to from 'const wchar_t[6]' to const char*'
 
Share this answer
 
Comments
Ryan Zahra 8-Mar-11 6:56am    
My bad...use wsprintfA instead of sprintfA
Richard MacCutchan 8-Mar-11 7:07am    
sprintf() does not take wide char arrays, use swprintf(). See this link: http://msdn.microsoft.com/en-us/library/ybk95axf.aspx

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900