Solved

displaying text

Posted on 2003-11-23
6
316 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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
while loop over for loop 7 79
string initialization in java 11 103
fix34  challenge 9 97
Fibonacci challenge 11 84
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
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.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

762 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

21 Experts available now in Live!

Get 1:1 Help Now