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

	strcpy(name,surname);

		for (int i = 0 ;i <size; i++)
		{
		encryption[i]='*';
		}

   lives = 5;
   found = 0;

}

encryption seems to be heading over its borders!! the size = 45 ; I've checked that so it should be restricted to 45, however its not...when I print the value of encrpytion on the screen I get 45 * then about 10 other chars which are junk values.....anyone offer a solution?

Thanks,

John

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

encryption[45]='\0'';

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?

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.

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 <strlenght; i++) ...assuming you've added one to the end of the lenght for the null,, it should work? since its basically what was perviously posted...but it doesnt.....why?

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

That will be out of bounds.

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

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

sure....

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

	strcpy(name,surname);

		for (int i = 0 ;i <strlen( name )  ; i++)
		{
		encryption[i]='*';
		}
	

   lives = 5;
   found = 0;

}

it prints out 45 '*' and another 16 junk values......why just 16 I dont know....

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 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:

#include <iostream>
#include <fstream>
#include <cstring>
#include <ctime>

#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 : "<<game1.getlives() << " lives \n " << endl;


  while (game1.getlives() !=0 && game1.checkstatus() !=0) 
      {
	 cout << "\n";
       game1.checkstatus();
       //cout << game1.getencrption();

	   cout << "\n"; //seperates * from inputted chars
	   cout << "guess:";

       guess = getchar() ;

	   system("cls");

	   game1.writefile(guess);
       game1.getletter(guess);

	   cout << "Your attempted letters are: " ;

	   game1.readfile();

      }


      if (game1.getlives() == 0)
      {
         cout << "You've lost the game!\n Your Guess's where: " <<endl;
		 game1.readfile();
		 cout << endl;
      }

	ofstream outfile; //writes to txt
	outfile.open("new.txt");


      return 0;
}

hangman game1(buffer, size) // creates an object but it doesnt call the constructor!

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

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

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

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

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 <strlen( name )  ; i++)
		{
		encryption[i]='*';
		}

   lives = 5;
   found = 0;

}

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;

}

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......

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.