Solved

displaying text

Posted on 2003-11-23
6
342 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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 …
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

829 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