Link to home
Start Free TrialLog in
Avatar of aspaul
aspaul

asked on

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

}
Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

>appendText("sometext\n");
You need:
appendText("sometext\r\n");
Avatar of aspaul
aspaul

ASKER

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??
ASKER CERTIFIED SOLUTION
Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial