I need some help keeping track of letters the user inputs. With my current code it only outputs one letter at a time of the word to be guessed. Can anyone help?

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

using namespace std;

void Hangman();
void LeftArm();
void Head();
void RightArm();
void Body();
void LeftLeg();
void Gallows(int count);
void DisplayAWord(string guessword);
void revealword(char letter, int position,string fileword, string guessword);
void secret(string fileword);
void hidden (string fileword);
char Question ();

int main()
{

srand(time(NULL));
    int x = rand()%10 +1;
    int count = 0;
    int position;
    char letter;
    ifstream words;
    string guessword, fileword;



    words.open("words.txt");
    do{
    words>> fileword;}
    while ( --x >= 0 );




    secret(fileword);


    while (guessword != fileword && count < 6){

    letter = Question();
    position = fileword.find(letter);

    if (position > fileword.length()){
        cout<<"There is no "<<letter<< "\n";
        count++;
        Gallows(count);}
    else  {
    cout<< letter << " is in the "<<position<<"th place\n";
    string guessword;
    revealword(letter, position, fileword, guessword);
    DisplayAWord(guessword);
    }

    }

     if (count >= 6)
        cout<<"Gameover man!\n";
        cout<<"The word was "<<fileword<< ".\n";
        false;


return 0;
}
void LeftArm()
{
    cout  << setw(8) <<"____" << endl;   
    cout  << setw(4) <<"|" << setw (5) << "}" << endl;
    cout  << setw(4) <<"|" << setw(4) << "\\" << endl;  
    cout  << setw(4) <<"|" << endl;
    cout  << setw(4) <<"|" << endl;       
    cout  <<"___|________" << endl;
}
void Head()
{
    cout  << setw(8) <<"____" << endl;   
    cout  << setw(4) <<"|" << setw (5) << "}" << endl;
    cout  << setw(4) <<"|" << setw(5) << "\\0" << endl;  
    cout  << setw(4) <<"|" << endl;
    cout  << setw(4) <<"|" << endl;
    cout  <<"___|________" << endl;
}
void RightArm()
{
    cout  << setw(8) <<"____" << endl;
    cout  << setw(4) <<"|" << setw (5) << "}" << endl;
    cout  << setw(4) <<"|" << setw(6) << "\\0/" << endl;
    cout  << setw(4) <<"|" << endl;
    cout  << setw(4) <<"|" << endl;
    cout  <<"___|________" << endl;
}
void Body()
{
    cout  << setw(8) <<"____" << endl;
    cout  << setw(4) <<"|" << setw (5) << "}" << endl;
    cout  << setw(4) <<"|" << setw(6) << "\\0/" << endl;
    cout  << setw(4) << "|" << setw(5) << "|" << endl;
    cout  << setw(4) <<"|" << endl; 
    cout  <<"___|________" << endl;
}
void LeftLeg()
{
    cout  << setw(8) <<"____" << endl;
    cout  << setw(4) <<"|" << setw (5) << "}" << endl;
    cout  << setw(4) <<"|" << setw(6) << "\\0/" << endl;
    cout  << setw(4) << "|" << setw(5) << "|" << endl;
    cout  << setw(4) << "|" << setw(4) << "/" << endl;
    cout  <<"___|________" << endl;
}
void Hangman()
{
    cout  << setw(8) <<"____" << endl;
    cout  << setw(4) <<"|" << setw (5) << "}" << endl;   
    cout  << setw(4) <<"|" << setw(6) << "\\0/" << endl; 
    cout  << setw(4) << "|" << setw(5) << "|" << endl;
    cout  << setw(4) << "|" << setw(6) << "/ \\" << endl;
    cout  <<"___|________" << endl;
}
void Gallows(int count)
{
    if (count == 1)
         LeftArm();
    else if (count == 2)
         Head();
    else if (count == 3)
        RightArm();
    else if (count == 4)
        Body();
    else if (count == 5)
        LeftLeg();
    else if (count == 6)
        Hangman();
}
void secret(string fileword)                                                          
{ 
    int i = 0; 
    int x;     
    string secret;
    secret = fileword; 
    x = secret.length();

cout<<"The word is ";
    while (i < x)
   {
     secret[i] ='_';
     cout<<secret[i]<<" ";
     i++; 
    } 
     cout<<endl;

}
char Question ()                                                             
{ 
    char letter;
    cout<<"What letter would you like to guess? ";
    cin>>letter;
    return letter;
}
void DisplayAWord(string guessword)                                                              
{ 
    cout << "The word is "<<guessword<< endl; 
}
void revealword(char letter,int position,string fileword, string guessword)
{

    int i = 0;
    int count = 0;
    int x;     
    string secret;
    secret = fileword; 
    x = secret.length();


    while (i < x)
   {
     secret[i] ='_';
     i++; 
    } 


    guessword = secret;


    if(position==0)
        guessword[position]=letter;
    else if(position==1)
        guessword[position]=letter;
    else if(position==2)
        guessword[position]=letter;
    else if (position==3)
        guessword[position]=letter;
    else if (position==4)
        guessword[position]=letter;
    else if (position==5)
        guessword[position]=letter;

    cout<<guessword[0]<<" "<<guessword[1]<<" "<<guessword[2]<<" "<<guessword[3]<<" "<<guessword[4]<<" "<<guessword[5]<<" "<<endl;



}

The problem, I think is in the revealword function? Please help!!!!

Recommended Answers

All 14 Replies

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

using namespace std;

void Hangman();
void LeftArm();
void Head();
void RightArm();
void Body();
void LeftLeg();
void Gallows(int count);
void DisplayAWord(string guessword);
void revealword(char letter, int position,string fileword, string guessword);
void secret(string fileword);
void hidden (string fileword);
char Question ();

int main()
{

srand(time(NULL));
int x = rand()%10 +1;
int count = 0;
int position;
char letter;
ifstream words;
string guessword, fileword;



words.open("words.txt");
do{
words>> fileword;}
while ( --x >= 0 );




secret(fileword);


while (guessword != fileword && count < 6){

letter = Question();
position = fileword.find(letter);

if (position > fileword.length()){
cout<<"There is no "<<letter<< "\n";
count++;
Gallows(count);}
else {
cout<< letter << " is in the "<<position<<"th place\n";
string guessword;
revealword(letter, position, fileword, guessword);
DisplayAWord(guessword);
}

}

if (count >= 6)
cout<<"Gameover man!\n";
cout<<"The word was "<<fileword<< ".\n";
false;


return 0;
}
void LeftArm()
{
cout << setw(8) <<"____" << endl;
cout << setw(4) <<"|" << setw (5) << "}" << endl;
cout << setw(4) <<"|" << setw(4) << "\\" << endl;
cout << setw(4) <<"|" << endl;
cout << setw(4) <<"|" << endl;
cout <<"___|________" << endl;
}
void Head()
{
cout << setw(8) <<"____" << endl;
cout << setw(4) <<"|" << setw (5) << "}" << endl;
cout << setw(4) <<"|" << setw(5) << "\\0" << endl;
cout << setw(4) <<"|" << endl;
cout << setw(4) <<"|" << endl;
cout <<"___|________" << endl;
}
void RightArm()
{
cout << setw(8) <<"____" << endl;
cout << setw(4) <<"|" << setw (5) << "}" << endl;
cout << setw(4) <<"|" << setw(6) << "\\0/" << endl;
cout << setw(4) <<"|" << endl;
cout << setw(4) <<"|" << endl;
cout <<"___|________" << endl;
}
void Body()
{
cout << setw(8) <<"____" << endl;
cout << setw(4) <<"|" << setw (5) << "}" << endl;
cout << setw(4) <<"|" << setw(6) << "\\0/" << endl;
cout << setw(4) << "|" << setw(5) << "|" << endl;
cout << setw(4) <<"|" << endl;
cout <<"___|________" << endl;
}
void LeftLeg()
{
cout << setw(8) <<"____" << endl;
cout << setw(4) <<"|" << setw (5) << "}" << endl;
cout << setw(4) <<"|" << setw(6) << "\\0/" << endl;
cout << setw(4) << "|" << setw(5) << "|" << endl;
cout << setw(4) << "|" << setw(4) << "/" << endl;
cout <<"___|________" << endl;
}
void Hangman()
{
cout << setw(8) <<"____" << endl;
cout << setw(4) <<"|" << setw (5) << "}" << endl;
cout << setw(4) <<"|" << setw(6) << "\\0/" << endl;
cout << setw(4) << "|" << setw(5) << "|" << endl;
cout << setw(4) << "|" << setw(6) << "/ \\" << endl;
cout <<"___|________" << endl;
}
void Gallows(int count)
{
if (count == 1)
LeftArm();
else if (count == 2)
Head();
else if (count == 3)
RightArm();
else if (count == 4)
Body();
else if (count == 5)
LeftLeg();
else if (count == 6)
Hangman();
}
void secret(string fileword)
{
int i = 0;
int x;
string secret;
secret = fileword;
x = secret.length();

cout<<"The word is ";
while (i < x)
{
secret[i] ='_';
cout<<secret[i]<<" ";
i++;
}
cout<<endl;

}
char Question ()
{
char letter;
cout<<"What letter would you like to guess? ";
cin>>letter;
return letter;
}
void DisplayAWord(string guessword)
{
cout << "The word is "<<guessword<< endl;
}
void revealword(char letter,int position,string fileword, string guessword)
{

int i = 0;
int count = 0;
int x;
string secret;
secret = fileword;
x = secret.length();


while (i < x)
{
secret[i] ='_';
i++;
}


guessword = secret;


if(position==0)
guessword[position]=letter;
else if(position==1)
guessword[position]=letter;
else if(position==2)
guessword[position]=letter;
else if (position==3)
guessword[position]=letter;
else if (position==4)
guessword[position]=letter;
else if (position==5)
guessword[position]=letter;

cout<<guessword[0]<<" "<<guessword[1]<<" "<<guessword[2]<<" "<<guessword[3]<<" "<<guessword[4]<<" "<<guessword[5]<<" "<<endl;



}

char Question ()
{
char letter;
cout<<"What letter would you like to guess? ";
cin>>letter;
return letter;
}

Instead of using only a locally-scoped letter, why not have an array of letters in your global scope? For example...

//Somewhere under the header, near the namespace declaration...

char[27] guessedChars = {0};

Now you'll have a fully null-terminated char array capable of holding 26 characters from index 0-26. I say 26 instead of 27 because the last should be reserved for a final null terminator.

What you'll want to do now is use a temporary char as the "receiver" for user input then iterate through your char array until you reach null. If you don't find a matching character in your char array before reaching null, place a copy of the char entered in the guessedChars array. If the value already exists you should warn the user and tell them that they've already guessed that character (or simply just penalize them hangman style).

I dont know how to do arrays yet, so I was hoping there was a way to fix it as is? I dont think the problem is that big but I just cannot figure it out. Any other suggestions?

What is words.txt? Is it just a text file with ten random words from the dictionary?

Yes it is just 10 words.

I'd like to apologize for my previous post- I meant to place the array subscript next to the variable name and not the type.

Switching back and forth between C++ and Java today isn't a good thing @_@

//Somewhere under the header, near the namespace declaration...

char guessedChars[27] = {0}; //correct syntax

In this code:

else {
cout<< letter << " is in the "<<position<<"th place\n";
string guessword;
revealword(letter, position, fileword, guessword);
DisplayAWord(guessword);
}

you are creating a brand new variable named guessword for every correct guess. So guessword is an empty string when it is created. You are passing this empty string to the revealword function. guessword is is NOT passed by reference to revealword, so it remains empty after you return from revealword. You are then passing an empty string to the function DisplayAWord. I am guessing that you want to create guessword OUTSIDE of the else block and/or pass it to revealword by reference (is guessword supposed to be changed in revealword?).

Ok I put in an ambersand but you also said to take it outside of the else...could you show me? Or explain a bit more?

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


using namespace std;

void Hangman();
void LeftArm();
void Head();
void RightArm();
void Body();
void LeftLeg();
void Gallows(int count);
void DisplayAWord(string guessword);
void revealword(char letter, int position,string fileword, string& guessword);
void secret(string fileword);
void hidden (string fileword);
char Question ();






int main()
{
	
	srand(time(NULL));
	int x = rand()%10 +1;
	int count = 0;
	int position;
	char letter;
	ifstream words;
	string guessword, fileword;

	
	
	words.open("words.txt");
	do{
	words>> fileword;}
	while ( --x >= 0 );
	
		//cout<<fileword;
	
	
	secret(fileword);
	
  
	while (guessword != fileword && count < 6){
	
	letter = Question();
	position = fileword.find(letter);

	if (position > fileword.length()){
		cout<<"There is no "<<letter<< "\n";
		count++;
        Gallows(count);}
	else  {
    cout<< letter << " is in the "<<position<<"th place\n";
	string guessword;
	revealword(letter, position, fileword, guessword);
	DisplayAWord(guessword);
	}
	
	}

	 if (count >= 6)
		cout<<"Gameover man!\n";
		cout<<"The word was "<<fileword<< ".\n";
		false;


return 0;
}
void LeftArm()
{
	cout  << setw(8) <<"____" << endl;   
	cout  << setw(4) <<"|" << setw (5) << "}" << endl;
	cout  << setw(4) <<"|" << setw(4) << "\\" << endl;  
	cout  << setw(4) <<"|" << endl;
	cout  << setw(4) <<"|" << endl;       
	cout  <<"___|________" << endl;
}
void Head()
{
	cout  << setw(8) <<"____" << endl;   
	cout  << setw(4) <<"|" << setw (5) << "}" << endl;
	cout  << setw(4) <<"|" << setw(5) << "\\0" << endl;  
	cout  << setw(4) <<"|" << endl;
	cout  << setw(4) <<"|" << endl;
	cout  <<"___|________" << endl;
}
void RightArm()
{
	cout  << setw(8) <<"____" << endl;
	cout  << setw(4) <<"|" << setw (5) << "}" << endl;
	cout  << setw(4) <<"|" << setw(6) << "\\0/" << endl;
	cout  << setw(4) <<"|" << endl;
	cout  << setw(4) <<"|" << endl;
	cout  <<"___|________" << endl;
}
void Body()
{
	cout  << setw(8) <<"____" << endl;
	cout  << setw(4) <<"|" << setw (5) << "}" << endl;
	cout  << setw(4) <<"|" << setw(6) << "\\0/" << endl;
	cout  << setw(4) << "|" << setw(5) << "|" << endl;
	cout  << setw(4) <<"|" << endl; 
	cout  <<"___|________" << endl;
}
void LeftLeg()
{
	cout  << setw(8) <<"____" << endl;
	cout  << setw(4) <<"|" << setw (5) << "}" << endl;
	cout  << setw(4) <<"|" << setw(6) << "\\0/" << endl;
	cout  << setw(4) << "|" << setw(5) << "|" << endl;
	cout  << setw(4) << "|" << setw(4) << "/" << endl;
	cout  <<"___|________" << endl;
}
void Hangman()
{
	cout  << setw(8) <<"____" << endl;
	cout  << setw(4) <<"|" << setw (5) << "}" << endl;   
	cout  << setw(4) <<"|" << setw(6) << "\\0/" << endl; 
	cout  << setw(4) << "|" << setw(5) << "|" << endl;
	cout  << setw(4) << "|" << setw(6) << "/ \\" << endl;
	cout  <<"___|________" << endl;
}
void Gallows(int count)
{
	if (count == 1)
		 LeftArm();
	else if (count == 2)
		 Head();
	else if (count == 3)
		RightArm();
	else if (count == 4)
		Body();
	else if (count == 5)
		LeftLeg();
	else if (count == 6)
		Hangman();
}
void secret(string fileword)                                                          
{ 
    int i = 0; 
    int x;     
    string secret;
    secret = fileword; 
    x = secret.length();
	
cout<<"The word is ";
	while (i < x)
   {
     secret[i] ='_';
	 cout<<secret[i]<<" ";
     i++; 
    } 
	 cout<<endl;

}
char Question ()                                                             
{ 
	char letter;
    cout<<"What letter would you like to guess? ";
	cin>>letter;
	return letter;
}
void DisplayAWord(string guessword)                                                              
{ 
    cout << "The word is "<<guessword<< endl; 
}
void revealword(char letter,int position,string fileword, string& guessword)
{

	int i = 0;
	int count = 0;
	int x;     
    string secret;
    secret = fileword; 
    x = secret.length();

	
	while (i < x)
   {
     secret[i] ='_';
     i++; 
    } 
	
	
	guessword = secret;
	

	if(position==0)
		guessword[position]=letter;
	else if(position==1)
		guessword[position]=letter;
	else if(position==2)
		guessword[position]=letter;
	else if (position==3)
		guessword[position]=letter;
	else if (position==4)
		guessword[position]=letter;
	else if (position==5)
		guessword[position]=letter;
	
	cout<<guessword[0]<<" "<<guessword[1]<<" "<<guessword[2]<<" "<<guessword[3]<<" "<<guessword[4]<<" "<<guessword[5]<<" "<<endl;
	

	
}

You already have it declared outside of the else in this line:

string guessword, fileword;

Just delete the guessword declaration line (line 3) within the else block:

else  {
    cout<< letter << " is in the "<<position<<"th place\n";
	string guessword;  // delete this line
	revealword(letter, position, fileword, guessword);
	
	cout << "?" << guessword << "?\n";
	
	DisplayAWord(guessword);
	}

What exactly is guessword supposed to contain after each correct guess and what exactly is the revealword function supposed to do, particularly as it pertains to changing guessword?

Say we have the word "vista" - vista gets stringed in through fileword as "_ _ _ _ _" - same number of letters to dashes, also letter is in to see if it is in fileword, if it is then guessword will cout which eve position the letter guessed is in. "V _ _ _ _". Atleast thats how its suppose to work.

Say we have the word "vista" - vista gets stringed in through fileword as "_ _ _ _ _" - same number of letters to dashes, also letter is in to see if it is in fileword, if it is then guessword will cout which eve position the letter guessed is in. "V _ _ _ _". Atleast thats how its suppose to work.

O.K. but what if they then guess 't'? Should guessword, before the call to revealword, be this:

v____

and after the call be this?:

v__t_

Correct, if they guess "v" the first time the output should be v _ _ _ _
Then if they guess "t" the second time the ouput should be v _ _ t _
Then the third guess "i" then the output should be vi _ t _
until they get the word all the way.

Right now if a user inputs "v" the first time, then the output is v _ _ _ _
then the second time they input "t" the output is _ _ _ t _
and it needs to be v _ _ t _
it needs to keep the letters

void revealword(char letter,int position,string fileword, string& guessword)
{

	int i = 0;
	int count = 0;
	int x;     
    string secret;
    secret = fileword; 
    x = secret.length();

	
	while (i < x)
   {
     secret[i] ='_';
     i++; 
    } 
	
	
	guessword = secret;
	

	if(position==0)
		guessword[position]=letter;
	else if(position==1)
		guessword[position]=letter;
	else if(position==2)
		guessword[position]=letter;
	else if (position==3)
		guessword[position]=letter;
	else if (position==4)
		guessword[position]=letter;
	else if (position==5)
		guessword[position]=letter;
	
	cout<<guessword[0]<<" "<<guessword[1]<<" "<<guessword[2]<<" "<<guessword[3]<<" "<<guessword[4]<<" "<<guessword[5]<<" "<<endl;
	

	
}

Your problem is in lines 12 through 19. You are initializing secret to all underscores in lines 12 - 16, then assigning those underscores to guessword in line 19. Anything that used to be in guessword before has been wiped out here. Thus the 'v' that was in guessword before is now an underscore. This is the same problem you had before when you had created guessword in the else statement before you deleted that line. You are initializing it every time you get a correct letter the way you have it. You need to initialize it ONCE. After you have picked fileword, but before you start guessing, you want to initialize guessword to all underscores. Then delete lines 3 - 19. You don't need them. While you are at it, you don't need any of those if-else if statements since you do the exact same thing regardless of the value of position. Also, you are hardcoding 0 through 5 here. How do you know the word will be exactly six letters long? Ditto with line 35. It can be replaced simply with this:

cout << guessword << endl;

So initialize guessword to all underscores in the MAIN function after you assign a word to fileword in main(). Take that code out of the revealword function. You'll have to change that code slightly when you put it in main() in order to initialize guessword correctly.

Thanks that did it!! Really appreciate all the help.

Thanks that did it!! Really appreciate all the help.

You're welcome. On a separate note, have you tried out the program on any words that have the same letter more than once, like "yellow"?

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.