PDA

Click to See Complete Forum and Search --> : converting int to string?


softwareguy74
Jan 4th, 2001, 10:06 AM
Hi,

I'm trying to implement a very simple function using VC++ in response to a user hitting a command button on a dialog box.

Everytime the user hits the button, I want a counter to increment and popup a message box to inform the user how many times the button has been pressed.

I defined an int as iCnt and then in the code I have:

...
iCnt=1;
MessageBox(hDlg,iCnt,"Info",MB_OK);
//implement counter incrementer here..
...

When running the above code, I get the following error:

'MessageBoxA' cannot convert parameter 2 from int to const char * bla bla bla...

I'm assuming the message box wants a string rather than an int. Also, how would I increment the counter after MessageBox()?

Thanks,

Dan

aaudette
Jan 4th, 2001, 10:34 AM
Add this code to the Form class that MFC Appwizard created for you:

class Form
{
public:

private:
int NumHits;
}

//Constructor
void Form::Form()
{
NUmHits=1;
}
void Form::OnButton()
{
CString str=NumHits;
AfxMessageBox(str);
NumHits++;
}

softwareguy74
Jan 4th, 2001, 11:35 AM
Actually, I'm not using MFC but I think I can figure out how to implement your example into my code.. If I can't, I will post a reply..

Thanks,

Dan

softwareguy74
Jan 4th, 2001, 12:54 PM
Is CString only available using MFC? In another post, I asked about converting an int to a string and the reply I got was:

char buf[40];
int iCnt;
int radix = 10;
itoa(iCnt,buf,radix);

That works fine, but seems a little much to just convert from an int to a string.. I tried the CString but it doesn't recognize it. How can I use CString without using MFC?

Thanks,

Dan

HarryW
Jan 4th, 2001, 01:07 PM
I think if you #include <string> (note no .h, it's the standard library not Microsoft's) you can use the String data type, which is basically the same as CString I think.

Using the basic data types char* and int, you would use the code you gave (more or less). In your code, all you would have to do is something like this:


char buf[40];
MessageBox(hDlg, itoa(iCnt, buf, 10), "Info", MB_OK);


The itoa() function puts the string value in buf, and also returns it as the function's return value.

aaudette
Jan 4th, 2001, 02:12 PM
#include <afx.h>