Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib>
- #include <fstream>
- #include <ctime>
- #include <string>
- using namespace std;
- const int TRIES = 5;
- int letterFill(char, string, string&);
- int main()
- {
- string name;
- char letter;
- int num_of_wrong_guesses = 0;
- string word;
- string line;
- ifstream myfilе("words.txt");
- int n = rand() % 600000;
- if (myfilе.is_open())
- {
- int index = 0;
- while (getline(myfilе, line))
- {
- if (index++ == n) {
- word = line;
- break;
- }
- }
- myfilе.close();
- }
- else cout<< "Unable to open file";
- //choose and copy a word from array of words randomly
- // Initialize the secret word with the * character.
- string unknown(word.length(), '*');
- // welcome the user
- cout<< "\n\nWelcome to hangman!! Guess the word";
- cout<< "\n\nYou have to type only one letter in one try.";
- cout<< "\n\nYou have " << TRIES << " tries to try and guess the word.";
- cout<< "\n*****************************************";
- // Loop until the guesses are used up
- while (num_of_wrong_guesses< TRIES)
- {
- cout<< "\n\n" << unknown;
- cout<< "\n\nGuess a letter: ";
- cin>> letter;
- if (letterFill(letter, word, unknown) == 0)
- {
- cout<<endl<< "Whoops! That letter isn't in there!" <<endl;
- num_of_wrong_guesses++;
- }
- else
- {
- cout<<endl<< "You found a letter!" <<endl;
- }
- // Tell user how many guesses has left.
- cout<< "You have " << TRIES - num_of_wrong_guesses;
- cout<< " guesses left." <<endl;
- // Check if user guessed the word.
- if (word == unknown)
- {
- cout<< word <<endl;
- cout<< "Yeah! You got it!";
- break;x
- }
- }
- if (num_of_wrong_guesses == TRIES)
- {
- cout<< "\nSorry, you lose." <<endl;
- cout<< "The word was : " << word <<endl;
- }
- cin.ignore();
- cin.get();
- return 0;
- }
- int letterFill(char guess, string secretword, string &guessword)
- {
- int i;
- int matches = 0;
- int len = secretword.length();
- for (i = 0; i<len; i++)
- {
- if (guess == guessword[i])
- return 0;
- if (guess == secretword[i])
- {
- guessword[i] = guess;
- matches++;
- }
- }
- return matches;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement