Thread: Displaying Text and Variables to a window

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    3

    Question Displaying Text and Variables to a window

    I am starting up on my first windows program and ran into a bit of a snag.

    I need to display a string and a variable to the window. I know how to display a string. I can even display the variable. But I can't display them both. I've tried strcat (string, (char *) &variable) and this will compile, but when I run the program it crashes.

    Interseting enough, whenever I use strcat in a console program it crashes as well. So I would like to avoid using that if I can (having no idea why strcat causes the program to fail).

    Basically, here is the code I am starting with:

    code
    -----------------------------------
    HDC h;
    int a = 9;
    char *buffer = "Data: ";

    h = GetDC (hwnd);
    TextOut (h, 0, 0, buffer, (sizeof (buffer)+1));
    ReleaseDC (hwnd, h);

    ------------------------------------
    /code

    -or-

    code
    ------------------------------------

    HDC h;
    int a = 9;
    char *buffer = "Data: ";

    h = GetDC (hwnd);
    TextOut (h, 0, 0, (char *) &a, (sizeof (buffer)+1));
    ReleaseDC (hwnd, h);

    ------------------------------------
    /code

    -or-
    code
    -----------------------------------
    #include <string>
    using namespace std;
    .
    .
    .

    HDC h;
    int a = 9;
    char *buffer = "Data: ";

    strcat (buffer, (char *) &a);

    h = GetDC (hwnd);
    TextOut (h, 0, 0, buffer, (sizeof (buffer)+1));
    ReleaseDC (hwnd, h);

    ------------------------------------
    /code == compiles fine but Crashes when it runs


    Steve

    p.s. I also tried doing consecutive TextOut commands, but couldn't figure out where the last x, y pos of text was printed, so I don't know how to start printing either after or under previous text. Should I be using another function to be writing text to a window?

  2. #2
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Try the sprintf function:
    Code:
    char tmpBuf[100];
    int a = 9; 
    char buffer[] = "Data: "; 
    sprintf(tmpBuf, "%s %d", data, a);
    ...
    TextOut (h, 0, 0, tmpBuf, (sizeof (tmpBuf)+1)); 
    ...
    B.t.w. which compiler are you using?

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    3
    Originally posted by Monster
    B.t.w. which compiler are you using? [/B]
    I am using Dec-C++ ver. 4.9.3.0 in Windows 2000.

    The sprintf function works real good, thank you.

    A couple of questions about it though.

    1) Can I print line breaks into it
    sprintf (buffer, "%s \n %s", text1, text2) does not seem to
    work

    2) When I do TextOut I have to pass the number of characters to
    print. Now, if I am using 2 strings, this is an easy process:

    TestOut (h, 0, 0, buffer2, (sizeof (buffer) / sizeof (char)));

    I could also just use the sizeof (buffer) wihout the sizeof (char)
    and the results would look correct but I think this is more
    correct...

    anyway, back to the question: When I am using an integer
    as the second part of sprintf (buffer, "%s %d, text1, num1)
    the sizeof (buffer) / sizeof (char) doesn't pass the correct
    number of characters to print.

    This is more due to the fact that when I am declaring buffer
    I have to do so as an array and create a size for that array.
    I've been doing this as
    char buffer [(sizeof(text1) + sizeof(text2))/sizeof(char)]
    which works fine for strings but nt for integers. I need to
    create a number of chars in my array = number of digits in
    my number.

    when it comes down to it, that is what I can't figure out how to
    do, find the number of digits in my number so I can use that to
    determine how big my buffer should be.

    3. Is there a source somewhere that I can get more information
    about the sprintf function? It is part of the windows.h library
    right? It is not described in my C++ book. I would like to know
    more of the variable refernces that can be used (for instance
    %s = string, %d = integer) . If anyone could direct me to
    someplace that might describe this it would help.


    Thanks again. And I am real sorry about this long post

    Steve

  4. #4
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Hi Steve,

    1) sprintf (buffer, "%s \n %s", text1, text2) does not seem to work

    I think the TextOut function doesn't handle newline characters. Can't do anything about that.

    2) When I do TextOut I have to pass the number of characters to
    print. Now, if I am using 2 strings, this is an easy process:

    TextOut (h, 0, 0, buffer2, (sizeof (buffer) / sizeof (char)));


    When you want to pass the number of characters to print you don't use the sizeof function. This function returns the size of the buffer. Use the strlen function instead. This function returns the actual number of characters in a string.
    Code:
    char tmpBuf[100];
    int a = 9; 
    char buffer[] = "Data: "; 
    sprintf(tmpBuf, "%s %d", data, a);
    ...
    TextOut (h, 0, 0, tmpBuf, strlen(tmpBuf));
    3. Is there a source somewhere that I can get more information
    about the sprintf function? It is part of the windows.h library
    right? It is not described in my C++ book.


    Actually it's ANSI C and is part of the standard C library. You can find more information on the sprintf function on this manual page. Be carefull because these are UNIX manual pages.

    You can also try searching with GOOGLE.

    Cheers,
    Monster

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Parsing Text File and gathering input variables
    By azamsharp1 in forum C Programming
    Replies: 2
    Last Post: 10-26-2005, 08:43 AM
  2. struct question
    By caduardo21 in forum Windows Programming
    Replies: 5
    Last Post: 01-31-2005, 04:49 PM
  3. input/output
    By dogbert234 in forum Windows Programming
    Replies: 11
    Last Post: 01-26-2005, 06:57 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM