hangman strings compiler error HELP

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 1263 Views - Last Post: 24 March 2008 - 04:44 AM Rate Topic: -----

#1 rudy5453  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 02-December 07

hangman strings compiler error HELP

Post icon  Posted 14 March 2008 - 03:09 AM

I am having a problem with a dynamic string being passed (i think) here are my compiler errors. I am lost

Compiling...
hangman.cpp
F:\programs\my proggies\hangman.cpp(51) : error C2664: 'display' : cannot convert parameter 2 from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > [6]' to 'const class std::basic_string<char,struct std::char_
traits<char>,class std::allocator<char> > &'
Reason: cannot convert from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > [6]' to 'const class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >'
No constructor could take the source type, or constructor overload resolution was ambiguous
F:\programs\my proggies\hangman.cpp(53) : error C2664: 'addletter' : cannot convert parameter 1 from 'char *' to 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > &'
A reference that is not to 'const' cannot be bound to a non-lvalue
F:\programs\my proggies\hangman.cpp(55) : error C2664: 'checkwinner' : cannot convert parameter 1 from 'char *' to 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > &'
A reference that is not to 'const' cannot be bound to a non-lvalue
Error executing cl.exe.

hangman.obj - 3 error(s), 0 warning(s)



#include <iostream>
#include <fstream>
#include <string>
using namespace std;

//global constants
const char filename[] = "words.dat";
const int BAD=6;


//function prototypes
int qtyOfWords();
bool getWord(string & s, int wtg);
bool addletter(string &bw, string &bg, const string &wtg, char ch);
int checkwinner( string &bw, string &bg);
void display(const string &blankword, const string& badguess);

int main()
{

	int word_count;
	int word2get;//what word within the file you get
	string word2guess; // the word in the file
	string badguess[BAD];

	char l2g;


	word_count = qtyOfWords();
	word2get = rand() % word_count;

	getWord(word2guess, word2get);

	cout << word2get << " " << word2guess << " " << word2guess.length() << endl;

	char * letterMatch = new char [word2guess.length()]; // dynamically create a c-string								


	cout<<"please enter your guess";
	cin>>l2g;


	display(letterMatch, badguess);

	addletter(letterMatch, badguess, word2guess, l2g);

	checkwinner(letterMatch,badguess);



	





	return 0;
}
/////////////////////////////////////////////////////////////////////////////////
bool getWord(string & s, int wtg)
{
	char ch;

	ifstream inFile(filename);

	if(inFile.fail())
		return false;

	for(int count = 0;count < wtg;)
	{
		 ch = inFile.get();
		 if(ch == '\n')
			 count++;
	}


	while(inFile.peek() != '\n')
	{
		s += inFile.get();
	}
	
	inFile.close();

	return true;
}
/////////////////////////////////////////////////////////////////////////////////
int qtyOfWords()
{
	int qty=0;
	char ch;
	ifstream inFile(filename);

	if(inFile.fail())
		return -1;

	while(!inFile.eof())
	{
		ch = inFile.get();

		if(ch == '\n')
			qty++;			
	}

	inFile.close();

	return qty;
}

////////////////////////////////////////////////
bool addletter(string &bw, string &bg, const string &wtg, char ch)

{
	int x;
	bool flag;
	for( x=0; x < wtg.length(); x++)
	{
		if(ch==wtg.at(x))
		{
			bw[x]=wtg[x];
			flag=true;
		}
	}

	if (flag)
		return false;
	for (x=0; x < bg.length(); x++)
	{
		if (bg[x]==ch)
			return true;
		if (bg[x]=='\0')
		{
			bg.append(bg);
			return false;
		}

	}
	return false;
}

///////////////////////////////////////////////////

int checkwinner( string &bw, string &bg)
{
	int x;
	bool contin=false;
	for(x=0; x<bw.length(); x++)
	{
		if( bw.at(x)=='_')
		{
			contin=true;
			break;
		}
	}
	if (!contin)
		return -1;
	contin= false;

	for(x=0; x<bw.length(); x++)
	{
			if (bw.at(x)=='_')
			{
				contin=true;
				break;
			}
	}

	if (!contin)
		return -2;

	return 0;

}

/////////////////////////////////////////////////////////
void display(const string &blankword, const string& badguess)
{
	int bodyparts=0,x;

	system("cls");

	for( x=0; x < badguess.length(); x++)
		if(badguess[x]!='_')
			bodyparts++;

		cout<<"\n __ Bad Letters";
		cout<<"\n|  | ";

		for(x=0; x<badguess.length(); x++)
			cout<< badguess[x] << " ";

		cout<<"\n "<<(bodyparts>0?"O":"")<<"  | ";
		cout<<"\n "<<(bodyparts>2?"/":"")<<(bodyparts>1?"|":"")<<(bodyparts>3?"\\":"")
			<<"  |  Current Word";
		cout<<"\n"<<(bodyparts>4?"/":"")<<""<<(bodyparts>5?"\\":"")
			<<"  | ";
		for(x=0; x<blankword.length(); x++)
			cout<<blankword[x]<<" ";
		cout<<"\n  | ";
		cout<<"\n _|_\n";
}



Is This A Good Question/Topic? 0
  • +

Replies To: hangman strings compiler error HELP

#2 rudy5453  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 02-December 07

Re: hangman strings compiler error HELP

Posted 14 March 2008 - 06:57 AM

I have updated the code however I feel that my error is in the lettermatch string
Was This Post Helpful? 0
  • +
  • -

#3 rudy5453  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 02-December 07

Re: hangman strings compiler error HELP

Posted 14 March 2008 - 07:28 AM

Ok so I fixed the fisrt probelm I think. My is pretty much done except it wont compile I still get the compiler error C2664 cannot convert whenever I use the dynamic array here is my updated code... I removed the answers too :)

#include <iostream>
#include <fstream>
#include <string>
#include <time.h>


using namespace std;

//global constants
const char filename[] = "words.dat";
const int BAD=6;


//function prototypes
int qtyOfWords();
bool getWord(string & s, int wtg);
bool addletter(string & bw, string & bg, const string & wtg, char ch);
int checkwinner( string & bw, string & bg);
void display(const string & blankword, const string & badguess);

int main()
{
	
char choice;
	do
	{		

		int word_count;
		int word2get;//what word within the file you get
		string word2guess; // the word in the file
		string badguess[BAD];

		char l2g;
		char tick;
		cout<<"\nThis is a game of hangman where you will get 6 chances to guess the word.\n";
		


		word_count = qtyOfWords();
		srand(clock()); //randomize rand()
		word2get = rand() % word_count + 1;

		cout<<"Enter a letter and press enter to continue\n";
		cin>>tick;//offset the random time

		getWord(word2guess, word2get);

		

		char * letterMatch = new char [word2guess.length()]; 
		
		do{


			cout<<"please enter your guess";
			cin>>l2g;


			display(letterMatch, *badguess);

			addletter(letterMatch, badguess, word2guess, l2g);

			checkwinner(letterMatch, badguess);

		}while(checkwinner(letterMatch, badguess)!=0);

		if(checkwinner(letterMatch,badguess)==-1)
			cout<<"\nYour a winner\n";

		if(checkwinner(letterMatch,badguess)==-2)
			cout<<"\nSorry you lost\n";


		cout<<"\nWould you like to play again? Y/N";
		cin>> choice;

	}while((choice=='Y')||(choice=='y'));							





	





	return 0;
}
/////////////////////////////////////////////////////////////////////////////////
bool getWord(string & s, int wtg)
{
	char ch;

	ifstream inFile(filename);

	if(inFile.fail())
		return false;

	for(int count = 0;count < wtg;)
	{
		 ch = inFile.get();
		 if(ch == '\n')
			 count++;
	}


	while(inFile.peek() != '\n')
	{
		s += inFile.get();
	}
	
	inFile.close();

	return true;
}
/////////////////////////////////////////////////////////////////////////////////
int qtyOfWords()
{
	int qty=0;
	char ch;
	ifstream inFile(filename);

	if(inFile.fail())
		return -1;

	while(!inFile.eof())
	{
		ch = inFile.get();

		if(ch == '\n')
			qty++;			
	}

	inFile.close();

	return qty;
}

////////////////////////////////////////////////
bool addletter(string &bw, string &bg, const string &wtg, char ch)

{
	int x;
	bool flag;
	for( x=0; x < wtg.length(); x++)
	{
		if(ch==wtg.at(x))
		{
			bw[x]=wtg[x];
			flag=true;
		}
	}

	if (flag)
		return false;
	for (x=0; x < bg.length(); x++)
	{
		if (bg[x]==ch)
			return true;
		if (bg[x]=='\0')
		{
			bg.append(bg);
			return false;
		}

	}
	return false;
}

///////////////////////////////////////////////////

int checkwinner( string &bw, string &bg)
{
	int x;
	bool contin=false;
	for(x=0; x<bw.length(); x++)
	{
		if( bw.at(x)=='_')
		{
			contin=true;
			break;
		}
	}
	if (!contin)
		return -1;

	contin= false;

	for(x=0; x<bg.length(); x++)
	{
			if (bg.at(x)=='_')
			{
				contin=true;
				break;
			}
	}

	if (!contin)
		return -2;

	return 0;

}

/////////////////////////////////////////////////////////
void display(const string &blankword, const string& badguess)
{
	int bodyparts=0,x;

	system("cls");

	for( x=0; x < badguess.length(); x++)
		if(badguess[x]!='_')
			bodyparts++;

		cout<<"\n __ Bad Letters";
		cout<<"\n|  | ";

		for(x=0; x<badguess.length(); x++)
			cout<< badguess[x] << " ";

		cout<<"\n "<<(bodyparts>0?"O":"")<<"  | ";
		cout<<"\n "<<(bodyparts>2?"/":"")<<(bodyparts>1?"|":"")<<(bodyparts>3?"\\":"")
			<<"  |  Current Word";
		cout<<"\n"<<(bodyparts>4?"/":"")<<""<<(bodyparts>5?"\\":"")
			<<"  | ";
		for(x=0; x<blankword.length(); x++)
			cout<<blankword[x]<<" ";
		cout<<"\n  | ";
		cout<<"\n _|_\n";
}



View Postrudy5453, on 14 Mar, 2008 - 07:26 AM, said:

Ok so I fixed the fisrt probelm I think. My is pretty much done except it wont compile I still get the compiler error C2664 cannot convert whenever I use the dynamic array here is my updated code... I removed the answers too :)

#include <iostream>
#include <fstream>
#include <string>
#include <time.h>


using namespace std;

//global constants
const char filename[] = "words.dat";
const int BAD=6;


//function prototypes
int qtyOfWords();
bool getWord(string & s, int wtg);
bool addletter(string & bw, string & bg, const string & wtg, char ch);
int checkwinner( string & bw, string & bg);
void display(const string & blankword, const string & badguess);

int main()
{
	
char choice;
	do
	{		

		int word_count;
		int word2get;//what word within the file you get
		string word2guess; // the word in the file
		string badguess[BAD];

		char l2g;
		char tick;
		cout<<"\nThis is a game of hangman where you will get 6 chances to guess the word.\n";
		


		word_count = qtyOfWords();
		srand(clock()); //randomize rand()
		word2get = rand() % word_count + 1;

		cout<<"Enter a letter and press enter to continue\n";
		cin>>tick;//offset the random time

		getWord(word2guess, word2get);

		

		char * letterMatch = new char [word2guess.length()]; 
		
		do{


			cout<<"please enter your guess";
			cin>>l2g;


			display(letterMatch, *badguess);

			addletter(letterMatch, badguess, word2guess, l2g);

			checkwinner(letterMatch, badguess);

		}while(checkwinner(letterMatch, badguess)!=0);

		if(checkwinner(letterMatch,badguess)==-1)
			cout<<"\nYour a winner\n";

		if(checkwinner(letterMatch,badguess)==-2)
			cout<<"\nSorry you lost\n";


		cout<<"\nWould you like to play again? Y/N";
		cin>> choice;

	}while((choice=='Y')||(choice=='y'));							





	





	return 0;
}
/////////////////////////////////////////////////////////////////////////////////
bool getWord(string & s, int wtg)
{
	char ch;

	ifstream inFile(filename);

	if(inFile.fail())
		return false;

	for(int count = 0;count < wtg;)
	{
		 ch = inFile.get();
		 if(ch == '\n')
			 count++;
	}


	while(inFile.peek() != '\n')
	{
		s += inFile.get();
	}
	
	inFile.close();

	return true;
}
/////////////////////////////////////////////////////////////////////////////////
int qtyOfWords()
{
	int qty=0;
	char ch;
	ifstream inFile(filename);

	if(inFile.fail())
		return -1;

	while(!inFile.eof())
	{
		ch = inFile.get();

		if(ch == '\n')
			qty++;			
	}

	inFile.close();

	return qty;
}

