Why was `unsigned int` causing the program to crash?
<p>I had to make a program that checks if two words can be obtained one from the other by permutating the letters. I wrote the code as you can see below and I struggled quite a long time to find out why the program is crashing, and at some point I  had the idea to change the fundametal type of these variables from <code>unsigned int</code> (<code>unsigned int code = 0, i, j, counter = 0, ok = 1;</code>) to <code>integer</code> (<code>int code = 0, i, j, counter = 0, ok = 1;</code>) and now it works properly but I don't know what changed so that now it works. I know (please correct me if I'm wrong) that <code>unsigned int</code> goes from 0 (including 0) to a very large number. All the variables were not going under 0, so I don't know why it didin't work in the <code>unsigned int</code> version and how the fundamental type changing solved the crash.</p>

<pre><code>#include&lt;iostream&gt;
#include&lt;string.h&gt;

using namespace std;

int main()

{   char word1[50], word2[50];
    unsigned int code = 0, i, j, counter = 0, ok = 1;

    cout &lt;&lt; "Type in the first word and then press ENTER:";
    cin.getline(word1, 50);
    cout &lt;&lt; "Type in the second word and then press ENTER:";
    cin.getline(word2, 50);
    cout &lt;&lt; endl;

    if (strlen(word1) == strlen(word2)) //If the two words don't have the same lenght, it's impossible to obtain one word of the other by permutating it's letters.

    {   for (i = 0; word1[i]; i++)  /*This nested FOR will generate a number in the code variable. The first digit will indicate how many times will the letter word1[0] be found in word2,
                                    the second digit will indicate how many times will the letter word1[1] be found in word2 and so on until the end of word1*/
        {   counter = 0;
            for (j = 0; word2[j]; j++)
                if (word1[i] == word2[j])
                    counter += 1;

            code = code * 10 + counter;
        }

        i = strlen(word1) - 1;

        while (i &gt;= 0 &amp;&amp; ok)    /*In this loop we are checking if the code is valable also for the word1 itself. If it is, it means that the words can be obtained one of the other by permutating the letters*/
        {   counter = 0;        
            for (j = strlen(word1) - 1; j &gt;= 0; j--)
                if (word1[i] == word1[j])
                    counter++;

            if (counter == code % 10)
                ok = 1;
            else
                ok = 0;

            code = code / 10;
            i--;
        }

        if (ok)
            cout &lt;&lt; "Yes, the words can be obtained one of the other by permutating the letters.";
        else
            cout &lt;&lt; "No, the words can not be obtained one of the other by permutating the letters.";
    }

    else
        cout &lt;&lt; "No, the words can not be obtained one of the other by permutating the letters.";

    cin.get();
}
</code></pre>