Welcome to OMG boards community!
Community of friends where we talk about daily life, offtopic stuff, and what keeps us busy.

You are currently viewing our community forums as a guest user. Sign up or
Having an account grants you additional privileges, such as creating and participating in discussions.

C++ Hangman Game

Discussion in 'The Geek District' started by Toby_offtopica, Jan 11, 2015.

  1. Toby_offtopica

    Toby_offtopica OMG Member

    Threads:
    92
    Messages:
    587
    Likes Received:
    0
    Trophy Points:
    75
    A C++ Hangman game I made. Not very big.




    Code:
    
    
    #include 
    
    
    #include 
    
    
    #include 
    
    
    #include 
    
    
    #include 
    
    
    #include 
    
    
    
    
    using namespace std;
    
    
    
    
    int main()
    
    
    {
    
    
    
    
    
    
       //setup
    
    
       const int maxwrong = 8;
    
    
    
    
       vector words;
    
    
       words.push_back("Forum");
    
    
       words.push_back("Lol");
    
    
       words.push_back("Awesome");
    
    
       words.push_back("Coolio");
    
    
       int wrong = 0;
    
    
    
    
       srand(time(0));
    
    
    
    
       random_shuffle(words.begin(), words.end());
    
    
       const string selectedword = words[0];
    
    
       string SoFar(selectedword.size(), '-');
    
    
       string used = "";
    
    
    
    
    
    
    
    
       cout << "C++ Hangman!\n";
    
    
    
    
       char guess;
    
    
    
    
       while(wrong < maxwrong && SoFar != selectedword)
    
    
       {
    
    
           cout << "\nYou have " << maxwrong - wrong << " guesses left.";
    
    
           cout << "\nYou've used these letters: " << used;
    
    
    
    
           cout << "\n\nEnter your guess: ";
    
    
    
    
           cin >> guess;
    
    
    
    
           while(used.find(guess) != string::npos)
    
    
           {
    
    
               cout << "\nYou have already guessed that letter! Try again!";
    
    
               cout << "\n\nEnter your guess: ";
    
    
               cin >> guess;
    
    
           }
    
    
    
    
           used+= guess;
    
    
    
    
           if(selectedword.find(guess) != string::npos)
    
    
           {
    
    
               cout << "\nYou guesses a letter! " << guess << " is in the letter.";
    
    
               for(int i = 0; i < selectedword.length(); i++)
    
    
               {
    
    
                   if(selectedword[i] == guess)
    
    
                   {
    
    
                       SoFar[i] = guess;
    
    
                   }
    
    
               }
    
    
           }
    
    
           else{
    
    
               cout << "\n\nSorry, " << guess << " is not in the word.";
    
    
               ++wrong;
    
    
           }
    
    
    
    
    
    
       }
    
    
    
    
       if(wrong == maxwrong)
    
    
       {
    
    
           cout << "\n\nYou have been hanged.";
    
    
           cin.get();
    
    
           cin.get();
    
    
           return 0;
    
    
       }
    
    
    
    
       cout << "The word was: " << selectedword;
    
    
       cin.get();
    
    
       cin.get();
    
    
    
    
    
    
    
    
    }
    
    
    
    
    
     
  2. Beyonder

    Beyonder OMG Member

    Threads:
    25
    Messages:
    602
    Likes Received:
    0
    Trophy Points:
    75
    Thanks for the code!
     
  3. OP
    Toby_offtopica

    Toby_offtopica OMG Member

    Threads:
    92
    Messages:
    587
    Likes Received:
    0
    Trophy Points:
    75
    No problem [​IMG]
     
  4. Beyonder

    Beyonder OMG Member

    Threads:
    25
    Messages:
    602
    Likes Received:
    0
    Trophy Points:
    75
    Toby has it guys respect him!
     
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice