I am trying to read in a file from the command line, read the characters into an array, count the characters individuality and print the results. the code compiles without any errors but the individual letter counts are much higher than they should be for large files and it does not count at all sometimes for small files.
#include <iostream>
#include <fstream>
#include <string.h>
#include <cctype>
#include <algorithm>
using namespace std;
int main(int argc, char **argv){
if(argc !=2){
cout << "usage: " << argv[0] << " <filename>\n";
}
else{
ifstream myfile (argv[1]);
if(!myfile.is_open()) //check to see if file is opened.
cout << "Can not open your file\n";
else{
static int array[26]; //stores the frequency of letters.
char t;
while(!myfile.eof()){
while(myfile.get(t)){
if(isupper(t)){
int i = 0;
do{
array[tolower(t)-'a']++;
i++;
}
while(i < 26);
}
}
int x = 0;
while(x < 26){
cout << 'a' + x << ":" << array[x] << endl;
x++;
}
}
}
}
return 0;
}