Adblock breaks this site

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

Discussion in 'Programming General' started by Nullware, Feb 27, 2008.

  1. Nullware

    Nullware Guru

    Joined:
    Jan 30, 2007
    Posts:
    1,761
    Referrals:
    4
    Sythe Gold:
    0
    [SOURCE] Basic C++ Hangman in the Command Prompt

    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;
    }
    
     
  2. Faskist

    Faskist Tuxhead
    Banned

    Joined:
    Apr 25, 2005
    Posts:
    1,869
    Referrals:
    0
    Sythe Gold:
    0
    [SOURCE] Basic C++ Hangman in the Command Prompt

    system("pause") is wildly inefficient, try getchar.
     
  3. Nullware

    Nullware Guru

    Joined:
    Jan 30, 2007
    Posts:
    1,761
    Referrals:
    4
    Sythe Gold:
    0
    [SOURCE] Basic C++ Hangman in the Command Prompt

    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. ;)
     
  4. Govind

    Govind The One Musketeer
    Mudkips Highly Respected Retired Administrator

    Joined:
    Apr 22, 2005
    Posts:
    7,825
    Referrals:
    13
    Sythe Gold:
    23
    Prove it! Trole Tier 1 Prizebox Tortoise Penis Le Monkey UWotM8? Wait, do you not have an Archer rank? Potamus
    [SOURCE] Basic C++ Hangman in the Command Prompt

    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/
     
  5. Nullware

    Nullware Guru

    Joined:
    Jan 30, 2007
    Posts:
    1,761
    Referrals:
    4
    Sythe Gold:
    0
    [SOURCE] Basic C++ Hangman in the Command Prompt

    I was hoping for some feedback/criticism on my methods of programming and such..
     
  6. Faskist

    Faskist Tuxhead
    Banned

    Joined:
    Apr 25, 2005
    Posts:
    1,869
    Referrals:
    0
    Sythe Gold:
    0
    [SOURCE] Basic C++ Hangman in the Command Prompt

    Not a bad idea, actually. Still, that's a shitty way of doing things, when you could just use the cross-platform getch.
     
  7. Swan

    Swan When They Cry...
    Retired Global Moderator

    Joined:
    Jan 23, 2007
    Posts:
    4,957
    Referrals:
    0
    Sythe Gold:
    0
    Sythe's 10th Anniversary Member of the Month Winner
    [SOURCE] Basic C++ Hangman in the Command Prompt

    Contradictory statement is contradictory.

    Good job for terminal ;)
     
< [Poll][TuT] Anyone wants to make an autotyper in C#??? | Hex Editing Functions >


 
 
Adblock breaks this site