Solved

Cannot enter a newline into a string - please help!

Posted on 2004-10-30
211 Views
Last Modified: 2008-02-01
Hi,

I'm trying to make a procedure where the user will enter a string as a parameter and the "edit box" will display the string on a new line.  It all works except I cannot make the string appear on a new line.  Where the newline should be, a "|" sybol is displayed instead.

Even when I submit the linefeed myself in the paramenter is appears as "|"
e.g. appendText("sometext\n");

Please help


#include <windows.h>
#include <string.h>
#include <stdio.h>
#include <iostream.h>
#include <string>
#include "debug.h"



HWND edit_box = NULL;
#define EDIT_BOX 0x00EB

LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
    switch (iMsg)                                                                  
    {                                                                                    
      case WM_CREATE:                                                            
                  
            RECT rect;

                  
            GetClientRect(hwnd,&rect);
         
            edit_box = CreateWindow("EDIT",NULL,WS_CHILD | WS_VISIBLE | ES_AUTOHSCROLL |
                                                      ES_AUTOVSCROLL | ES_LEFT | ES_MULTILINE | ES_NOHIDESEL |
                                                      WS_VSCROLL | WS_HSCROLL | WS_BORDER | ES_WANTRETURN,
                                                      0, 0, rect.right, rect.bottom, hwnd, (HMENU)EDIT_BOX,
                                                      ((LPCREATESTRUCT)lParam)->hInstance, NULL);

            break;                                                                  
      case WM_SIZE:                                                break;                                          case WM_PAINT:                                                break;                                          case WM_DESTROY:                                                PostQuitMessage(0);                                                    break;                                          }                                                                              
      return DefWindowProc (hwnd, iMsg, wParam, lParam);            
}

int debug::debugInit(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow){
      HWND            debug;
            
      WNDCLASSEX  wndclass;                                                      

    wndclass.cbSize        = sizeof (wndclass);                        
    wndclass.style         = CS_HREDRAW | CS_VREDRAW;            
      wndclass.lpfnWndProc   = WndProc;                                    
    wndclass.cbClsExtra    = 0;                                                
    wndclass.cbWndExtra    = 0;                                                
    wndclass.hInstance     = hInstance;                                    
    wndclass.hIcon         = LoadIcon (NULL, IDI_WINLOGO);      
    wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW);      
                                                                                          
    wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
    wndclass.lpszMenuName  = NULL;                                          
    wndclass.lpszClassName = "Window Class 1";                                                
    wndclass.hIconSm       = LoadIcon (NULL, IDI_WINLOGO);      

      RegisterClassEx (&wndclass);                                    

      debug = CreateWindow ("Window Class 1",                              
                                     "Game debug",                                
                                     WS_OVERLAPPEDWINDOW,                        
                                     -800,                              
                                     100,                                    
                                     400,                                    
                                     400,                                 
                                     NULL,                                                
                                     NULL,                                                
                                     hInstance,                                        
                                     NULL);                                                


      
    // check if window creation failed (hwnd would equal NULL)
   
      if (!debug)
        return 0;
    ShowWindow(debug, nCmdShow);   // display the window
    UpdateWindow(debug);
    SetWindowText(edit_box, "yo");
}

void debug::appendText(char *str){
      char *buff = NULL;
      int length = GetWindowTextLength(edit_box);
      length++; // This accounts for a NULL terminator character

      buff = new char[length];
      GetWindowText(edit_box, buff, length);

      
      char *finalbuff1  = strcat(buff, "\n");

      char *finalbuff  = strcat(finalbuff1, str);

      SetWindowText(edit_box, finalbuff);

}
0
Question by:aspaul
    3 Comments
     
    LVL 55

    Expert Comment

    by:Jaime Olivares
    >appendText("sometext\n");
    You need:
    appendText("sometext\r\n");
    0
     

    Author Comment

    by:aspaul
    Further to this problem: I added the above code and it works but after a few hundred calls or so the program crashes out and I beleive a problem with resourses causes it.

    I added some delete statements as show below:

    void debug::appendText(char *str){
          char *buff = NULL;
          int length = GetWindowTextLength(edit_box);
          length++; // This accounts for a NULL terminator character

          buff = new char[length];
          GetWindowText(edit_box, buff, length);

          
          char *finalbuff1  = strcat(buff, "\r\n");

          char *finalbuff  = strcat(finalbuff1, str);

          SetWindowText(edit_box, finalbuff);

          delete[] buff;
          delete[] finalbuff1;
          delete[] finalbuff;

    }

    The problem now is on the first call to this procedure I get this error:

    Debug Error!

    Program...path\myapp2.exe

    DAMAGE: after Normal block (#83) at 0x00365988

    (press retry to debug the application)

    Anyone know why??
    0
     
    LVL 55

    Accepted Solution

    by:
    Your are reserving just space for the null character but not for the appending string, try with this:


    void debug::appendText(char *str){
         char *buff;                    // = NULL is not needed;

         int length = GetWindowTextLength(edit_box);
         length += strlen(str)+3; // <-------------This accounts for CR, LF, appending string and  NULL terminator character

         buff = new char[length];
         GetWindowText(edit_box, buff, length);

    //     char *finalbuff1  = strcat(buff, "\r\n");     <----- both pointer variables are not needed
    //     char *finalbuff  = strcat(finalbuff1, str);
    //     SetWindowText(edit_box, finalbuff);
    // replace above block with:

         strcat(buff, "\r\n");
         strcat(buff, str);
         SetWindowText(edit_box, buff);

         delete[] buff;
    }
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
    In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
    In this Experts Exchange video Micro Tutorial, I'm going to show how small business owners who use Google Apps can save money by setting up what is called a catch-all email address in their Gmail accounts. By using the catch-all feature, small busin…
    This video discusses moving either the default database or any database to a new volume.

    884 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now