Code Review for Hangman in C++ I have the following C++ program:

#include &lt;iostream&gt;
#include &lt;cstdlib&gt;
#include &lt;ctime&gt;
#include &lt;vector&gt;

void printVector (std::vector&lt;int&gt;&amp; vec)
{
    for (int a = 0; a &lt; vec.size(); a++)
        std::cout &lt;&lt; (char)vec[a] &lt;&lt; ", ";
}

int main ()
{
    const std::string HANGMAN[] = {"\n------+",
                                  "\n      |\n      |\n      |\n      |\n      |\n------+",
                                  "\n   ---+\n      |\n      |\n      |\n      |\n      |\n------+",
                                  "\n   ---+\n   |  |\n      |\n      |\n      |\n      |\n------+",
                                  "\n   ---+\n   |  |\n   O  |\n      |\n      |\n      |\n------+",
                                  "\n   ---+\n   |  |\n   O  |\n   |  |\n      |\n      |\n------+",
                                  "\n   ---+\n   |  |\n   O  |\n  /|  |\n      |\n      |\n------+",
                                  "\n   ----\n   |  |\n   O  |\n  /|\\ |\n      |\n      |\n-------",
                                  "\n   ----\n   |  |\n   O  |\n  /|\\ |\n  /   |\n      |\n-------",
                                  "\n   ----\n   |  |\n   O  |\n  /|\\ |\n  / \\ |\n      |\n-------"};
    const std::string WORDS[] = {"zigzagging", "wigwagging", "grogginess", "beekeeping", "mummifying",
                                 "fluffiness", "fulfilling", "shabinness", "revivified", "kobnobbing",
                                 "beekeepers", "wheeziness", "shagginess", "sleeveless", "parallaxes",
                                 "woolliness", "chumminess", "skyjacking", "grubbiness", "wobbliness",
                                 "feebleness", "jaywalking", "alkalizing", "blabbering", "overjoying"};
    srand(time(0));
    std::string compWord = WORDS[rand() % 25];
    char compHiddenWord[] = "----------";
    int hangmanPos = 0;
    std::vector&lt;int&gt; guessed;
    char userGuess;
    bool letterCorrect;
    while (hangmanPos != 9)
    {
        std::cout &lt;&lt; "\nThis is your hangman:\n" &lt;&lt; HANGMAN[hangmanPos] &lt;&lt;
                     "\n\nThese are the letters you've already guessed:\n\n";
        printVector(guessed);
        std::cout &lt;&lt; "\n\nThis is my word:\n\n" &lt;&lt; compHiddenWord &lt;&lt;
                     "\n\nGuess a letter: ";
        std::cin &gt;&gt; userGuess;
        guessed.push_back(userGuess);
        letterCorrect = false;
        for (int a = 0; a &lt; compWord.size(); a++)
            if (compWord[a] == userGuess)
            {
                if (letterCorrect == false)
                {
                    std::cout &lt;&lt; userGuess &lt;&lt; " is in my word!\n\n\n";
                    letterCorrect = true;
                }
                compHiddenWord[a] = userGuess;
            }
        if (letterCorrect == false)
        {
            std::cout &lt;&lt; "Oops! " &lt;&lt; userGuess &lt;&lt; " is not in my word.";
            hangmanPos++;
        }
        else
            for (int a = 0; a &lt; compWord.size(); a++)
                if (compWord == compHiddenWord)
                {
                    std::cout &lt;&lt; "Well done, " &lt;&lt; compWord &lt;&lt; " was my word!";
                    return 0;
                }
    }
    std::cout &lt;&lt; "\nOh Dear! Looks like you've been hanged. My word was actually " &lt;&lt; compWord &lt;&lt; ".";
}


Which is supposed to replicate the classic game of hangman visually. Is the code fully optimized? Is there an other way I could improve it?
I'm by far no expert, and I might be wrong on some of my statements, so take them as a basis for discussion. Anyway, I am trying to learn about code quality in C++ myself, so I'm giving it a shot.


printVector should take the argument as const-ref, because the vector is not supposed to be changed in the function call.
Consider using an iterator to loop through the vector in printVector. However, in a small-scale program like this I don't see this as a big issue. In larger programs, it might help you to make a function more general. And thanks to C++11's auto, it will be very readable -- even more so if you use a range-based for loop.
You're using the vector&lt;int&gt; guessed to store the chars from userGuess. Why not a vector&lt;char&gt; to store char? Would also make the type cast to (char) in printVector unnecessary.
Also you're mixing C-arrays and STL's std::vector, C-strings (char *) and std::string. Try to stick to the STL versions throughout. If you are using a C++11 capable compiler, you can even initialize the std::vector the same way as you're doing with the C-array now.
You are using curly braces {} in for loops and if/else statements only when absolutely required. Most style guides encourage using braces for every for/if/else block, or at most allow omitting them for one-line "blocks". The reason is readability (you can always expect to find a } where a longer block ends) and editability (It's easy to forget adding the braces when adding a line to the supposed for/if/else block).
It doesn't look like idiomatic C++ to compare a bool variable with == as in if(letterCorrect == false). You should use something like if (! letterCorrect) instead.
You should add a return 0; statement when the user is hanged. As far as I know it isn't required for the main function, but as you're already returning with return 0; when the user guesses the word, you should also do so at the end of the function.
The condition if (compWord == compHiddenWord) is wrong. You're looping over the array elements, but then you're comparing the pointer to the 0th element of the array to the std::string, instead of comparing the array elements. If you follow my suggestion above and change compHiddenWord to std::string, then you can omit the loop and simply write if(compWord == compHiddenWord) (Edit: As I learned, it also works correctly for comparisons of char* and std::string, so I deleted my example for by-character comparison)
You're using a as a loop index. While this isn't wrong, C++ programmers would typically use i or j for this purpose. 
Edit: Your hangman array goes from 0 to 9 -- but in the while loop you never reach the 9 thanks to the hangManPos != 9 condition. Therefore, you will never print the completely hanged hangman.


Let me know what you think!