[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 238
  • Last Modified:

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

}
0
aspaul
Asked:
aspaul
  • 2
1 Solution
 
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

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now