-
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;
}
-
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.