Results 1 to 8 of 8

Thread: Variables in strings (part II)

  1. #1

    Thread Starter
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772

    Variables in strings (part II)

    Ok, I am trying to MessageBox an integer variable. I can convert it to a string using itoa(), but how can i put text into it? This is what i am doing now:

    Code:
    int szTickCount;
    char buffer[33];
    
    szTickCount = GetTickCount();
    szTickCount = szTickCount/(1000*60); //minutes
    itoa(szTickCount, buffer, 10);
    MessageBox(NULL, buffer, "How long have you been on?", MB_OK | MB_ICONINFORMATION);
    This works, but all it says is the number (ex: 43). How can I redo this so it says something like "Your computer has been on for 43 minutes"?
    Alcohol & calculus don't mix.
    Never drink & derive.

  2. #2
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    Quote from another thread
    How would I do something like this:


    PHP:--------------------------------------------------------------------------------
    int score;
    char bufferScore[4];
    itoa(score, bufferScore, 10);
    MessageBox...--------------------------------------------------------------------------------

    I want it to say "Your score is (whatever it is), good job!" or something like that...in vb its the &, what is it in C++
    Answer:
    Code:
    #include <iostream.h>
    #include <windows.h>
    
    int main(int argc, char* argv[])
    {
    int score = 5555;
    char bufferScore[5];
    itoa(score, bufferScore, 10);
    char allText[] = "Your score is ";
    strcat(allText,bufferScore);
    strcat(allText," good job");
    MessageBox(NULL,allText,"ok",MB_OK);
    return 0;
    }
    or use the string class (much easier)

    Code:
    #include <string>
    using namespace std;
    
    int main(int argc, char* argv[])
    {
    string t1 = "text1";
    string t2 = "text2";
    string all  = t1+t2;
    MessageBox(NULL,all.c_str(),"Caption",MB_OK);
    return 0;
    }
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

  3. #3
    Lively Member
    Join Date
    May 2000
    Posts
    123
    or you could do this:

    PHP Code:
    #include <windows.h>
    #include <stdio.h>

    void mainvoid )
    {
       
    int szTickCount;
       
    char buffer[33];

       
    szTickCount GetTickCount();
       
    szTickCount szTickCount/(1000*60); //minutes

       
    sprintfbuffer,"Your computer has been on for %d minutes"szTickCount );
       
    MessageBox(NULLbuffer,MB_OK MB_ICONINFORMATION);


  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Code:
    MessageBox(NULL, buffer,MB_OK | MB_ICONINFORMATION);
    I think you missed a parameter there somewhere
    Code:
    MessageBox(NULL, "Hello", buffer, MB_OK | MB_ICONINFORMATION);
    Also, it's a better idea to use _snprintf as this prevents you overrunning a buffer:
    Code:
    TCHAR pcBuffer[100];
    _sntprintf(pcBuffer, 100, Your computer has been on for %d minutes", szTickCount);
    One thing about prefixes -- if you're going to use them, then the official prefix for int is i. sz is for NULL-terminated strings.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  5. #5
    PowerPoster sail3005's Avatar
    Join Date
    Oct 2000
    Location
    Chicago, IL, USA
    Posts
    2,340
    Are there any other standard prefixes, for say string, char, bool, double etc?

    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA

  6. #6
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    I posted the official document somewhere, but I have a slightly adapted version of that, so this is my system (similar but different):
    Code:
    Prefix  |  Type
    --------|---------------
    p       |  Pointer
    b       |  bool
    ub      |  unsigned char [b for byte]
    sb      |  signed char
    us      |  unsigned short
    ss      |  signed short
    ul      |  unsigned long
    sl      |  signed long
    f       |  float
    d       |  double
    c       |  TCHAR [char for MBCS, wchar_t==unsigned short for Unicode]
    So, for a pointer to a character, the total prefix is pc as used above.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  7. #7
    PowerPoster sail3005's Avatar
    Join Date
    Oct 2000
    Location
    Chicago, IL, USA
    Posts
    2,340
    Cool, thanks for the info. I like using prefixes, and find them useful. One more question, is there an offical prefix for string, from the string class? perhaps, str?

    Thanks again!

    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA

  8. #8
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    I use s, like I do for VB
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

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