• Log in or sign up for an account here: https://www.sythe.org/login

[SOURCE] Basic C++ Hangman in the Command Prompt

Nullware

Guru
Joined
Jan 30, 2007
Sythe Gold
0
Messages
1,761
Location
/home/Canada
I made this to see what I was capable of in a little bit of spare time and I thought it turned out all right so I've decided to share it in hopes that it might help some people. (It's commented almost throughout) :)

I also wouldn't mind if any of the better programmers want to point out flaws in my code or my style of programming. Points to improve on and criticism would actually be appreciated. ;)

I use Dev-CPP which uses the MinGW compiler so you might have to change the return statement at the end or make other minor changes to get it to work.

Code:
#include <cstdlib> // Standard C++ library
#include <iostream> // Basic input/output library
#include <conio.h> // Required for getch()

using namespace std;

int toupper(int c);

// Match the user's guess up against the puzzle word and update any characters guessed right
int updateWord(string& tempWord, string realWord, string guessedChar) {
       string previousWord = tempWord; // Hold the previous value of the puzzle with unknowns

       for(int i=0;i<=realWord.length()-1;i++)  // Loop through the length of the puzzle string
       {
         if (realWord.substr(i,1)==guessedChar) { // If the current character being looped through matches the user's guess
         tempWord = tempWord.substr(0,i)+ guessedChar + tempWord.substr(i+1,tempWord.length()); // Replace that character in the progress string
          
         }
       }
       
       if(previousWord == tempWord) { // If the puzzle hasn't changed
           cout << "Sorry, there are none of that letter in the puzzle.\n";
       }
       else{ // The puzzle was updated due to some letters being found
           cout << "Some letters were found and now your puzzle has been updated.\n";
       }
}

int main(int argc, char *argv[])
{
    string dictionary[6] = { "hockey", "revolution", "extortion", "football", "alias", "detention" }; // Initialize the dictionary of words to use
    string guess, playagain; // Strings to hold the user input
    
    cout << "------------------ Welcome to Hangman! ------------------\n";
    
  do
  {
    
    srand(time(0)); // Initialize our reference to the time variable for randomizations
    int wordNumber = rand() % 6; // Generate a random number to choose a word from the dictionary
    
    string word = dictionary[wordNumber]; // Save the value of the word we're using in an independant variable
    string tempWord (word.length(),'_'); // Setup a variable with underscores to represent letters not known of the length of the chosen word
    
    while(tempWord != word) // While the word hasn't been guessed loop through
    {
    cout << "\nThe puzzle is " << tempWord << " (" << tempWord.length() << " characters), guess a letter.\n"; // Ask the user to guess a letter and output his current status in guessing the word

    guess = getch(); // Get the user's guess by keyboard input

    updateWord(tempWord, word, guess); // Check the user's guess up against the puzzle word characters            
    }
    
    cout << "You win! The word was: " << word << "\n"; // Output this message if the user wins
    
    do {
    cout << "\nWould you like to play again? (Y/N).\n";
    playagain = getch();
    }while(playagain!="y" && playagain !="n"  && playagain !="Y"  && playagain !="N"); // Keep asking if the user wants to play again until he enters yes (y) or no (n)

  }while(playagain=="y" || playagain=="Y"); // Loop through the game again if he presses y (yes)

    system("PAUSE");
    return EXIT_SUCCESS;
}
 
system("pause") is wildly inefficient, try getchar.
 
system("pause") is wildly inefficient, try getchar.
Oh, Dev-CPP automatically puts that at the end of your code so you can view any output before it closes. I can remove it anyway now because it will always ask you whether you want to quit before it does. ;)
 
Le Monkey
PHP Shit Show 2k26
Potamus
Prove it!
Sythe Awards 2011 Winner
Tier 1 Prizebox
Tortoise Penis
Trole
UWotM8?
Wait, do you not have an Archer rank?
Lol, system("PAUSE") only doesn't work in Unix or Cygwin environments. What to do is write an app which does
printf("Press any key to continue . . . ");
getch();
and put that in /usr/bin/
 
I was hoping for some feedback/criticism on my methods of programming and such..
 
Lol, system("PAUSE") only doesn't work in Unix or Cygwin environments. What to do is write an app which does
printf("Press any key to continue . . . ");
getch();
and put that in /usr/bin/
Not a bad idea, actually. Still, that's a shitty way of doing things, when you could just use the cross-platform getch.
 
Member of the Month Winner
Sythe's 10th Anniversary
Not a bad idea, actually. Still, that's a shitty way of doing things, when you could just use the cross-platform getch.
Contradictory statement is contradictory.

Good job for terminal ;)
 
Runescape Gamblingrunescape gambling
Back
Top