?
Solved

displaying text

Posted on 2003-11-23
6
Medium Priority
?
368 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
Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

 

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

Get proactive database performance tuning online

At Percona’s web store you can order full Percona Database Performance Audit in minutes. Find out the health of your database, and how to improve it. Pay online with a credit card. Improve your database performance now!

Question has a verified solution.

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

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Suggested Courses
Course of the Month10 days, 14 hours left to enroll

765 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