counting numbers from a file

I am trying to count the Xs, Bs Is and Ws that exist in a file and output a summary of the count.

ERROR: No error! the console window closed before any display is showed.

void Cruise:: displaySummmary()
{
std::cout << "Begin reading file.\n";

ifstream fin;
int letterCount[26]={0};
char next , ch;
int a= 0;

fin.get(next);

fin.open("cabinConfig.txt");
if (fin.fail( ))
{
cout << "Input file opening failed.\n";
exit(1);
}
while(!fin.eof())
{
if (next == '-')
ch = 'X';
//out_stream << ch;

// Increment your count here while you are reading
a++;
fin.get(next);
}
cout << "Number of Reserved cabins is : " << a << endl;


fin.close();
}
Well, your while loop makes no sense, though I don't think that could break the program.
Are you sure there is something to stop the app in the end? You should also put the same thing before every exit() you use.
So whats your opinion?
This is my latest code, but it counts to zero.I want it to give me a summary of each type of specific character in the fil.They are just 4 (abc and d)

ifstream fin;
int letterCount[26]={0};
string fileName;
char next , ch;
int a= 0;

//fin.get word;

fin.open("cabinConfig.txt");
if (fin.fail( ))
{
cout << "Input file opening failed.\n";
exit(1);
}
while(!fin.eof())
{
if (next == 'a')
ch = 'X';
//cout << ch;

// Increment your count here while you are reading
a++;
fin.get(next);
}
As I said, your while loop makes no sense.
'ch = 'X'' has no effect on anything, your not using letterCount anywhere...
Try this instead:
1
2
3
4
5
while(!fin.eof()){
    char c = fin.get();//get the char. You may want to use tolower here if you want this to work with capital letters too
    if(c >= 'a' && c <= 'z')//check if its a letter. I think there's a function isalpha that does this
        letterCount[c-'a']++;//increment the appropriate element
}

Topic archived. No new replies allowed.