Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Nov 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Debugging C++ code(Displaying char string per character)

    I've tried a bunch of alternative methods to prevent an assertion error. "not understanding" the bug and why I'm getting it are relevant here, not proper, (or more appropriate), coding methods. I would write it in another way to prevent the error, I simply want to understand what is happening during run-time that causes the situation.

    What is the bug?
    ---------------------------------------
    [assertion error]
    [expression _block_type_is_valid(phead->nBlockUse)]
    --------------------------------

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    int main()
    {
    	cout << "Enter your name : ";
    	string Name;
    	cin >> Name;
    	// Add 1 to reserve space for a terminating null
    	int CharsToAllocate = Name.length() + 1;
    	// request for memory to hold copy of input
    	char* CopyOfName = new char[CharsToAllocate];
    	// strcpy copies from a null-terminated string
    	strcpy(CopyOfName, Name.c_str());
    	// Display the copied string
    	cout << "Dynamically allocated buffer contains : " << endl;
    
    	for (int placeinline = 1; placeinline <= 6; ++placeinline)
    	{
    		if (placeinline == 6)
    		{
    			cout << *CopyOfName << endl;
    			goto terminate;
    		}
    		else
    			cout << *CopyOfName << endl;
    			*CopyOfName++;
    					
    	}
    	terminate:
    	// Done using buffer? Delete
    	delete[] CopyOfName;
    
    	int end;
    	cin >> end;
    	return 0;
    }

  • #2
    New Coder
    Join Date
    Jul 2014
    Location
    Auckland New Zealand
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hello

    I tried it on code:blocks 13.10 (or something and got error on line

    strcpy(CopyOfName, Name.c_str());

    ||=== Build: Debug in forum one (compiler: GNU GCC Compiler) ===|
    C:\Documents and Settings\Eric\My Documents\programing\forum one\main.cpp||In function 'int main()':|
    C:\Documents and Settings\Eric\My Documents\programing\forum one\main.cpp|14|error: 'strcpy' was not declared in this scope|
    C:\Documents and Settings\Eric\My Documents\programing\forum one\main.cpp|27|warning: value computed is not used [-Wunused-value]|
    ||=== Build failed: 1 error(s), 1 warning(s) (0 minute(s), 0 second(s)) ===|




    I see the point you are trying to make, when I write code on Arduino sometimes the error is about six lines up or so

  • #3
    New Coder
    Join Date
    Jul 2014
    Location
    Auckland New Zealand
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Oh I went back it was only a small quick minute problem #include <string> had to be changed to #include <string.h>

    But my guess is that is not the help you wanted.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •