A first Hangman game in C++ I wonder whether I included too many libraries; also can you point out obvious beginner mistakes?

Is the while loop the best for what I intend, or is there a better way?

Also, is it possible to put more into functions such that the main() looks like this:

int main()

{

call function1

call function2

etc.

return 0;

}


#include &lt;iostream&gt;
#include &lt;fstream&gt;
#include &lt;stdlib.h&gt;
#include &lt;time.h&gt;
#include &lt;string&gt;
#include &lt;vector&gt;
#include &lt;cstring&gt;
using namespace std;

string random_line()
{
    string line;
    vector&lt;string&gt; lines;
    srand(time(0));
    //input file stream
    ifstream file("wordlist.txt");
    //count number of total lines in the file and store the lines in the string vector
    int total_lines=0;
    while(getline(file,line))
    {
        total_lines++;
        lines.push_back(line);
    }
    //generate a random number between 0 and count of total lines
    int random_number=rand()%total_lines;
    //fetch the line where line index (starting from 0) matches with the random number
    return lines[random_number];
}

int main()
{
    string secret_word=random_line();
    cout &lt;&lt; secret_word; //this has to be cut at the end
    int word_length=secret_word.length();
    string guess_word="";
    string guessed_letters="";
    for (int i=0; i&lt;word_length; ++i)
    {
        guess_word.append("_");
    }
    int remaining_fails=10;
    string guess="";

    while(remaining_fails!=0)
    {
        if(guess_word==secret_word)
        {
            cout &lt;&lt; "Congratulations, the word is: "&lt;&lt;secret_word&lt;&lt;endl;
            return 0;
        }
        else
        {
            cout &lt;&lt; string(50, '\n');
            cout &lt;&lt; guess_word &lt;&lt; endl;
            cout &lt;&lt; "Remaining fails: " &lt;&lt; remaining_fails &lt;&lt; endl;
            cout &lt;&lt; "Guessed letters: " &lt;&lt; guessed_letters &lt;&lt; endl;
            cout &lt;&lt; "Guess your letter!" &lt;&lt; endl;
            cin  &gt;&gt; guess;
            if(guess.length() !=1)
            {
                cout &lt;&lt; "Please only enter one character!";
            }
            else if(strstr(secret_word.c_str(),guess.c_str()))
            {
                for(int i=0; i&lt;word_length; ++i)
                {
                    if (guess.at(0)==secret_word.at(i))
                    {
                        guess_word.at(i)=guess.at(0);
                    }
                    else
                    {

                    }
                }
            }
            else
            {
                guessed_letters.append(guess);
                remaining_fails--;
            }
        }


    }
    cout &lt;&lt; "You loose!"&lt;&lt;endl;
    return 0;
}


The wordlist.txt contains?

word
trio
list
I'm not sure if I'd use the phrase "too many", but I think you've included a header or two you'd probably be better off without.

The most obvious example would be &lt;cstring&gt;. There's rarely a good reason to use any of the &lt;cstring&gt; functions in C++ code (and I don't see any hint that this code is one of the rare exceptions either).

Right now, the main (only?) thing I see you using from &lt;cstring&gt; is strstr. std::string has a find member that can find a substring like strstr does.
Your code also seems somewhat inconsistent, at least to me. For example, in one place you create a string with N repetitions of a single character using code like: std::string(character, repetitions). In another place you do roughly the same thing using a loop to append on underscore at a time. Since they're doing the same thing, it would be better for them to do it the same way (preferably the constructor for std::string).
Right now you use a string to hold a single character (and enforce its being only one character). Since it's only a single char, I'd rather just use a char to hold it.
As a general rule of thumb, I'd prefer a well-chosen algorithm from the standard library over a raw loop.
Yes, you can (and in my opinion, should) break the code up into more functions (and based on your question, it seems like you already agree, but may not be sure how to do it).
I'd generally prefer to use the new random-number generation routines in &lt;random&gt; over the srand/rand from the C library. Likewise, I'd rather use the distribution classes in the standard library over the rand() % total_lines you have now. (but in fairness, they are more work to use, and it's unlikely to make a difference anybody's likely to notice or care about (but in many other cases, it's more important).
I'd avoid using namespace std; I know at first it seems painful to type in std:: everywhere, but it's still better than using namespace std;.
There are also neater ways to read words from a file into an array of strings, such as:

std::ifstream infile("wordlist.txt");

std::vector&lt;std::string&gt;{std::istream_iterator&lt;std::string&gt;(infile), {}};


One point though: this will produce different results if your input file has multiple words on one line. In this case your code treats a whole line as one word, but this treats each word separately.
I generally advise against using std::endl. It's probably harmless in this case, but a habit I'd try to avoid anyway.