Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.45 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <fstream>
  4. #include <ctime>
  5. #include <string>
  6. using namespace std;
  7. const int TRIES = 5;
  8. int letterFill(char, string, string&);
  9. int main()
  10. {
  11.     string name;
  12.     char letter;
  13.     int num_of_wrong_guesses = 0;
  14.     string word;
  15.     string line;
  16.     ifstream myfilе("words.txt");
  17.     int n = rand() % 600000;
  18.     if (myfilе.is_open())
  19.     {
  20.         int index = 0;
  21.         while (getline(myfilе, line))
  22.         {
  23.             if (index++ == n) {
  24.                 word = line;
  25.                 break;
  26.             }
  27.         }
  28.  
  29.         myfilе.close();
  30.     }
  31.     else cout<< "Unable to open file";
  32.     //choose and copy a word from array of words randomly
  33.     // Initialize the secret word with the * character.
  34.     string unknown(word.length(), '*');
  35.     // welcome the user
  36.     cout<< "\n\nWelcome to hangman!! Guess the word";
  37.     cout<< "\n\nYou have to type only one letter in one try.";
  38.     cout<< "\n\nYou have " << TRIES << " tries to try and guess the word.";
  39.     cout<< "\n*****************************************";
  40.     // Loop until the guesses are used up
  41.     while (num_of_wrong_guesses< TRIES)
  42.     {
  43.         cout<< "\n\n" << unknown;
  44.         cout<< "\n\nGuess a letter: ";
  45.         cin>> letter;
  46.         if (letterFill(letter, word, unknown) == 0)
  47.         {
  48.             cout<<endl<< "Whoops! That letter isn't in there!" <<endl;
  49.             num_of_wrong_guesses++;
  50.         }
  51.         else
  52.         {
  53.             cout<<endl<< "You found a letter!" <<endl;
  54.         }
  55.         // Tell user how many guesses has left.
  56.         cout<< "You have " << TRIES - num_of_wrong_guesses;
  57.         cout<< " guesses left." <<endl;
  58.         // Check if user guessed the word.
  59.         if (word == unknown)
  60.         {
  61.             cout<< word <<endl;
  62.             cout<< "Yeah! You got it!";
  63.             break;x
  64.         }
  65.     }
  66.     if (num_of_wrong_guesses == TRIES)
  67.     {
  68.         cout<< "\nSorry, you lose." <<endl;
  69.         cout<< "The word was : " << word <<endl;
  70.     }
  71.     cin.ignore();
  72.     cin.get();
  73.  
  74.     return 0;
  75. }
  76. int letterFill(char guess, string secretword, string &guessword)
  77. {
  78.     int i;
  79.     int matches = 0;
  80.     int len = secretword.length();
  81.     for (i = 0; i<len; i++)
  82.     {
  83.         if (guess == guessword[i])
  84.             return 0;
  85.         if (guess == secretword[i])
  86.         {
  87.             guessword[i] = guess;
  88.             matches++;
  89.         }
  90.     }
  91.     return matches;
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement