Results 1 to 6 of 6

Thread: converting int to string?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091
    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

  2. #2
    Lively Member
    Join Date
    Nov 1999
    Location
    Cincinnati, OH
    Posts
    66
    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++;
    }
    If you drop a blond and a UNIX box out a window, which one lands first?

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091
    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

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091
    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

  5. #5
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    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:

    Code:
    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.
    Harry.

    "From one thing, know ten thousand things."

  6. #6
    Lively Member
    Join Date
    Nov 1999
    Location
    Cincinnati, OH
    Posts
    66
    #include <afx.h>
    If you drop a blond and a UNIX box out a window, which one lands first?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width