I recently make a Win32 program using MFC. I am using Visual C++ 6.0. The program suppose to run a dialog box that will perform a calculation. I get an error that say I must cast from char to char *. Here is the code:
Code:
#include <afxwin.h>
#include "Addition_ids.h"
#include "CAdditionDialog.h"
afx_msg void CAdditionDialog::OnAdd()
{
const int TEXT_SIZE = 16;
char szText(TEXT_SIZE + 1);
CEdit *pTotal = (CEdit *)(GetDlgItem(IDC_TOTAL));
CEdit *pNum = (CEdit *)(GetDlgItem(IDC_NUMBER));
pNum->GetWindowText(szText, TEXT_SIZE);
m_nTotal += atoi(szText);
itoa(m_nTotal, szText, 10);
pTotal->SetWindowText(szText);
pNum->SetWindowText("");
pNum->SetFocus();
}
afx_msg void CAdditionDialog::OnClear()
{
CEdit *pTotal = (CEdit *)(GetDlgItem(IDC_TOTAL));
CEdit *pNum = (CEdit *)(GetDlgItem(IDC_NUMBER));
m_nTotal = 0;
pTotal->SetWindowText("");
pNum->SetFocus();
}
BEGIN_MESSAGE_MAP(CAdditionDialog, CDialog)
ON_COMMAND(IDC_ADD, OnAdd)
ON_COMMAND(IDC_CLEAR, OnClear)
END_MESSAGE_MAP()
class CAdditionApp : public CWinApp
{
public:
BOOL InitInstance()
{
CAdditionDialog addition_dialog;
addition_dialog.DoModal();
return FALSE;
}
} additionApp;
Here the error I get:
Code:
D:\VS 6.0 Test\testexperiment7\Addition.cpp(12) : error C2664: 'int __thiscall CWnd::GetWindowTextA(char *,int) const' : cannot convert parameter 1 from 'char' to 'char *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
D:\VS 6.0 Test\testexperiment7\Addition.cpp(13) : error C2664: 'atoi' : cannot convert parameter 1 from 'char' to 'const char *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
D:\VS 6.0 Test\testexperiment7\Addition.cpp(14) : error C2664: 'itoa' : cannot convert parameter 2 from 'char' to 'char *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
D:\VS 6.0 Test\testexperiment7\Addition.cpp(15) : error C2664: 'SetWindowTextA' : cannot convert parameter 1 from 'char' to 'const char *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
I have try the following solution:
Code:
#include <afxwin.h>
#include "Addition_ids.h"
#include "CAdditionDialog.h"
afx_msg void CAdditionDialog::OnAdd()
{
const int TEXT_SIZE = 16;
char szText(TEXT_SIZE + 1);
CEdit *pTotal = (CEdit *)(GetDlgItem(IDC_TOTAL));
CEdit *pNum = (CEdit *)(GetDlgItem(IDC_NUMBER));
pNum->GetWindowText((char *)szText, TEXT_SIZE);
m_nTotal += atoi((char *)szText);
itoa(m_nTotal, (char *)szText, 10);
pTotal->SetWindowText((char *)szText);
pNum->SetWindowText("");
pNum->SetFocus();
}
afx_msg void CAdditionDialog::OnClear()
{
CEdit *pTotal = (CEdit *)(GetDlgItem(IDC_TOTAL));
CEdit *pNum = (CEdit *)(GetDlgItem(IDC_NUMBER));
m_nTotal = 0;
pTotal->SetWindowText("");
pNum->SetFocus();
}
BEGIN_MESSAGE_MAP(CAdditionDialog, CDialog)
ON_COMMAND(IDC_ADD, OnAdd)
ON_COMMAND(IDC_CLEAR, OnClear)
END_MESSAGE_MAP()
class CAdditionApp : public CWinApp
{
public:
BOOL InitInstance()
{
CAdditionDialog addition_dialog;
addition_dialog.DoModal();
return FALSE;
}
} additionApp;
Although the program is running after I attempt the solution above, but the calculation is not running properly. It keeps come out with error message everytime I do the calculation. I have check the documentation but I cannot find any information that say how to do the proper cast from char to char *. Can you guys help me on this problem? Thank you for your help.