Hangman-like program

This is a discussion on Hangman-like program within the C++ Programming forums, part of the General Programming Boards category; I've tried several different solutions as usual, but it's another one I can't seem to figure out. It is suppose ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    465

    Hangman-like program

    I've tried several different solutions as usual, but it's another one I can't seem to figure out. It is suppose to return the letters the player has guessed and if the word contains those letters by printing 'Correct' or 'Wrong'. I highlighted the problematic area.

    Code:
    #include <iostream>
    #include <string>
    #include <ctime>
    
    using namespace std;
    
    
    int main(){
    int i=1;
    int difficulty;
    int maxguess;
    char guess;
    string test[5]={"architect","ancient","tower","symptom","astronomer"};//words to guess
    string guessed;
    srand(time(NULL));
    do{
    cout<<"Difficulty level? 1-easy 2-medium 3-hard"; //ask difficulty level
    cin>>difficulty;
    if(difficulty==1){
    maxguess=10;
    cout<<"Difficulty set to easy.";
    }
    if (difficulty==2){
    maxguess=8;
    cout<<"Difficulty set to medium.";
    }
    if (difficulty==3){
    maxguess=4;
    cout<<"Difficulty set to hard.";
    }
    }while(difficulty<1||difficulty>3);
    int word=rand()%5;          //randomly pick word from string test
    cout<<"Your word is "<<test[word].size()<<" letters long.";
    while (maxguess>0){
    cout<<guessed;       //display previously guessed letters
    cout<<"Guess:";
    cin>>guess;
    if((guessed.find(guess))==1){
    cout<<"You have already guessed that."; //check if letter has already been guessed
    }
    if((guessed.find(guess))==0){
    	if (test[word].find((guess)==1)){
    	cout<<"Correct.";
    	}
    	if (test[word].find((guess)==0)){
    	cout<<"Wrong!";
    	maxguess--;
    	}
    guessed[i]=guess;	//add letter to guessed string
    i++;
    }
    }
    return 0;
    }
    My computer is awesome.

  2. #2
    System.out.println("");
    Join Date
    Jan 2005
    Posts
    84
    You have a lot of problems, but the most major ones are that a few of your if statements need to be turned into if else statements. Also, I don't think your find function does what you think it does. You seem to think it is going to accept a char argument and return a boolean telling you if the character was found in the string. I think it just returns the index of the location of the character in the string if it is found. http://www.cppreference.com/cppstring/find.html. I think you need to write a for loop to increment over the string to determine if the char is in there.
    Last edited by Loctan; 08-06-2006 at 01:12 AM.

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