Thread: Hangman program

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

    Question Hangman program

    I have to create a program that asks the user for a word clears the screen then lets the user guess the letters or the whole word so far i have it so that is clears the screen after the word is entered and if you want to guess the whole word you can, the problem is I dont know what to do with guessing single letters
    this is what i have so far:

    Code:
    #include<iostream.h>
    #include<console.h>
    #include<string.h>
    
    int main()
    
    {
    
    
    char secret[100];
    char answer;
    char answer2[100];
    
    cout<<"Enter word: "<<endl;
    cin>>secret;
    
    	
    clrscr();	
    
    
    	
    for(int i=0; i<secret.length(); i++)
    	{
    	cout<<"-";
    	}	
    
    cout<<endl;
    
    do{
    
    cout<<"Enter your guess($ to guess the whole word and * to quit): "<<endl;
    cin>>answer;
    
    if(answer=='$')	
    	{
    	cout<<"Enter your guess for the whole word: ";
    	cin>>answer2;
    		
    	
    	if(strcmp(secret, answer2))
    		cout<<"incorret, guess again"<<endl;
    	else		
    		cout<<"You are right!";
    		
    	}
    	
    }while(answer!='*');
    
    
    
    return(0);
    
    }
    I need to make the output something like
    ----
    -a--
    ca--
    etc.
    I also wanted to use secret.length(); to print the intial dashs of how long the word was but I cant get it to work

    Any help is apprieciated
    thank you
    I am a C++ newb
    using: Visual C++ 6.0
    thanx for any Help

  2. #2
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330
    char isn't a class and doesn't have a length() member function

    You probably want string. Include <string> and use the standard namespace.

    And instead of using strcmp, just use == (overloaded operator found in the string class)

    To find if it's in your string... just go from position 0 to the end of the string is see if your char matches any place... if it does... use some sort of gotoxy() to place that char on the screen

    You can do it

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, 10: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, 12:34 PM
  4. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM