d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > A Noob Question In C++
Add Reply New Topic New Poll
Member
Posts: 27,086
Joined: Mar 7 2008
Gold: 685.00
Sep 3 2013 09:23pm
It's a simple program to show string lenght but can't convert int into string to show into IDC_EXIT2 edit control


Code
int someStrLen;
char Val[10];

if (LOWORD(wParam) == IDC_Byte)
{
  GetDlgItemText(hDlg, IDC_Entry2, Val, 10);
  if (Val != NULL)
  {
         someStrLen = strlen(Val);
         SetDlgItemText(hDlg, IDC_EXIT2, // Must have someStrLen value);
  }        
  else
  {
        SetDlgItemText(hDlg, IDC_EXIT2, "Error");
  }
}


This post was edited by eric838 on Sep 3 2013 09:25pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Sep 3 2013 09:26pm
Quote (eric838 @ Sep 3 2013 11:23pm)
It's a simple program to show string lenght but can't convert int into string to show into IDC_EXIT2 edit control


Code
int someStrLen;
char Val[10];

if (LOWORD(wParam) == IDC_Byte)
{
  GetDlgItemText(hDlg, IDC_Entry2, Val, 10);
  if (someStr != NULL)
  {
         someStrLen = strlen(someStr);
         SetDlgItemText(hDlg, IDC_EXIT2, // Must have someStrLen value);
  }        
  else
  {
        SetDlgItemText(hDlg, IDC_EXIT2, "Error");
  }
}


https://www.google.com/search?q=cast+string+to+int+cpp
Member
Posts: 27,086
Joined: Mar 7 2008
Gold: 685.00
Sep 3 2013 09:36pm
Quote (carteblanche @ 3 Sep 2013 22:26)


I tried some but not worked
Member
Posts: 27,086
Joined: Mar 7 2008
Gold: 685.00
Sep 4 2013 12:10am
anyone else w/o google?
Member
Posts: 2,757
Joined: Nov 26 2007
Gold: 1,214.81
Sep 4 2013 07:10am
std::string convertInt(int number)
{
std::stringstream ss;
ss << number;
return ss.str();
}
Member
Posts: 237
Joined: Aug 6 2011
Gold: 6,026.00
Sep 4 2013 01:03pm
in C:

Code
int someStrLen;
char Val[10];

if (LOWORD(wParam) == IDC_Byte)
{
 GetDlgItemText(hDlg, IDC_Entry2, Val, 10);
 if (Val != NULL)
 {
        someStrLen = strlen(Val);
        char *int_to_str = (char*)malloc(20); //should be enough to hold any assignable char buffer on 32 bit systems
        _itoa(someStrLen, int_to_str, 10);//C std call to convert int to char, declared in stdlib.h

        SetDlgItemText(hDlg, IDC_EXIT2, int_to_str);
        free(int_to_str); /*don't forget to free the buffer after use. Consider using a permanent buffer for the scope or a char[] within local scope so that stack releases it on its own*/
 }        
 else
 {
       SetDlgItemText(hDlg, IDC_EXIT2, "Error");
 }
}


This post was edited by flyinggoat on Sep 4 2013 01:05pm
Member
Posts: 27,086
Joined: Mar 7 2008
Gold: 685.00
Sep 9 2013 07:53pm
Code


int someStrLen;
char Val[10];

if (LOWORD(wParam) == IDC_Byte)
{
 GetDlgItemText(hDlg, IDC_Entry2, Val, 10);
 if (someStr != NULL)
 {
        someStrLen = strlen(someStr);
        SetDlgItemText(hDlg, IDC_EXIT2, itoa(someStrLen, Val, 10));
 }        
 else
 {
       SetDlgItemText(hDlg, IDC_EXIT2, "Error");
 }
}


Fixed with this : itoa(someStrLen, Val, 10)

I'm in win32 btw, not console
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll