I need to make a hangman game but I have code but it won't end the game even if you ges the word, and it won't show the gesses all at once.
This is what I have

#include <iostream>
using namespace std;

int main()
{
	char solution[20];	
	char blank[20];		
	int counter = 0;	
	int right = 0;		
	char guess;

	cout<<"Enter phrase 20 chars or less."<<endl;
	cin.getline(solution, 20);
	int puzzLength = strlen(solution);			



		
	for (counter = 0; counter < puzzLength; counter++)
               {
		solution[counter] = toupper(solution[counter]);
	}
	
		
	

	
	for (counter = 0; counter < puzzLength; counter++) {		
		if (isalnum(solution[counter])) blank[counter] = '*';
		else blank[counter] = solution[counter];
		
}	

	while (strcmp(solution, blank))  
	{
		cout<<endl<<"Solution phrase is: "<<solution<<"."<<endl;
		cout<<"The current 'blank' puzzle is: "<<blank<<"."<<endl;
		cout<<"Enter a guess."<<endl;
		cin>>guess;
		guess = toupper(guess);


		
		for (counter = 0; counter <= puzzLength; counter++) 
		{
				if (guess == solution[counter])  
				{
					blank[counter] = guess;				
				}
	
		}			
	}	
	cout<<"Winner!";//Ican't get to this
	cin.get();
	return 0;
}

Recommended Answers

All 13 Replies

Ok. Where's the camera? Or am I just going insane?
This is yet again the exact same code as
http://www.daniweb.com/forums/thread128043.html
http://www.daniweb.com/forums/thread128037.html

Are you three in the same class or something? Or are you all the same person?

But to solve your problem, put this: strcpy_s(blank, solution); right after

for (counter = 0; counter < puzzLength; counter++)
{
solution[counter] = toupper(solution[counter]);
}

That way you all have the exact same code and your teacher will fail you for sure

As an aside, you use the C string and character functions, but forgot to include <cstring> and <cctype>.

> cout<<"Winner!";//Ican't get to this
When Edward runs the program, the problem shows up in the output for blank. You didn't null terminate the string, so strcmp is failing. That's easy to fix by changing this:

char blank[20];

To this:

char blank[20] = {0};

What that does is initializes every character in blank to null. That way as long as the phrase is less than 20 characters the string is always null terminated.

Thanks, but last time, how do I get it to show all the letters used.

thanks for stealing my code

commented: 'your code' .... -1

[edit] Read my later replies..

I changed it to

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

void viewWords();
void addWord();
void Menu();
void game();
void deleteWord();


int main()
{
    // run menu program
    Menu();
    
    return 0;
}



//this is were the menu code will be
void Menu()
{
    char option;
    option = 'n';
    // while  loop untill quit is selected

    while (option != 'q')
    {

    cout << "##  ##    ##   ##    #######\n";
    cout << "##  ##   ####  ####  ##    #\n";
    cout << "##  ##  ###### ## ## ##     \n";
    cout << "######  ##  ## ## ## ##     \n";
    cout << "######  ##  ## ## ## ##     \n";
    cout << "##  ##  ###### ## ## ##  ###\n";
    cout << "##  ##  ##  ## ## ## ##   ##\n";
    cout << "##  ##  ##  ## ## ## #######\n";
    cout << "\n";
    cout << " ##   ##  ####  ##   \n";
    cout << " ### ### ###### #### \n";
    cout << " ####### ##  ## ## ##\n";
    cout << " ##   ## ##  ## ## ##\n";
    cout << " ##   ## ###### ## ##\n";
    cout << " ##   ## ##  ## ## ##\n";
    cout << " ##   ## ##  ## ## ##\n";
    
    cout << "Please select from the options below:\n";
    cout << "P:lay Game\n";
    cout << "A:dd Words\n";
    cout << "D:elete Words\n";
    cout << "V:iew Words\n";
    cout << "Q:uit\n";
    cin  >> option;
    
    /*
    void Menu() 
    { char option; 
    cout << "Please select from the options below:\n"; 
    cout << "P:lay Game\n";
    cout << "A:dd Words\n"; 
    cout << "D:elete Words\n"; 
    cout << "V:iew Words\n";
    cout << "Q:uit\n"; 
    cin >> option; 
    
    // while loop untill quit is selected while (option != 'q')
    */


            switch (option)
    {
        case 'P':
                game();
                break;

        case 'p':
                game();
                break;

        case 'A':
                
                addWord();
                break;

        case 'a':
                
                addWord();
                break;

        case 'D':
                deleteWord();
                break;

        case 'd':
                deleteWord();
                break;

        case 'V':
                
                viewWords();
                break;

        case 'v':
                
                viewWords();
                break;


        case 'Q':
                cout << "Quiting\n";
                break;

        case 'q':
                cout << "Quiting\n";
                break;

        default:
            cout << "Sorry that is not an option. ";
            
        }
    }
}


void addWord()
              // Program needs to be able to read the word and only add it if it does not already exist.

{
    //clear screen
    system("CLS");
        // opening file to be used    
    ofstream fout;
    fout.open("library.txt", ios::app);

    char addedWord[25];
    char level; // label for word to determine Easy, Medium or Hard.
    
    
    
        // put in just incase something has happened to the library file the user will be notified.
            if (!fout.good())
            {
                cout << "Error creating library.txt";
                
            }
        cout << "Please type the word you wish to be added: ";
        cin >> addedWord;
        
        int length = strlen(addedWord);

        if (length <= 5)
            level = 'E';
        else if(length <= 10)
            level = 'M';
        else if(length > 10)
            level = 'H';

        fout<<level<<addedWord<<endl;

        cout << "You have added "<< addedWord << " to the list of words in the Library\n";

        // close open file
    fout.close();

        // Here i want to implement a looping code to ask if user wants to add another word
        // rather than having to go back to the menu.

}

void viewWords()    // This Section will change to Wich words would you like to view, Easy, Meduim, Hard.
                    // at that point the program will pull up the words listed by the dificulty varable
                    // listed with the word in library file.
{

    //clear screen
    system("CLS");

    ifstream fin("library.txt");

        // declaring varaiables to be used
        string words;
        
                    // Checks to ensure the library file is intact.
                if (!fin.good())
            {
                cout << "Cannot find library.txt" << endl;
                
            }

        // need to adjust so that you specify wich words to display, easy medium hard
  
  while(! fin.eof())
  {

   getline(fin, words);
   cout << words << endl;
    
  
  }
    system("PAUSE");
        cout<< "\n";

   fin.close();


        // want program to ask if user would like to display again

}

void game()
{
    system("CLS");
        
        void startGraf();
        void gallowGraf();
        void ropeGraf();
        void headGraf();
        void bodyGraf();
        void armsGraf();
        void deadGraf();
        void youWon();


    // I want the  search for word code in a seperate function

    char word[25];
    int i;
    int x;
    char c;

    ifstream fin("library.txt");
        if(!fin) 
            { //clrscr();
            cout<<"File missing, aborting.";
            system("pause");
            
            }

        for (i=0;!fin.eof();i++)   fin.getline(word,25);
        fin.close();

        do {x=rand();}
        while(x>i || x<0);

        ifstream finn("library.txt");
        
        for (i=0;!finn.eof();i++)
        {finn>>c; finn.getline(word,25);
        
        if (x==i) 
            break;}
        finn.close();
int L;
int nog;
int n;
int d;
char ch;
char blankWord[25];

L=strlen(word);
char choosen[25]="\0";
n=0;d=0;

for(i=0;i<=24;i++) // define blank word to display * if letter not guessed.
   {
    if (word[i]=='\0') 
    {blankWord[i]='\0';break;}
    
    if (word[i]== ' ')  
    {blankWord[i]=' ';  n++;}
    
    if (word[i]!=' ')
    blankWord[i]='*';
   }
    
   nog = 7;       
   do{ system("CLS");
      
    there:

   if (d!=0)  

       switch (nog)
   {
       case 6:
        startGraf();
        break;
       case 5:
           gallowGraf();
           break;
       case 4:
           ropeGraf();
           break;
       case 3:
           headGraf();
           break;
       case 2:
           bodyGraf();
           break;
       case 1:
           armsGraf();
           break;
   }
   cout<<"\n\n\t\t\tChoosen letters : "<<choosen<<"\n";
     cout<<"\n\n\n\t\t\t      "<<blankWord<<"\n\n\nYou have "<<nog<< " guesses left, choose a letter : ";
     cin>>ch; cin.get();    

    for (i=0;i<25;i++) 
    
        if (choosen[i]==ch) 
         {
             system("CLS");
            cout<<"\a\t\t     !!You have choosen "<<ch<<" already!!\n";goto there;
         }
        
          if (choosen[i] !=ch)
          {

            choosen [d]=ch;
            choosen [d+1]=',';
            d+=2;
            nog = (nog - 1);
          }

    for (i=0;i<=24;i++)   
            if (word[i]==ch) 
                {
                blankWord[i]=ch;
                        nog = (nog + 1);
                }

     
        if (!strcmpi (blankWord,word)) 
        youWon();
        // this is not working corectly!

    }while(nog>0 || !strcmpi (blankWord,word));
   deadGraf();
   
// need to have computer pick a word based on level of difficulty supplied by user
   // also need to figure out how to set in the loop to display the apropriate graphic
   // based off of how many guesses are left (nog variable)


        

}