////////////////////////////////////////////////
bool addletter(string &bw, string &bg, const string &wtg, char ch)

{
	int x;
	bool flag;
	for( x=0; x < wtg.length(); x++)
	{
		if(ch==wtg.at(x))
		{
			bw[x]=wtg[x];
			flag=true;
		}
	}

	if (flag)
		return false;
	for (x=0; x < bg.length(); x++)
	{
		if (bg[x]==ch)
			return true;
		if (bg[x]=='\0')
		{
			bg.append(bg);
			return false;
		}

	}
	return false;
}

///////////////////////////////////////////////////

int checkwinner( string &bw, string &bg)
{
	int x;
	bool contin=false;
	for(x=0; x<bw.length(); x++)
	{
		if( bw.at(x)=='_')
		{
			contin=true;
			break;
		}
	}
	if (!contin)
		return -1;

	contin= false;

	for(x=0; x<bg.length(); x++)
	{
			if (bg.at(x)=='_')
			{
				contin=true;
				break;
			}
	}

	if (!contin)
		return -2;

	return 0;

}

/////////////////////////////////////////////////////////
void display(const string &blankword, const string& badguess)
{
	int bodyparts=0,x;

	system("cls");

	for( x=0; x < badguess.length(); x++)
		if(badguess[x]!='_')
			bodyparts++;

		cout<<"\n __ Bad Letters";
		cout<<"\n|  | ";

		for(x=0; x<badguess.length(); x++)
			cout<< badguess[x] << " ";

		cout<<"\n "<<(bodyparts>0?"O":"")<<"  | ";
		cout<<"\n "<<(bodyparts>2?"/":"")<<(bodyparts>1?"|":"")<<(bodyparts>3?"\\":"")
			<<"  |  Current Word";
		cout<<"\n"<<(bodyparts>4?"/":"")<<""<<(bodyparts>5?"\\":"")
			<<"  | ";
		for(x=0; x<blankword.length(); x++)
			cout<<blankword[x]<<" ";
		cout<<"\n  | ";
		cout<<"\n _|_\n";
}



I may have more compiler errors but they are all the same one with the same string.
Was This Post Helpful? 0
  • +
  • -

#4 AmitTheInfinity  Icon User is offline

  • C Surfing ∞
  • member icon

Reputation: 117
  • View blog
  • Posts: 1,559
  • Joined: 25-January 07

Re: hangman strings compiler error HELP

Posted 14 March 2008 - 07:53 AM

your lettermatch variable is actually a character array, whereas you have string as your first parameter for your function signature.
make your lettermatch variable as string and the error will go. It's just signature mismatch I guess. Change this and see if it works or gives few new problems. :)
Was This Post Helpful? 0
  • +
  • -

#5 rudy5453  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 02-December 07

Re: hangman strings compiler error HELP

Posted 14 March 2008 - 08:05 AM

View PostAmitTheInfinity, on 14 Mar, 2008 - 07:53 AM, said:

your lettermatch variable is actually a character array, whereas you have string as your first parameter for your function signature.
make your lettermatch variable as string and the error will go. It's just signature mismatch I guess. Change this and see if it works or gives few new problems. :)

what you just said confuses me. I suck at life... lol.
I understand where your getting the array from but i am lost on how to fix the problem...
Was This Post Helpful? 0
  • +
  • -

#6 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: hangman strings compiler error HELP

Posted 14 March 2008 - 08:10 AM

What he is saying is when you're declaring letterMatch you're declaring it as an array here


char * letterMatch = new char [word2guess.length()]; 



But in the signature of the method you're passing this to you are looking for a string, not an array


int checkwinner( string &bw, string &bg)
{

}



So, you either need to change the declaration of lettermatch to a string, or the first parameter in the signature of checkwinner to look for a char array.

Hope that helps :)
Was This Post Helpful? 0
  • +
  • -

#7 rudy5453  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 02-December 07

Re: hangman strings compiler error HELP

Posted 14 March 2008 - 08:20 AM

