Cannot enter a newline into a string - please help!

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);

}
aspaulAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jaime OlivaresSoftware ArchitectCommented:
>appendText("sometext\n");
You need:
appendText("sometext\r\n");
0
aspaulAuthor Commented:
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
Jaime OlivaresSoftware ArchitectCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual C++.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.