Counting repetitions in a file I've written this code to get input from a file and count the repetition of number of that file and give output to another file. How can I make this code better?

#include &lt;iostream&gt;
#include &lt;cmath&gt;
#include &lt;fstream&gt;

using namespace std;

int main()
{
    ofstream outputFile("outPutData.txt");
    ifstream theFile ("inputData.txt");
    int MaxRange= 50;

    int myint[100]={0};

    int mycompare[90]={0};

    int mycount[90] = {0};

    int i = 0, j = 0, k = 0, sum = 0;

    for(j=0;j &lt;= MaxRange;j++){
        mycompare[j] = j;
    }

    do
    {
        theFile&gt;&gt;myint[i];

        for(j=0;j&lt;=MaxRange;j++)
            for(k=0;k&lt;=MaxRange;k++)
            {

                j = k;
                if(myint[i] == mycompare[j])
                    mycount[k] = mycount[k]+1;
            }
        i++;

    }
    while((myint[i-1] &gt;=0) &amp;&amp; (myint[i-1] &lt;= MaxRange));

    cout&lt;&lt; "The Number\t\t Frequency"&lt;&lt;endl;
    cout&lt;&lt;"------\t\t------"&lt;&lt;endl;


    for(k=0; k &lt;=MaxRange ;k++)
    {

        if(mycount[k] != 0)
        {
            outputFile&lt;&lt;k&lt;&lt;"  "&lt;&lt;mycount[k]&lt;&lt;endl;
            cout&lt;&lt; k&lt;&lt;"\t\t"&lt;&lt;mycount[k]&lt;&lt;endl;

            sum = sum + mycount[k];
        }
    }

    cout&lt;&lt;"Total input from file  = "&lt;&lt;sum&lt;&lt;endl;

    return 0;
}


If I have the series of number in my input file like this:




9
9
9
6
6
5
3
7
9
1
2



It gives the output:


The Number  Frequency
---------   --------
1             1
2             1
3             1
5             1
6             2
7             1
9             4
Please do not do this:

using namespace std;


I know every stupid shitty book about C++ does this. But don't. It works fine if your code is 10 lines long but anything larger it becomes a problem. So it is a really bad habit to start and breaking habits is hard so don't start it. Do yourself a favour and get into the habit of not using it. See Why is “using namespace std;” considered bad practice? for details.

This looks like it should be a constant:

int MaxRange= 50;


You should make it const. Then you can also use it as the size of the arrays below.

Declare one variable per line:

int i = 0, j = 0, k = 0, sum = 0;


The is one corner case where it does not do what you expect. But that's not the real reason for not doing this. Readability. And declare the variable as close to the point of use as possible. No point in declaring a variable that is not used until the bottom line of a program. Also loop variables should be declared as part of the loop so there affect does not bleed beyond the loop.

for(int loop = 0; loop &lt; 5; ++loop)
{
}
// loop not valid here.


Also use better names. If you have a loop that covers multiple lines. Try and find all uses of the variable i. Your editor is going to hit a bunch of words in comments and other things. Which is a real pain when you are trying to fix code. 

Why do you only initialize half of the mycompare. It may be valid but a comment on why would definately be in order.

int MaxRange= 50;
int mycompare[90]={0};
for(j=0;j &lt;= MaxRange;j++){
    mycompare[j] = j;
}


After reading the code it seems that mycompare is not really needed.
Even in your loop to print out the values you don't use it.

This is an anti pattern for reading from a stream.
What happens if the read (operator>>) fails?

do
{
    theFile&gt;&gt;myint[i];
    // STUFF
}
while((myint[i-1] &gt;=0) &amp;&amp; (myint[i-1] &lt;= MaxRange));


The correct patter for reading from a file is:
Note: You will find that nearly all programming languages work this way. It is a VERY common pattern. Put the read as part of the loop conditional to validate the read worked.

while(theFile&gt;&gt;myint[i])
{
    // Read worked so do work.
}


You can combine your other conditions:

while((theFile&gt;&gt;myint[i]) &amp;&amp; (myint[i] &gt;=0) &amp;&amp; (myint[i] &lt;= MaxRange))
{
    // Read worked and value in the correct range.
}


Try to always use {} around block statements. It will save your butt one day. 

    for(j=0;j&lt;=MaxRange;j++)
        for(k=0;k&lt;=MaxRange;k++)


Prefer

    for(j=0;j&lt;=MaxRange;j++)
    {
        for(k=0;k&lt;=MaxRange;k++)
        {
        }
    }


Not sure what the outer loop is doing here.

    for(j=0;j&lt;=MaxRange;j++)              // This line seems redundant.
        for(k=0;k&lt;=MaxRange;k++)
        {
            j = k;                        // because of this line.
            if(myint[i] == mycompare[j])
                mycount[k] = mycount[k]+1;
        }


You may as well remove the outer loop.

Prefer not to use std::endl to mark end of line. Perfer '\n'. The difference is that std::endl puts the '\n' character on the stream then flushes the stream. If you don't need the flush do not use it. It makes the stream much more efficient not to use it.

cout&lt;&lt; "The Number\t\t Frequency"&lt;&lt;endl;
cout&lt;&lt;"------\t\t------"&lt;&lt;endl;


In C++ the return in main() is optional.
If you only return 0 then I would encourage you not put the return in the code. It is an indication that the program can not logically fail. 

return 0;


Conclusion:

You need to learn how to use the built in types. This problem becomes trivial to do by using the std::map.

int main()
{
    std::map&lt;int,int&gt;   data;
    int  tmp;

    // Read the data
    ifstream theFile ("inputData.txt");
    while(theFile &gt;&gt; tmp)
    {
        ++data[tmp];
    }

    // Write the data 
    for(auto loop = data.begin(); loop != data.end();++loop)
    {
         std::cout &lt;&lt; loop-&gt;first &lt;&lt; "\t\t" &lt;&lt; loop-&gt;second &lt;&lt; "\n";
    }
}