how would i convert the declaration of the array into a string?
I thought I was creating a dyamic string...
I am showing my lack of knoweldge here
Was This Post Helpful? 0
  • +
  • -

#8 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: hangman strings compiler error HELP

Posted 14 March 2008 - 08:45 AM

You can change your function to accept a char pointer as opposed to string object.
Was This Post Helpful? 0
  • +
  • -

#9 rudy5453  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 02-December 07

Re: hangman strings compiler error HELP

Posted 14 March 2008 - 08:50 AM

View Postrudy5453, on 14 Mar, 2008 - 08:20 AM, said:

how would i convert the declaration of the array into a string?
I thought I was creating a dyamic string...
I am showing my lack of knoweldge here

it didnt solve my problem but i did put in the delete for the dynamic array.

I still am trying to figure out how to convert letterMatch to a string


i really would like to pass the string into the function
Was This Post Helpful? 0
  • +
  • -

#10 rudy5453  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 02-December 07

Re: hangman strings compiler error HELP

Posted 14 March 2008 - 09:08 AM

I am having trouble trying to pass this into a function because i need to convert this dynamic array into a string. I am lost??

char * letterMatch = new char [word2guess.length()]; 



I originally thought i was creating a string. however i was not.
if you want to see the rest of the code message me
Was This Post Helpful? 0
  • +
  • -

#11 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: hangman strings compiler error HELP

Posted 14 March 2008 - 09:18 AM

Duplicate topics mereged. Please try to avoid duplicate topics.

I'm not sure I understand the problem here. Why are you not able to simply declare the variable as a string? Failing that, simply modify your function to take a char pointer instead of a string variable.
Was This Post Helpful? 0
  • +
  • -

#12 rudy5453  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 02-December 07

Re: hangman strings compiler error HELP

Posted 14 March 2008 - 09:22 AM

View PostAmadeus, on 14 Mar, 2008 - 09:18 AM, said:

Duplicate topics mereged. Please try to avoid duplicate topics.

I'm not sure I understand the problem here. Why are you not able to simply declare the variable as a string? Failing that, simply modify your function to take a char pointer instead of a string variable.

i guess my real problem here is that i do not know hoe to do that. It is a dynamic array but if i change the char to string it causes more problems... i am confused
Was This Post Helpful? 0
  • +
  • -

#13 rudy5453  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 02-December 07

Re: hangman strings compiler error HELP

Posted 14 March 2008 - 09:42 AM

/*******************************************************************************
Program:	 hangman.cpp
Author:	  Christopher Moore
Assignment#: 4
Date Due:	3/13/08
Purpose:	 This is a game of hangman using the string class.
********************************************************************************/

#include <iostream>
#include <fstream>
#include <string>
#include <time.h>


using namespace std;

//global constants
const char filename[] = "words.dat";
const int BAD=6;


//function prototypes
int qtyOfWords();
bool getWord(string & s, int wtg);
bool addletter(string & bw, string & bg, const string & wtg, char ch);
int checkwinner( string & bw, string & bg);
void display(const string & blankword, const string & badguess);

int main()
{
	
char choice;
	do
	{		

		int word_count;
		int word2get;//what word within the file you get
		string word2guess; // the word in the file
		string badguess[BAD];

		char l2g;
		char tick;
		cout<<"\nThis is a game of hangman where you will get 6 chances to guess the word.\n";
		


		word_count = qtyOfWords();
		srand(clock()); //randomize rand()
		word2get = rand() % word_count + 1;

		cout<<"Enter a letter and press enter to continue\n";
		cin>>tick;//offset the random time

		getWord(word2guess, word2get);

		

		string letterMatch = new char [word2guess.length()]; 

		
		do{


			cout<<"please enter your guess";
			cin>>l2g;


			display(letterMatch, *badguess);

			addletter(letterMatch, *badguess, word2guess, l2g);

			checkwinner(letterMatch, *badguess);

		}while(checkwinner(letterMatch, *badguess)!=0);

		if(checkwinner(letterMatch,*badguess)==-1)
			cout<<"\nYour a winner\n";

		if(checkwinner(letterMatch,*badguess)==-2)
			cout<<"\nSorry you lost\n";


		cout<<"\nWould you like to play again? Y/N";
		cin>> choice;


	}while((choice=='Y')||(choice=='y'));
	






	





	return 0;
}
/////////////////////////////////////////////////////////////////////////////////
bool getWord(string & s, int wtg)
{
	char ch;

	ifstream inFile(filename);

	if(inFile.fail())
		return false;

	for(int count = 0;count < wtg;)
	{
		 ch = inFile.get();
		 if(ch == '\n')
			 count++;
	}


	while(inFile.peek() != '\n')
	{
		s += inFile.get();
	}
	
	inFile.close();

	return true;
}
/////////////////////////////////////////////////////////////////////////////////
int qtyOfWords()
{
	int qty=0;
	char ch;
	ifstream inFile(filename);

	if(inFile.fail())
		return -1;

	while(!inFile.eof())
	{
		ch = inFile.get();

		if(ch == '\n')
			qty++;			
	}

	inFile.close();

	return qty;
}

////////////////////////////////////////////////
bool addletter(string &bw, string &bg, const string &wtg, char ch)

{
	int x;
	bool flag;
	for( x=0; x < wtg.length(); x++)
	{
		if(ch==wtg.at(x))
		{
			bw[x]=wtg[x];
			flag=true;
		}
	}

	if (flag)
		return false;
	for (x=0; x < bg.length(); x++)
	{
		if (bg[x]==ch)
			return true;
		if (bg[x]=='\0')
		{
			bg.append(bg);
			return false;
		}

	}
	return false;
}

///////////////////////////////////////////////////

int checkwinner( string &bw, string &bg)
{
	int x;
	bool contin=false;
	for(x=0; x<bw.length(); x++)
	{
		if( bw.at(x)=='_')
		{
			contin=true;
			break;
		}
	}
	if (!contin)
		return -1;

	contin= false;

	for(x=0; x<bg.length(); x++)
	{
			if (bg.at(x)=='_')
			{
				contin=true;
				break;
			}
	}

	if (!contin)
		return -2;

	return 0;

}

/////////////////////////////////////////////////////////
void display(const string &blankword, const string& badguess)
{
	int bodyparts=0,x;

	system("cls");

	for( x=0; x < badguess.length(); x++)
		if(badguess[x]!='_')
			bodyparts++;

		cout<<"\n __ Bad Letters";
		cout<<"\n|  | ";

		for(x=0; x<badguess.length(); x++)
			cout<< badguess[x] << " ";

		cout<<"\n "<<(bodyparts>0?"O":"")<<"  | ";
		cout<<"\n "<<(bodyparts>2?"/":"")<<(bodyparts>1?"|":"")<<(bodyparts>3?"\\":"")
			<<"  |  Current Word";
		cout<<"\n"<<(bodyparts>4?"/":"")<<""<<(bodyparts>5?"\\":"")
			<<"  | ";
		for(x=0; x<blankword.length(); x++)
			cout<<blankword[x]<<" ";
		cout<<"\n   | ";
		cout<<"\n  _|_\n";
}




new problem the game does not end or display the dead guy or the arrays filling up
Was This Post Helpful? 0
  • +
  • -

#14 letthecolorsrumble  Icon User is offline

  • Student of The Sun
  • member icon

Reputation: 27
  • View blog
  • Posts: 555
  • Joined: 07-November 07

Re: hangman strings compiler error HELP

Posted 14 March 2008 - 09:44 AM

#include <iostream>
#include <fstream>
#include <string>
#include <time.h>


using namespace std;

//global constants
const char filename[] = "words.dat";
const int BAD=6;


