hangman level easy medium and hard c++

question>>
You must create a wordlist class that manages the list of words. it's up to you to figure out what goes in it.
The length of a word will determine its difficulty:
Easy: words >= 1 character and < 3 characters
Medium: words >= 3 characters and < 6 characters
Hard: words with >= 6 characters

------------------------>>

compile problems:
hangman.hpp:75: error: 'string' has not been declared
hangman.hpp:75: error: ISO C++ forbids declaration of 'str' with no type
hangman.cpp: In member function 'void hangman::run()':
hangman.cpp:41: error: invalid use of qualified-name 'hangman::easy'
hangman.cpp:45: error: invalid use of qualified-name 'hangman::medium'
hangman.cpp:49: error: invalid use of qualified-name 'hangman::hard'
hangman.cpp: In function 'int main(int, char**)':
hangman.cpp:183: error: 'next' was not declared in this scope
hangman.cpp:186: error: expected ';' before 'if'
hangman.cpp:190: error: 'else' without a previous 'if'
hangman.cpp:192: error: no matching function for call to 'std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >::size(bool)'
/usr/lib/gcc/i686-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/stl_vector.h:532: note: candidates are: size_t std::vector<_Tp, _Alloc>::size() const [with _Tp = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _Alloc = std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >]
hangman.cpp:196: error: 'else' without a previous 'if'
hangman.cpp:198: error: expected primary-expression before '>=' token
hangman.cpp: At global scope:
hangman.cpp:214: error: expected unqualified-id before 'while'
hangman.cpp:215: error: expected constructor, destructor, or type conversion before ';' token
hangman.cpp:216: error: expected constructor, destructor, or type conversion before ';' token
hangman.cpp:217: error: expected constructor, destructor, or type conversion before ';' token
hangman.cpp:218: error: expected unqualified-id before 'return'
hangman.cpp:219: error: expected declaration before '}' token
make: *** [hangman] Error 1


------------------->>

code:
#include <iostream>
#include <fstream>
#include <vector>
#include <cstring>
#include <string>
#include <cmath>
#include "hangman.hpp"

using std::vector;
using std::string;
using std::endl;
using std::cout;
using std::cin;

bool Quit = false;
char Answer;
hangman::hangman(int initGuess, std::string wordToGuess){
int i;
guesses = initGuess;
word = wordToGuess;
guess = word;

for( i = 0; i < guess.length(); i++ ) { // length of the guess
guess[i] = '-';
}
for( int i = 0; i < MAX_GUESSES; i++ ) { // maximum guesses can be made
incorrect[i] = ' ';
}
incorrect_index = 0;
}

void hangman::run(){

int choice;
bool keep_going = true;
std::cout << "Welcome to hangman!" << std::endl;
std::cout << "Choose 0 for easy, 1 medium and 2 for hard: " << endl;
int user_input; // input by the user to chose the hardness level

while(user_input = 0){ // while user enters 0 easy
char hangman::easy();

}
while(user_input = 1){ // while user enters 1 medium
char hangman::medium();

}
while(user_input = 2){ // while user enters 2 hard
char hangman::hard();

}

while(1){
update_screen();
keep_going = guess_letter();
if( keep_going == false ) {
break;
}
}
}

bool hangman::process_incorrect_guess(char letter_guess) {
std::cout << "Sorry that's not a letter in the word!" << std::endl;
std::cout << std::endl;
--guesses;
if( guesses == 0 ) {
//Ran out of guesses. Stop the game
std::cout << "Sorry! you lose"<< std::endl;
std::cout << "The word was: ";
std::cout << word << std::endl;
return false;
} else {
// Still has guesses remaining. Keep going
incorrect[incorrect_index] = letter_guess;
++incorrect_index;
return true;
}
}

bool hangman::process_correct_guess() {
if( word == guess ) {
// Word guessed correctly. Stop the game
std::cout << "Excellent work!"<< std::endl;
std::cout << "The word was: ";
std::cout << word << std::endl;
return false;
} else {
// Guess was correct but there are still letters to guess
// Keep going.
std::cout << "Excellent guess!" << std::endl;
std::cout << std::endl;
return true;
}
}

bool hangman::guess_letter(){
char letter_guess;
int i;
int num_correct = 0;
std::cout << "What is your guess? :"<< std::endl;
std::cin >> letter_guess;
std::cout << std::endl;

//use a for loop to cycle through the letters of the
// array of characters (notice how if there is a duplicate
// letter in the array this algorithm will fill both letters in)
for(i = 0; i < word.length(); i++){

//if the guess is num_correct
if(letter_guess == word[i]){

//fill word at the num_correct index
guess[i] = letter_guess;
++num_correct;
}
}

if(num_correct == 0){

// User didn't guess a letter correctly
return process_incorrect_guess(letter_guess);
} else {

// User guessed a letter correcty
return process_correct_guess();
}
}



void hangman::update_screen(){
//Outputs the word with blanks
std::cout << "Word: ";
std::cout << guess << std::endl;

//Prints the incorrectly guessed letters so far
std::cout << "Incorrect letters: ";
for( int i = 0; i < incorrect_index; i++ ) {
if( i == ( incorrect_index-1 )) {
std::cout << incorrect[i];
} else {
std::cout << incorrect[i] << " ";
}
}
std::cout << std::endl;

//Prints the number of incorrect guesses remaining
std::cout << "Incorrect guesses remaining: ";
std::cout << guesses << std::endl;
std::cout << std::endl;
}

int main(int argc, char**argv){ //main

char answer=' ';
std::ifstream inStream;
inStream.open("words.txt"); //input file

vector <string> words;

if( inStream.fail())
{
std::cout << "Input file opening failed " << endl;
exit(1);
}
while (inStream >> next){
words.push_back(next);
next.str()
if(words.size()<3); // when the user wants to play easy the char should be less than 3

cout << "easy" << endl;

else

if(words.size()<6);// when the user wants to play medium the char should be less than 6 and more than 3

cout << "meduim"<< endl;

else

if(words.size(>=6)); // hard more the 6 char

cout << "hard" << endl;


cout << "Would you like to go again? (Y/N): "<< endl; // play again
}


while (answer!='y' && answer!='Y' && answer!='n' && answer!='N') // ask the user for the input
{
cout<<"Sorry, that was an incorrect response."<< endl; // wrong char
cout<<"Please re-enter choice: "<< endl; // ask again
cin>>answer;
}
}
while (answer=='y' || answer=='Y');
EndTitle();
Signature();
Quit();
return(0);
}
Last edited on
code tags
here are some tags... Thank you...
Whatever you did, you haven't used code tags to format your post. Please do so, to make it readable.
#include "hangman.hpp"

That's one of your errors, a simple typo.

#include "hangman.hpp" is header file and its not a problem...
Topic archived. No new replies allowed.