954,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

constructor problem

[php]


hangman::hangman( char * surname, int size)
{
name = new char[size];
encryption = new char[size];

strcpy(name,surname);

for (int i = 0 ;i

Acidburn
Posting Pro
511 posts since Dec 2004
Reputation Points: 12
Solved Threads: 5
 

You need to allocate one more space for null character.....
and after for loop add this line

encryption[45]='\0'';
SpS
Posting Pro
599 posts since Aug 2005
Reputation Points: 70
Solved Threads: 32
 

You need to allocate one more space for null character..... and after for loop add this line

encryption[45]='\0'';

thats an easy way out, only i dont always know the end of the array hence the use of strlenght function.....any other ideas?

Acidburn
Posting Pro
511 posts since Dec 2004
Reputation Points: 12
Solved Threads: 5
 

You need to allocate one more space for null character..... and after for loop add this line

encryption[45]='\0'';

That will be out of bounds.any other ideas?If you intend to use standard C-style string functions, they do require null termination. Otherwise, you could use looping or a function such as memcpy. And of course, you can't print a non-null-terminated with functions expecting null-terminated strings.

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

intresting but if you know the size of the array already ie, you've taken the strlenght function and done this:

int i;

for (i = 0 ; i

Acidburn
Posting Pro
511 posts since Dec 2004
Reputation Points: 12
Solved Threads: 5
 

I don't suppose you could post a minimal snippet that demonstrates the problem?

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 
That will be out of bounds.


Why is it out of bounds...i am saying one more space for null character

SpS
Posting Pro
599 posts since Aug 2005
Reputation Points: 70
Solved Threads: 32
 
I don't suppose you could post a minimal snippet that demonstrates the problem?

sure....

[php]
hangman::hangman( char * surname, int size)
{
name = new char[size];
encryption = new char[size];

strcpy(name,surname);

for (int i = 0 ;i

Acidburn
Posting Pro
511 posts since Dec 2004
Reputation Points: 12
Solved Threads: 5
 

I guess this should work

hangman::hangman( char * surname, int size)
{
    name = new char[size];  
    encryption = new char[size];
    int len=strlen( name ) ;
    strcpy(name,surname);

        for (int i = 0 ;i < len; i++)//This is efficient than the previous one
        {
        encryption[i]='*';
        }
    encryption[len]='\0';

   lives = 5;
   found = 0;

}
SpS
Posting Pro
599 posts since Aug 2005
Reputation Points: 70
Solved Threads: 32
 

I guess this should work

hangman::hangman( char * surname, int size)
{
    name = new char[size];  
    encryption = new char[size];
    int len=strlen( name ) ;
    strcpy(name,surname);

        for (int i = 0 ;i < len; i++)//This is efficient than the previous one
        {
        encryption[i]='*';
        }
    encryption[len]='\0';

   lives = 5;
   found = 0;

}

I've put aload of Cout statements in the constructor.......seems to be the constructor isnt getting called yet I've called it!!

heres main:

[php]
#include
#include
#include
#include

#include "hangman.h"

using namespace std;

int main()
{
char *buffer; // holds name
int size = 0;

char *holdingArray[] =
{
"jane",
"John Deer Company",
"The quick brown fox jumped over the lazy dog",
"Adam",
"Smith"
};

srand(time(0)) ;
int random;

random = ( rand() % 4);

size = strlen( holdingArray[2] ) + 1 ;

buffer = new char(strlen( holdingArray[2] ) + 1) ;

strcpy(buffer, holdingArray[2]);

char guess ;
hangman game1(buffer, size); // creates an instance of the game

cout << "Welcome to hangman alpha ver 1.2 " ;
cout <<"You start off with : "<

Acidburn
Posting Pro
511 posts since Dec 2004
Reputation Points: 12
Solved Threads: 5
 
I've put aload of Cout statements in the constructor.......seems to be the constructor isnt getting called yet I've called it!!


What do you mean it doesn't get called....it will get called automatically when you declare an object...and you can't call it explicitly.....and it doesn't matter how many cout statements you put in constructor

SpS
Posting Pro
599 posts since Aug 2005
Reputation Points: 70
Solved Threads: 32
 

I know that but putting a cout statement in there means, when the constructor is called the cout is also called, hence its shown on the screen....but it doesnt..... Only explination for the inititlization failures

Acidburn
Posting Pro
511 posts since Dec 2004
Reputation Points: 12
Solved Threads: 5
 

2 Things
---------

First...why are you passing size in constructor...you can calculate it inside the constructor since you are already passing the string itself

Second...I don't see any cout statements in your constructor

SpS
Posting Pro
599 posts since Aug 2005
Reputation Points: 70
Solved Threads: 32
 

sorry should of reposted...I've made the "size modifications..... heres the new function with the cout statements,

[php]
hangman::hangman( char * surname)
{
name = new char[size];
encryption = new char[size];

strcpy(name,surname);

cout <<"size of name is: " << strlen(name) << "\n" << endl;

for (int i = 0 ;i

Acidburn
Posting Pro
511 posts since Dec 2004
Reputation Points: 12
Solved Threads: 5
 

Your code should be like this...if not...then i am not getting you

hangman::hangman( char * surname)
{
    int len=strlen(surname);
    name = new char[len+1];  
    encryption = new char[len+1];

    strcpy(name,surname);

    cout <<"size of name is: " <<  len << "\n" << endl;

        for (int i = 0 ;i <len  ; i++)//don't call strlen again and again in for                 loop
        {
        encryption[i]='*';
        }
   encryption[len]='\0';
   cout<<"\nEncrypted Data  :" <<encryption;
   lives = 5;
   found = 0;

}
SpS
Posting Pro
599 posts since Aug 2005
Reputation Points: 70
Solved Threads: 32
 

thanks it seems to be solved now :D, thankyou, well ... I best get plodding along a little more, hoping to get it how I want it soon......

Acidburn
Posting Pro
511 posts since Dec 2004
Reputation Points: 12
Solved Threads: 5
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You