Welcome
Welcome to the forums of AntiRTFM's Absolute N00b Spoonfeed C++ Tutorials!

You are currently viewing our boards as a guest, which gives you limited access to view most discussions and access our other features. By joining our free community, you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content, and access many other special features. Registration is fast, simple, and absolutely free, so please, <a href="/profile.php?mode=register">join our community today</a>!

Hangman By- CyberSpy

Finished making your own program (thats not part of the practicing forums) ? Show it off here

Hangman By- CyberSpy

Postby cyberspy on Sun Sep 20, 2009 8:24 am

Finally, after long hours of corrections, i ve made this game.
I wanted it to be fully editable.
You can add your own, words, using function words.push_back("Your Word");
(in case didnt know).
thanks for having a look.
Please report any bugs.

Written by-
CyberSpy aka TheFutureOfEminem

Code: Select all
#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <algorithm>
using namespace std;
int main()
{
   const int MAX_WRONG=8;
   vector<string> words;
   words.push_back("school");
   words.push_back("computer");
   words.push_back("cheat");
   words.push_back("lose");
   words.push_back("yourself");
   words.push_back("they");
   words.push_back("dont");
   words.push_back("care");
   words.push_back("about");
   words.push_back("us");
   words.push_back("hunter");
   words.push_back("thriller");
   words.push_back("billie");
   words.push_back("jean");
   words.push_back("sing");
   words.push_back("for");
   words.push_back("the");
   words.push_back("moment");
   words.push_back("fraction");
   words.push_back("geography");
   words.push_back("dangerous");
   words.push_back("documentation");
   words.push_back("vector");
   words.push_back("hello");
   srand(time(0));
   random_shuffle(words.begin(), words.end());
   const string theWord=words[0]; //Word To Be Guessed
   int wrong=0;
   string soFar(theWord.size(), '-');
   string used=" ";


   cout<<"                    Welcome To Hangman\n"<<endl;
   while((soFar!=theWord) && (wrong<=MAX_WRONG))
   {
      char guess;
      cout<<"You have "<<MAX_WRONG-wrong <<" chances left.\n";
      cout<<"So far the word is \n"<<   soFar;
      cout<<"\nEnter your guess : ";
      cin>>guess;
      while(used.find(guess)!=string::npos)
      {
         cout<<"\nYou have already guessed that character";

         cout<<"\nEnter another letter : ";
         cin>>guess;
      }
      used+=guess;
      if(theWord.find(guess)!=string::npos)
      {
         cout<<"\nCorrect the letter is in the word\n";
         for (unsigned int i=0; i<=theWord.length();i++)
         {
            if(theWord[i]==guess)
            {
               soFar[i]=guess;
            }
         }
      }
      else
      {
         cout<<"\nThe guess is incorrect.\n";
         wrong++;         
      }

   }
      if(wrong==MAX_WRONG)
         cout<<"\nYou have been hanged\n";
      else
         cout<<"\nCongratulations, you guessed it!!!\n";
   cout<<"\nThe word was "<<theWord<<endl;
   cout<<"\n\n    Written By-\n          CyberSpy aka TheFutureOfEminem\n";

   
   return 0;
}
cyberspy
 
Posts: 12
Joined: Sun Sep 13, 2009 4:14 am

Return to Full programs

Who is online

Users browsing this forum: No registered users and 1 guest