void deleteWord()    // Here user will enter a word, program then takes word searches library file
                    // and removes the word if it is there. in either case gives conformation
                    // if the word was removed or if not because it does not exist.

{
    cout << "This function is not yet working\n";

}

void startGraf()
{
        cout <<"                         \n";
        cout <<"                         \n";
        cout <<"                         \n";
        cout <<"                         \n";
        cout <<"                         \n";
        cout <<"                         \n";
        cout <<"                         \n";
        cout <<"                         \n";
        cout <<"                         \n";
        cout <<"                          \n";    
        cout <<"                         \n";
        cout <<"                         \n";
        cout <<"_________________________\n";
        cout <<"#########################\n";

}
void gallowGraf()
{
    system("CLS");

        cout <<"                      !!!\n";
        cout <<"                      !!!\n";
        cout <<"                      !!!\n";
        cout <<"                      !!!\n";
        cout <<"                      !!!\n";
        cout <<"                      !!!\n";
        cout <<"                      !!!\n";
        cout <<"                      !!!\n";
        cout <<"                      !!!\n";
        cout <<"                      !!!\n";    
        cout <<"                      !!!\n";
        cout <<"                      !!!\n";
        cout <<"______________________!!!\n";
        cout <<"#########################\n";
}

void ropeGraf()
{
    system("CLS");

        cout <<"          !:::!!!!!!!!!!!\n";
        cout <<"            :         !!!\n";
        cout <<"            :         !!!\n";
        cout <<"                      !!!\n";
        cout <<"                      !!!\n";
        cout <<"                      !!!\n";
        cout <<"                      !!!\n";
        cout <<"                      !!!\n";
        cout <<"                      !!!\n";
        cout <<"                      !!!\n";    
        cout <<"                      !!!\n";
        cout <<"                      !!!\n";
        cout <<"______________________!!!\n";
        cout <<"#########################\n";
}

void headGraf()
{
    system("CLS");

        cout <<"          !:::!!!!!!!!!!!\n";
        cout <<"            :         !!!\n";
        cout <<"            :         !!!\n";
        cout <<"           ~~~        !!!\n";
        cout <<"          (* *)       !!!\n";
        cout <<"           (o)        !!!\n";
        cout <<"                      !!!\n";
        cout <<"                      !!!\n";
        cout <<"                      !!!\n";
        cout <<"                      !!!\n";    
        cout <<"                      !!!\n";
        cout <<"                      !!!\n";
        cout <<"______________________!!!\n";
        cout <<"#########################\n";
}

void bodyGraf()
{
    system("CLS");

        cout <<"          !:::!!!!!!!!!!!\n";
        cout <<"            :         !!!\n";
        cout <<"            :         !!!\n";
        cout <<"           ~~~        !!!\n";
        cout <<"          (x x)       !!!\n";
        cout <<"           (o)        !!!\n";
        cout <<"           aaa        !!!\n";
        cout <<"           aaa        !!!\n";
        cout <<"           aaa        !!!\n";
        cout <<"            a         !!!\n";    
        cout <<"                      !!!\n";
        cout <<"                      !!!\n";
        cout <<"______________________!!!\n";
        cout <<"#########################\n";
}

void armsGraf()
{
    system("CLS");

        cout <<"          !:::!!!!!!!!!!!\n";
        cout <<"            :         !!!\n";
        cout <<"            :         !!!\n";
        cout <<"           ~~~        !!!\n";
        cout <<"          (x x)       !!!\n";
        cout <<"        w  (o)  w     !!!\n";
        cout <<"         a aaa a      !!!\n";
        cout <<"          aaaaa       !!!\n";
        cout <<"           aaa        !!!\n";
        cout <<"            a         !!!\n";    
        cout <<"                      !!!\n";
        cout <<"                      !!!\n";
        cout <<"______________________!!!\n";
        cout <<"#########################\n";

}

void deadGraf()
{
    char playagain;

    system("CLS");

        cout <<"          !:::!!!!!!!!!!!\n";
        cout <<"            :         !!!\n";
        cout <<"            :         !!!\n";
        cout <<"           ~~~        !!!\n";
        cout <<"          (x x)       !!!\n";
        cout <<"        w  (o)  w     !!!\n";
        cout <<"         a aaa a      !!!\n";
        cout <<"          aaaaa       !!!\n";
        cout <<"           aaa        !!!\n";
        cout <<"           aaa        !!!\n";    
        cout <<"          a   a       !!!\n";
        cout <<"        ddd   bbb     !!!\n";
        cout <<"______________________!!!\n";
        cout <<"#########################\n";
        cout <<"                         \n";
        cout <<"        GAME OVER        \n";
        cout <<"                         \n";
        cout <<"#########################\n";
        cout <<"\n";
        cout <<"Would you like to Play again? Y/N\n";
        cin >> playagain;

            switch (playagain)
       {
        case 'Y':
                game();
                break;

        case 'y':
                game();
                break;

        case 'N':
                
                break;

        case 'n':
                
                break;
        }
}

void youWon()
{
    char again;

    system("CLS");

    cout <<"\n";
    cout <<" ,~'*````````````*'~,\n";
    cout <<"   Congratulations   \n";
    cout <<" ,~'*............*'~,\n";
    cout <<"\n";
    cout <<" Would you like to play again? Y/N\n";
    cin  >> again;

    switch (again)
       {
        case 'Y':
                game();
                break;

        case 'y':
                game();
                break;

        case 'N':
                // need beter way to exit
                break;

        case 'n':
                // need better way to exit
                break;
        }
}
commented: Who do you think you're kiddin' -1

I changed it to

No. You didn't change anything. You just stole the code somewhere else
Suprised I found it? Some of us actually know the powers of google

Trying to cheat your way through class is bad enough, but asking for help because even cheating is to difficult is just sad...:-/

thanks for stealing my code

Don't make me laugh. I found 'your' code as well. So above message is also for you :(

wow someone really has NO LIFE, unless this website is your paying job, get off the computer and do something usefull with your life. Don't bother flaming me because I won't be checking this again.

commented: simply rude... +0

k. Bye!

All the time I open this thread it just puts a smile on my face..but I have just laughed my ass off after some of the previous posts :)

All the time I open this thread it just puts a smile on my face..but I have just laughed my ass off after some of the previous posts :)

That's funny. Every time I read this thread I get frustated... :@

Things like this irritate me quite a lot:

wow someone really has NO LIFE

And to think, I actually tried to help this guy, before I found out he was stealing all his code...
What a waste of minutes.... :)

That's funny. Every time I read this thread I get frustated... :@

well the fact that someone can copy a code from somewhere, call it his, then someone else gets the same code from the original cheat...and so on..it is frustrating and really disapointing but the fact that someone can even get upset over something they know isnt theirs..then that is funny..:)

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.