Solved

displaying text

Posted on 2003-11-23
6
325 Views
Last Modified: 2013-11-20
void CEditorsDlg::OnOK()
{
      LPCTSTR a;
      int d;

      d = 55;
      a = inttostr(d);
   
      MessageBox(a,NULL,NULL);
               SetDlgItemText(IDC_EDIT1,a);
      

}

My question is: Why does MessageBox displayed string 55 on variable "a"
and SetDlgItemText will display garbage characters on variable "a"?
0
Comment
Question by:girlswants_me
  • 3
  • 2
6 Comments
 

Author Comment

by:girlswants_me
ID: 9807978
anybody?
0
 

Expert Comment

by:Drew_Benton
ID: 9808179
Ok since no1 knows, i have a working alternative that i use.

////////////////////////////////////////////
Your code:
LPCTSTR a;
int d;

d = 55;
a = inttostr(d);
   
MessageBox(a,NULL,NULL);
SetDlgItemText(IDC_EDIT1,a);
////////////////////////////////////////////
My code ;) :
char a[128];
int d;
d = 55;
itoa(d,a,10);
MessageBox(a,NULL,NULL);
SetDlgItemText(IDC_EDIT1,a);
////////////////////////////////////////////

itoa(integer, string, number base)
string has to be a char[]!

I hope this works for you, ask if u have ne ques, as for ur orig prob, i dont know y it does that, mabey try using a char [] instead of the char*, (LPCSTR)
- Drew
0
 

Expert Comment

by:Drew_Benton
ID: 9808205
This is going on what i last said, but heres a function that you should add to your programs, and anyone else reading this to reduce the time it takes to messagebox and integer:

void DisplayInt(int num, int base = 10)
{
   static char text[256];
   itoa(num,text,base);
   MessageBox(text,NULL,NULL);
}

*the last param defaults the function to base 10, so you only have to supply the number to display
* static text makes sure that only 256 bytes of memory is used throught out the program, so memorry wont become fragments from heavy usage
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:girlswants_me
ID: 9808351
I have this function for strtoint.


int strtoint(char* pch)
{
       return atoi(pch);  
}


char* inttostr(int no)
{
      char pch[10] = "";
      return itoa(no, pch, 10);
}
0
 

Accepted Solution

by:
Drew_Benton earned 30 total points
ID: 9808623
Ohh im soo sry, i misread your question, what u want to do is this:

char* inttostr(int no)
{
      static char pch[10];
      itoa(no, pch, 10);
      return pch;
}

** itoa does not return the string that is converted, thats why the code is garbaged,

however, you will need to make a string for every number you do, b/c if you do this:

MessageBox(a,NULL,NULL);
SetDlgItemText(IDC_EDIT1,a);  
inttostr(25);
MessageBox(a,NULL,NULL);
SetDlgItemText(IDC_EDIT1,a);

you will see how it is overwritten with every call, so id recommend modifying your code to

something like:

int strtoint(char* pch)
{
   return atoi(pch);  
}

void inttostr(int no, char pch[10])
{
   itoa(no, pch, 10);
}

void CEditorsDlg::OnOK()
{
   char a[10];
   int d;

   d = 55;
   inttostr(d,a);
   
   MessageBox(a,NULL,NULL);
   SetDlgItemText(IDC_EDIT1,a);    
}

I hope this works for you, b/c it worked for me, if you need anymore explanation or need more hlp plz ask :)
- Drew
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 9808819
int d = 55;
CString s;
s.Format(_T("%ld"), d);
AfxMessageBox(s);//displays 55
SetDlgItemText(IDC_EDIT1, s);//displays 55
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now