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>!

Console Hangman game for two players

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

Console Hangman game for two players

Postby backlash53 on Sun Sep 06, 2009 1:05 pm

Code: Select all
#include <iostream>
#include <string>

using namespace std;

void frame(string& word, int& guess);


int main()
{
   string nameOne;
   string nameTwo;
   string word;
   char letter;
   int guesses = 0;
   
   bool cont = true;

   cout << "Player 1, please enter your name: ";
      cin >> nameOne;

      cout << "Player 2, please enter your name: ";
      cin >> nameTwo;

      cout << nameOne << " please enter a word: ";
      cin >> word;
      system("cls");

      string hidden(word.size(), '_');
      

   while ( cont )
   {
      frame(hidden, guesses);
      cout << nameTwo << " guess a letter: ";
      cin >> letter;
      guesses++;
      if ( guesses >= 15 )
      {
         cout << "You lose !\n";
         break;
      }
      


      for ( int i = 0; i != word.size(); i++ )
      {
         if ( letter == word[i] )
         {
            hidden[i] = letter;
         }

         if (hidden  == word )
         {
            system("cls");
            cout << "Congratulations " << nameTwo << ", you guessed";
            cout <<   " the word\n in " << guesses << " guesses !" << endl;
            guesses = 0;
            cont = false;
            break;
         }
      }
}
   /// now players two turn
   cout << nameTwo << " please enter a word: ";
      cin >> word;
      system("cls");

      string secondhidden(word.size(), '_');
    cont = true;
   while ( cont )
   {
      frame(secondhidden, guesses);
      cout << nameOne << " guess a letter: ";
      cin >> letter;
      guesses++;
      if ( guesses >= 15 )
      {
         cout << "You lose !\n";
         break;
      }
      


      for ( int i = 0; i != word.size(); i++ )
      {
         if ( letter == word[i] )
         {
            secondhidden[i] = letter;
         }

         if (secondhidden  == word )
         {
            system("cls");
            cout << "Congratulations " << nameOne << ", you guessed";
            cout <<   " the word\n in " << guesses << " guesses !" << endl;
            guesses = 0;
            cont = false;
            break;
         }
      }
}
   return main();
   return 0;
}

void frame(string& word, int& guess)
{
   string top(word.size()+4, '*');
   string bottom(word.size()+4, '*');
   string middle = "* " + word + " *";

   cout << "WORD SO FAR: \t GUESSES MADE: " << guess << endl;
   cout << top << endl;
   cout << middle << endl;
   cout << bottom << endl;
}
backlash53
 
Posts: 26
Joined: Wed Feb 04, 2009 1:42 pm

Re: Console Hangman game for two players

Postby pipey85 on Thu Sep 10, 2009 6:56 am

I can seem to get it to work, it keeps throwing up these two errors...
1>MSVCRTD.lib(crtexew.obj) : error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup
and fatal error LNK1120: 1 unresolved externals
Pardon my n00bness, what I lack in C++ I make up for in time spent reading about C++ :P
pipey85
 
Posts: 40
Joined: Mon Jun 22, 2009 6:11 pm

Re: Console Hangman game for two players

Postby pipey85 on Thu Sep 10, 2009 7:12 am

Just a quick question too, what is "return main();" a recursive function? ive touched on them, but by god they are confusing! I cant see why you've put return 0; straight after it? please educate me :D
Pardon my n00bness, what I lack in C++ I make up for in time spent reading about C++ :P
pipey85
 
Posts: 40
Joined: Mon Jun 22, 2009 6:11 pm

Re: Console Hangman game for two players

Postby noobgrammer on Thu Sep 10, 2009 8:39 am

You might have a setting on your compiler to not compile on certain warnings like infinite looping
his main recursion is a infinite loop and the return 0 will never get touched. I believe main() recursion is a big no no
you would need to pass something in main(x - 1)with a if statement to check it but you can't do that with main function.
He may not know about looping yet.

recursion is still confusing to me too.
I need a compiler with a can of RAID built into it

I added a msn messenger just for programming feel free to email or add yourself to it
User avatar
noobgrammer
 
Posts: 198
Joined: Tue Aug 25, 2009 10:44 am
Location: Orlando


Return to Full programs

Who is online

Users browsing this forum: No registered users and 0 guests