C++ console based - "Word Guessing" game I have made a console game in C++ which is called "Word Guessing", and according to the compiler, it is using about 542 KB of memory. 

I am mainly wondering if it's possible to decrease the memory usage some more, and if possible, how it can be improved.

#include &lt;iostream&gt;
#include &lt;fstream&gt;
#include &lt;string&gt;
#include &lt;vector&gt;
#include &lt;algorithm&gt;

using namespace std;

// Function Declaration
string getRandWord();
void checkLetter(char&amp;, string&amp;, int&amp;, string&amp;);

// main function
int main() {

    // str Variables
    string RandomWord = getRandWord();
    string hiddenWord = "";

    // int Variables
    unsigned long int _length_ = RandomWord.length();
    int chances = int(_length_)+1;
    char newLetter;

    // fill varialbe with characters
    hiddenWord.append((_length_),'_');

    cout &lt;&lt; "The Word is "&lt;&lt; _length_ &lt;&lt;" letters long." &lt;&lt; endl;

    // game loop
    while(chances &gt; 0){
        // request (char) input
        cin &gt;&gt; newLetter;
        checkLetter(newLetter, RandomWord, chances, hiddenWord);

        // check if player won
        if(hiddenWord == RandomWord){
            cout &lt;&lt; "Congratulations! You won the game." &lt;&lt; endl;
            break;
        }
        // decrease try chances
        chances--;
    }

    return 0;
}


// Functions

void checkLetter(char &amp;newLetter, string &amp;randstr, int &amp;strlen, string &amp;hiddenWord){
    // initiate iterator it
    string::iterator it;

    // iterate trough string
    for(auto it = randstr.begin(); it != randstr.end();++it){
        // validate letter mach
        if(*it == newLetter){
            // replace letter
            hiddenWord.at(it - randstr.begin()) = newLetter;
        }
    }
    cout &lt;&lt; hiddenWord &lt;&lt; endl;
}

string getRandWord(){
    string filePath = "/Users/nedimkanat/XCODE/testcpp/testcpp/";

    // constants
    enum sizes {
        COUNTER = 0,
        ARRAY_SIZE = 5
    };

    // set seed
    srand((unsigned)time(NULL));

    // get random int between 0 and 5
    int randint = rand() % ARRAY_SIZE;

    // str to store each line from file
    string str;

    // array to store 5 (random) words
    vector&lt;string&gt; arr;

    // initialize file object &amp; open file
    ifstream file(filePath+"words.txt");
    int counter = COUNTER;

    // loop trough file
    if (file.is_open()){
        while (getline(file,str) &amp;&amp; counter &lt; ARRAY_SIZE){
            arr.push_back(str);
            counter++;
        }
        file.close();
    } else {
        cout &lt;&lt; "File is not open" &lt;&lt; endl;
    }

    // send away random word
    if(arr.empty()){
        cout &lt;&lt; "CANCER" &lt;&lt; endl;
    }
    return arr.at(randint);
}
Well, there is much to improve with your code, so let's begin at the beginning:


You should avoid using namespace std; It's a plague and just biding its time to bite you later.
Why is “using namespace std;” considered bad practice?
You might want to rework your logic regarding the tries. It's somewhat cruel to let the user go on guessing after he already cannot win anymore. And it's somewhat pointless if he can ask for every single character.
You should const-qualify what you can. Doing so makes logic-errors compile-errors, which can be caught by the compiler.
Use auto where you can. Remember that repetition is error-prone and everything you don't write nobody ever has to understand and check. Also known as the "Don't repeat yourself" (DRY) principle.
In that vein, I would also reorder your functions so you don't have to provide forward-declarations.
Consider everything but main() for static linkage, as it is never used outside the TU.
I'm sorry, but at best your comments are a blatant violation of DRY. Never repeat the code in the comments, write about why the code does something surprising or at least not quite straight-forward.
Consider consistent casing. Most of your variables begin with lower-case, why not all? Also, consider consistently having one space around binary operators.
There's nothing quite as useless as explicitly requesting something which will happen implicitly anyway. Aside from immediately afterwards undoing it.

std::string hiddenWord = "";
unsigned long int _length_ = randomWord.length();
hiddenWord.append((_length_),'_');
// Has the same effect as
std::string(randomWord.length(), '_');

Avoid beginning and/or ending names with an underscore. Even if you know the rules, it's error-prone and ugly.
Casts, function-style or not, are a last resort. And if you must use them, consider using the new-style casts as they are more prominent and explicitly name your intent.
Anyway, just use the right type.
Don't use std::endl unless you really need a manual flush. First, it's way uglier than a simple newline '\n', second, it kills performance as flushing is costly, and third, it's superfluous.
The standard input- and output-streams std::cin and std::cout are coupled so using the latter flushes the former if used with an interactive console.
If you want to do something a specific number of times, use a for-loop, that's what it's for.
Always test whether input succeeded. A pipe for example could be closed prematurely, or a file run out.
Also, you might want to re-try until you get something.
I wonder why checkLetter doesn't return whether now everything is revealed.
return 0; is implicit for main() in C++ and C99+.
rand and srand is quite a bad random-number-generator. Still, if you insist on using it, consider initializing it directly in main() to make sure you never inadvertently initialize it twice. And especially to make sure you don't repeatedly re-initialize it before using it again.
Take a look at &lt;random&gt; for better RNGs.
There's absolutely no reason to limit yourself to the first five words in the input-file. Or to fail if the file contains less words.
All error output should be send to the error stream, not standard output. That's what it's there for.
Consider accepting the input-filename as an argument, instead of hardcoding it. You might even give it a default.
Extract the task of reading the opening and reading the file. std::istream_iterator might come in handy for it.
Why does checkLetter() accept everything by modifiable reference? hiddenWord should be, sure, but randstr (which should be named randomWord for consistency) should be a constant reference, newLetter a value and strlen is superfluous.
That function also has two variables named it, the first one should be summarily removed.


Modified code:

#include &lt;iostream&gt;
#include &lt;fstream&gt;
#include &lt;string&gt;
#include &lt;vector&gt;
#include &lt;algorithm&gt;
#include &lt;iterator&gt;
#include &lt;string_view&gt;

static bool checkLetter(
    char newLetter,
    std::string_view randomWord,
    std::string &amp;hiddenWord
) {
    for(auto it = randomWord.begin(); it != randomWord.end(); ++it)
        if(*it == newLetter)
            hiddenWord.at(it - randomWord.begin()) = newLetter;
    std::cout &lt;&lt; hiddenWord &lt;&lt; '\n';
    return randomWord == hiddenWord;
}

static auto readFile(const char* filename) {
    std::ifstream file(filename);
    using isi = std::istream_iterator&lt;std::string&gt;;
    std::vector&lt;std::string&gt; arr{isi(file), isi()};
    return arr;
}

static void removeBadEntries(std::vector&lt;std::string&gt;&amp; arr) {
    std::sort(arr.begin(), arr.end());
    arr.resize(std::remove_if(arr.begin(), arr.end(),
        [last = std::string_view()](auto&amp; x) mutable{
            for(auto y : x)
                if(!isgraph((unsigned char)y))
                    return true;
            if(last == x)
                return true;
            last = x;
            return false;
        }) - arr.begin());
}

static auto getRandWord(const char* filename) {
    auto arr = readFile(filename);
    removeBadEntries(arr);
    std::cout &lt;&lt; "Choosing from " &lt;&lt; arr.size() &lt;&lt; " unique valid entries.\n";

    return arr.empty() ? std::string("CANCER") : std::move(arr[rand() % arr.size()]);
}

int main(int argc, char* argv[]) {
    std::setlocale(LC_ALL, "");
    srand((unsigned)time(NULL));

    const auto randomWord = getRandWord(argc &gt; 1
        ? argv[1]
        : "/Users/nedimkanat/XCODE/testcpp/testcpp/words.txt");
    std::string hiddenWord(randomWord.length(), '_');

    std::cout &lt;&lt; "The Word is " &lt;&lt; randomWord.length() &lt;&lt; " letters long.\n";

    for(auto chances = randomWord.length() + 1; chances &gt; 0; --chances){
        char c;
        do {
            if(!(std::cin &gt;&gt; c)) {
                std::cerr &lt;&lt; "Error reading a character from stdin.\n";
                return EXIT_FAILURE;
            }
        } while(!std::isgraph((unsigned char)c));

        if(checkLetter(c, randomWord, hiddenWord)) {
            std::cout &lt;&lt; "Congratulations! You won the game.\n";
            break;
        }
    }
}