//function prototypes
int qtyOfWords();
bool getWord(string & s, int wtg);
bool addletter(string & bw, string & bg, const string & wtg, char ch);
int checkwinner( string & bw, string & bg);
void display(const string & blankword, const string & badguess);
int main()
{
    
char choice;
    do
    {        

        int word_count;
        int word2get;//what word within the file you get
        string word2guess; // the word in the file
        string badguess[BAD];
		
        char l2g;
        char tick;
        cout<<"\nThis is a game of hangman where you will get 6 chances to guess the word.\n";
        


        word_count = qtyOfWords();
        srand(clock()); //randomize rand()
        word2get = rand() % word_count + 1;

        cout<<"Enter a letter and press enter to continue\n";
        cin>>tick;//offset the random time

        getWord(word2guess, word2get);

        string letterMatch;

        
        
        do{


            cout<<"please enter your guess";
            cin>>l2g;


            display(letterMatch, *badguess);

            addletter(letterMatch, *badguess, word2guess, l2g);

            checkwinner(letterMatch, *badguess);

        }while(checkwinner(letterMatch, *badguess)!=0);

        if(checkwinner(letterMatch,*badguess)==-1)
            cout<<"\nYour a winner\n";

        if(checkwinner(letterMatch,*badguess)==-2)
            cout<<"\nSorry you lost\n";


        cout<<"\nWould you like to play again? Y/N";
        cin>> choice;

    }while((choice=='Y')||(choice=='y'));                            





    





    return 0;
}
/////////////////////////////////////////////////////////////////////////////////
bool getWord(string & s, int wtg)
{
    char ch;

    ifstream inFile(filename);

    if(inFile.fail())
        return false;

    for(int count = 0;count < wtg;)
    {
         ch = inFile.get();
         if(ch == '\n')
             count++;
    }


    while(inFile.peek() != '\n')
    {
        s += inFile.get();
    }
    
    inFile.close();

    return true;
}
/////////////////////////////////////////////////////////////////////////////////
int qtyOfWords()
{
    int qty=0;
    char ch;
    ifstream inFile(filename);

    if(inFile.fail())
        return -1;

    while(!inFile.eof())
    {
        ch = inFile.get();

        if(ch == '\n')
            qty++;            
    }

    inFile.close();

    return qty;
}

////////////////////////////////////////////////
bool addletter(string &bw, string &bg, const string &wtg, char ch)

{
    int x;
    bool flag;
    for( x=0; x < wtg.length(); x++)
    {
        if(ch==wtg.at(x))
        {
            bw[x]=wtg[x];
            flag=true;
        }
    }

    if (flag)
        return false;
    for (x=0; x < bg.length(); x++)
    {
        if (bg[x]==ch)
            return true;
        if (bg[x]=='\0')
        {
            bg.append(bg);
            return false;
        }

    }
    return false;
}

///////////////////////////////////////////////////

int checkwinner( string &bw, string &bg)
{
    int x;
    bool contin=false;
    for(x=0; x<bw.length(); x++)
    {
        if( bw.at(x)=='_')
        {
            contin=true;
            break;
        }
    }
    if (!contin)
        return -1;

    contin= false;

    for(x=0; x<bg.length(); x++)
    {
            if (bg.at(x)=='_')
            {
                contin=true;
                break;
            }
    }

    if (!contin)
        return -2;

    return 0;

}

/////////////////////////////////////////////////////////
void display(const string &blankword, const string& badguess)
{
    int bodyparts=0,x;

    system("cls");

    for( x=0; x < badguess.length(); x++)
        if(badguess[x]!='_')
            bodyparts++;

        cout<<"\n __ Bad Letters";
        cout<<"\n|  | ";

        for(x=0; x<badguess.length(); x++)
            cout<< badguess[x] << " ";

        cout<<"\n "<<(bodyparts>0?"O":"")<<"  | ";
        cout<<"\n "<<(bodyparts>2?"/":"")<<(bodyparts>1?"|":"")<<(bodyparts>3?"\\":"")
            <<"  |  Current Word";
        cout<<"\n"<<(bodyparts>4?"/":"")<<""<<(bodyparts>5?"\\":"")
            <<"  | ";
        for(x=0; x<blankword.length(); x++)
            cout<<blankword[x]<<" ";
        cout<<"\n  | ";
        cout<<"\n _|_\n";
}



This code compiles and runs, but you need to do some further changes , I have modified to char* letterMatch to string letterMatch. The badguess array had to be passed to the functions correctly.


And I see you have the same code now. :)

This post has been edited by letthecolorsrumble: 14 March 2008 - 09:48 AM

Was This Post Helpful? 0
  • +
  • -

#15 rudy5453  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 02-December 07

Re: hangman strings compiler error HELP

Posted 14 March 2008 - 10:11 AM

I have fixed some of the display issues still the game does not terminiate or show the bad guesses

#include <iostream>
#include <fstream>
#include <string>
#include <time.h>


using namespace std;

//global constants
const char filename[] = "words.dat";
const int BAD=6;


