Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Cannot enter a newline into a string - please help!

Posted on 2004-10-30
3
Medium Priority
?
228 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
Comment
Question by:aspaul
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 55

Expert Comment

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

Author Comment

by:aspaul
ID: 12457038
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:
Jaime Olivares earned 375 total points
ID: 12457084
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

604 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