Thread: Conversion Char To Char * Problem

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    7

    Conversion Char To Char * Problem

    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.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    char szText(TEXT_SIZE + 1);

    should be

    char szText[TEXT_SIZE + 1];
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    MFC6 is very old, but GetWindowText has an overload for taking a CString instead of char*, so you should use that one if it exists (if it exists for MFC6, which I don't know).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    It does. But it was called MFC 4 back then.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  2. The Interactive Animation - my first released C program
    By ulillillia in forum A Brief History of Cprogramming.com
    Replies: 48
    Last Post: 05-10-2007, 02:25 AM
  3. How do i un-SHA1 hash something..
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 09-14-2005, 05:59 AM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM