Thread: VC++ help.

  1. #1
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257

    VC++ help.

    Hey,
    I know there is a windows board, but I think the problem is in the non-visual part.
    I want to extract user input from edit boxes into an array. The code I have only works if all the boxes are filled. I want it to keep going if the edit box is empty and just not put that value into the vector. Here's what I got:
    Code:
              structLink templink;
    
    	CEdit* pGetEdit[512];
    	
    	for(i=0; i<FAD.nExpected; i++)
    	{
    		
    		pGetEdit[i] = (CEdit*) GetDlgItem(i+100);
    
    		int len = pGetEdit[i]->LineLength();
    
    		if(len > 0)
    		{
    			pGetEdit[i]->GetLine(1,templink.ParamName.GetBuffer(len), len);
    			templink.Index = i;
    			vars.vectParmSelected.push_back(templink);
    		}
    
    	}
    I get a run time error that memory could not be read. My check for emprty strings maybe wrong or something else.

    thanks a lot.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I think this should be in the Windows board.

    Obviously you declared i higher up?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    From MSDN (CEdit::GetLine): The copied line does not contain a null-termination character.

    From MSDN (CString::GetBuffer): If you use the pointer returned by GetBuffer to change the string contents, you must call ReleaseBuffer before using any other CString member functions.

    From MSDN (CString::GetBuffer): Note that if you keep track of the string length yourself, you should not append the terminating null character. You must, however, specify the final string length when you release the buffer with ReleaseBuffer. If you do append a terminating null character, you should pass –1 for the length to ReleaseBuffer and ReleaseBuffer will perform a strlen on the buffer to determine its length.


    Translation: You need to call ReleaseBuffer with the Length specified (len). As I said last time the problem is not with how you are using the vector, it's with how you are using CEdit and CString.

  4. #4
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    I've included the ReleaseBuffer line, but there's still a problem. same thing with the if statement. I also tried checking how many characters getLine retrieved, but same thing.

    Daved, is there an on-line tutorial that explains all this, or any good VC++ tutorials?

    Code:
    structLink templink;
    
    	CEdit* pGetEdit;
    	i=0;
    
    	int len = 1;	
    	
    	do
    	{
    		pGetEdit = (CEdit*) GetDlgItem(i+100);
    		len = pGetEdit->LineLength();
    		
    		pGetEdit->GetLine(0,templink.ParamName.GetBuffer(len));
    		
    		templink.ParamName.ReleaseBuffer(len);
    		templink.Index = i;
    
    			//if(len >0)
    
    			vars.vectParmSelected.push_back(templink);
    	
    		templink.ParamName.ReleaseBuffer(len);	
    		
    		i++;
    	}
    	while(i<FAD.nExpected);

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Not that I know of... I just use MSDN (you can use MSDN online as well, but it's harder to find stuff).

    The only other suspicious thing is the cast of the return value of GetDlgItem. Are you sure that the IDs you are passing it are valid IDs and are all edit boxes?

    I'd suggest you use your debugger (I assume you're using VC++) to get more information. It is a powerful tool. There should be tutorials for using the VC++ debugger online, I don't know any myself. At the least you should run it through the debugger, and set your exceptions to always stop. If you are using VC++ 6.0, once the program is running, go to Debug->Exceptions..., then select all the exceptions in the box and hit the Stop Always option, then hit OK. This should get the debugger to stop right when the error occurs. If you are using another IDE let us know and somebody might be able to tell you how to do the same thing if you cannot find it yourself.

  6. #6
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    I tried the exceptions thing, and all I got was an access violation at the line of the if statement. I'm not sure what to do about it.
    Last edited by earth_angel; 07-14-2005 at 12:35 PM.

  7. #7
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Try not to dual-post.

    I answered your question in the proper thread on the Windows board here.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to show line no in VC ++
    By mpushkar in forum Tech Board
    Replies: 1
    Last Post: 12-17-2006, 09:33 AM
  2. makefile exported by vc 6.0 doesn't work
    By wow in forum Windows Programming
    Replies: 7
    Last Post: 03-24-2006, 04:20 PM
  3. Can't compile this with VC 6.0
    By uriel2013 in forum C++ Programming
    Replies: 4
    Last Post: 05-31-2003, 07:43 PM
  4. VC 7.0 and templates
    By Mongush in forum C++ Programming
    Replies: 12
    Last Post: 03-11-2003, 10:32 AM
  5. Why VC?
    By Unregistered in forum Windows Programming
    Replies: 1
    Last Post: 04-15-2002, 05:24 AM