//function prototypes
int qtyOfWords();
bool getWord(string & s, int wtg);
bool addletter(string & bw, string & bg, const string & wtg, char ch);
int checkwinner( string & bw, string & bg);
void display(const string & blankword, const string & badguess);

int main()
{
	
char choice;
	do
	{		

		int word_count;
		int word2get;//what word within the file you get
		string word2guess; // the word in the file
		string badguess[BAD];

		char l2g;
	
		cout<<"\nThis is a game of hangman where you will get 6 chances to guess the word.\n";
		


		word_count = qtyOfWords();
		srand(clock()); //randomize rand()
		word2get = rand() % word_count;
		

		getWord(word2guess, word2get);

		

		string letterMatch = new char [word2guess.length()]; 

		
		do{
			
			display(letterMatch, *badguess);


			cout<<"please enter your guess";
			cin>>l2g;

			addletter(letterMatch, *badguess, word2guess, l2g);

			checkwinner(letterMatch, *badguess);

		}while(checkwinner(letterMatch, *badguess)!=0);

		if(checkwinner(letterMatch,*badguess)==-1)
			cout<<"\nYour a winner\n";

		if(checkwinner(letterMatch,*badguess)==-2)
			cout<<"\nSorry you lost\n";


		cout<<"\nWould you like to play again? Y/N";
		cin>> choice;


	}while((choice=='Y')||(choice=='y'));
	






	





	return 0;
}
/////////////////////////////////////////////////////////////////////////////////
bool getWord(string & s, int wtg)
{
	char ch;

	ifstream inFile(filename);

	if(inFile.fail())
		return false;

	for(int count = 0;count < wtg;)
	{
		 ch = inFile.get();
		 if(ch == '\n')
			 count++;
	}


	while(inFile.peek() != '\n')
	{
		s += inFile.get();
	}
	
	inFile.close();

	return true;
}
/////////////////////////////////////////////////////////////////////////////////
int qtyOfWords()
{
	int qty=0;
	char ch;
	ifstream inFile(filename);

	if(inFile.fail())
		return -1;

	while(!inFile.eof())
	{
		ch = inFile.get();

		if(ch == '\n')
			qty++;			
	}

	inFile.close();

	return qty;
}

////////////////////////////////////////////////
bool addletter(string &bw, string &bg, const string &wtg, char ch)

{
	int x;
	bool flag;
	for( x=0; x < wtg.length(); x++)
	{
		if(ch==wtg.at(x))
		{
			bw[x]=wtg[x];
			flag=true;
		}
	}

	if (flag)
		return false;
	for (x=0; x < bg.length(); x++)
	{
		if (bg[x]==ch)
			return true;
		if (bg[x]=='\0')
		{
			bg.append(bg);
			return false;
		}

	}
	return false;
}

///////////////////////////////////////////////////

int checkwinner( string &bw, string &bg)
{
	int x;
	bool contin=false;
	for(x=0; x<bw.length(); x++)
	{
		if( bw.at(x)=='_')
		{
			contin=true;
			break;
		}
	}
	if (!contin)
		return -1;

	contin= false;

	for(x=0; x<bg.length(); x++)
	{
			if (bg.at(x)=='_')
			{
				contin=true;
				break;
			}
	}

	if (!contin)
		return -2;

	return 0;

}

/////////////////////////////////////////////////////////
void display(const string &blankword, const string& badguess)
{
	int bodyparts=0,x;

	system("cls");

	for( x=0; x < badguess.length(); x++)
		if(badguess[x]!='_')
			bodyparts++;

		cout<<"\n  __ Bad Letters";
		cout<<"\n |  | ";

		for(x=0; x<badguess.length(); x++)
			cout<< badguess[x] << " ";

		cout<<"\n "<<(bodyparts>0?"O":"")<<"   | ";
		cout<<"\n "<<(bodyparts>2?"/":"")<<(bodyparts>1?"|":"")<<(bodyparts>3?"\\":"")
			<<"   |  Current Word";
		cout<<"\n"<<(bodyparts>4?"/":"")<<""<<(bodyparts>5?"\\":"")
			<<"	| ";
		for(x=0; x<blankword.length(); x++)
			cout<<blankword[x]<<" ";
		cout<<"\n	| ";
		cout<<"\n   _|_\n";
}



Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2