Hangman program Help!

This is a discussion on Hangman program Help! within the C++ Programming forums, part of the General Programming Boards category; I made this hangman program and everything works except I have been trying to put a variable named count which ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    32

    Hangman program Help!

    I made this hangman program and everything works except I have been trying to put a variable named count which adds one everytime an incorrect letter is guessed and after six incorrect guesses the program quits, but i dont know where to put it so that it will work any help would be appreiciated here is my code:
    Code:
    #include<iostream.h>
    #include<console.h>
    #include<string.h>
    
    int main()
    
    {
    
    int count=0;
    int wrong;
    string secret;
    char answer;
    string answer2;
    string cover;
    
    cout<<"Enter word: "<<endl;  //User enters word to be guessed
    cin>>secret;
    
    cover=secret;
    answer2=secret;
    	
    clrscr();// clears screen	
    
    
    	
    for(int i=0; i<secret.length(); i++)//prints dashs to show length of word
    	{
    	cover[i]='-';
    	cout<<cover[i];
    	}	
    
    cout<<endl;
    
    do{
    
    cout<<"Enter your guess($ to guess the whole word and * to quit): "<<endl;
    cin>>answer;
    
    
    
    if(answer=='$')	// Allows you to guess the whole word
    	{
    	cout<<"Enter your letter guess for the whole word: ";
    	cin>>answer2;
    		
    	
    	if(secret==answer2) // checks if word entered is right
    		{
    		cout<<"You are right!"<<endl;
    		answer='*';
    		
    		}	
    	else		
    		{
    		
    		cout<<"incorret, guess again"<<endl;
    		 
    		}
    	}
    else
    	{
    		
    		
    		
    		for(int j=0; j<secret.length(); j++)  //changes dashs to correct letters guessed
    			{
    			if(secret[j]==answer)
    				{
    				cover[j]=answer;
    				 wrong=true;
    				}
    			
    			cout<<cover[j];
    			
    			}
    			
    				cout<<endl;
    				if(wrong!=true)
    				count++;
    		
    			if(count==1)
    				cout<<"O";
    			else if(count==2)
    				cout<<"  O  "<<endl<<"  |  ";
    			else if(count==3)
    				cout<<"  O  "<<endl<<" /| ";	
    			else if(count==4)	
    				cout<<"  O  "<<endl<<" /|\ ";
    			else if(count==5)
    				cout<<"  O  "<<endl<<" /|\\ "<<endl<<" / ";
    			else if(count==6)
    				{
    				cout<<"  O  "<<endl<<" /|\\ "<<endl<<" / \\ ";		
    				cout<<"You've lost!";
    				answer='*';
    				}
    			cout<<" "<<count;
    		cout<<endl;
    		
    		
    		if(secret==cover)   //When all letters guessed program ends
    			{
    			cout<<"You guessed it!";
    			answer='*';
    			}	
    	
    	}	
    }while(answer!='*');
    
    
    
    return(0);
    
    }
    Last edited by venomgts550; 04-18-2002 at 05:33 PM.
    I am a C++ newb
    using: Visual C++ 6.0
    thanx for any Help

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    You could try:

    if(count >= 6) answer='*';

    Somewhere at the end of your loop
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    32
    Well the problem i was running into was that where i placed the count++; code it would not work out and add too many letters or just not add at all, so if I put it out of the for loop it will count every guess, I only want it to count the wrong guesses and I dont have anymore ideas of where to put count++, or any other ways of doing it.
    I am a C++ newb
    using: Visual C++ 6.0
    thanx for any Help

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    if(wrong!=true) count++;

    Shouldn't this be:

    if(wrong==true) count++;

    Since you only want to increase count when there have been a wrong answer...?
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Registered User
    Join Date
    Nov 2001
    Posts
    32
    well wrong is just the word I use but the code in the loop checks if answer is in the word, and then makes wrong = true if it is
    so if(wrong!=true) should work but it doesnt, if there is another way I can do this i will gladly change it because i havent been able to get it to work this way
    I am a C++ newb
    using: Visual C++ 6.0
    thanx for any Help

  6. #6
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Hmm, this might be the problem:
    You never reset wrong to false at the beginning of each loop. So if you guess the right letter in say turn 3, wrong will have the value true in every loop after that one.
    Set wrong to false in the beginning of your loop.

    wrong=false;

    (It might be more proper if you use the datatype bool on wrong, instead of int. bool can only have the value true and false)
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with Hangman program
    By Trank07 in forum C Programming
    Replies: 1
    Last Post: 11-24-2007, 09:00 PM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. Help on hangman program
    By vserenev in forum C++ Programming
    Replies: 1
    Last Post: 01-11-2007, 11:34 AM
  4. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 10:17 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21