[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

displaying text

Posted on 2003-11-23
6
Medium Priority
?
380 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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 60 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 45

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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

650 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