hey guys,
i am trying to get my code to count the number of characters in a file. However, the program is continuously going through the characters (so it goes from a-z thousands of times) when i want it to say how many times 'a' occured and 'b' and so on, in the text file.

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void characterCount(char ch, int list[]);
void writeTotal(int list[]);
void initialize(int list[]);
int main()
{
    int letterCount[26];
 string fileName;
 ifstream inStream;
 inStream.open("C:\\data.txt");
 if (inStream.fail()) 
    {
  cout << "could not open input file";
  return 1;
    }
    
    while (!inStream.eof())
    {
         writeTotal(letterCount);
    }
  system("PAUSE");
  return 0;
}
void initialize(int list[])
{
     
     for(int j=0; j<26; j++)
             list[j]=0;
}
void characterCount(char ch, int list[])
{
     int index;
     ch=tolower(ch);
     
     index=static_cast<int>(ch)-static_cast<int>('a');
     
     if (0 <= index & index <26)
           list[index]++;
}
void writeTotal(int list[])
{
     int index;
     
     for(index=0; index<26; index++)
                  cout<<static_cast<char>(index
                                             +static_cast<int>('a'))
                  <<"("<<list[index]<<")"<<endl;
}

When i change my while loop to:

while(!inStream.eof())
      {
         inStream>>fileName;
         cout<<fileName<<" ("<<fileName.size()<<")"<<endl;
      }

i manage to get all the words to display with the amount of characters in each word. I am struggling to make the transition from calculating how many in a word to how many of each letter.
Any help will be appreciated.

Recommended Answers

All 4 Replies

First off, lets look at two code segments. The first is your original while loop, the second is writeTotal.

/* ... */
  // the while loop:
  while (!inStream.eof())
  {
    writeTotal(letterCount);
  }
/* ... */
// writeTotal
void writeTotal(int list[])
{
  int index;
  for(index=0; index<26; index++)
    cout << static_cast<char>(index+static_cast<int>('a')) << "(" << list[index] << ")" << endl;
}

Your loop is based of whether you've reached the end of the file. If you have not, you call writeTotal. That outputs the total from the array. It doesn't change the file, so you keep looping (thousands of times, if you let it go that long).

Second, using eof() might not be the best way to check for file state (see http://www.daniweb.com/techtalkforums/post155265-18.html; and kudos to Dave Sinkula for posting it in another recent thread. ;))

[edit:] So I tinkered with the code a little bit to get it working, and there were some things I noticed. You don't initialize letterCount, so whatever values you get as your result will likely be on the order of -1203455802 or similar. That can be fixed by calling initialize() , or just having int letterCount[26] = {0}; and the compiler will autofill the array with 0.

Next, you never call characterCount. This is the critical part of your program. I'd recommend that you change it to accept a string instead of a character, but that's up to you. Either way, there's also no check in place to make sure the char is a letter or not. You can #include <cctype> to get the isalpha(char) function.

Third, writeTotal should not be inside the loop, unless you want to see all 26 counts each time one of them changes. A little extreme in my opinion.

commented: you are helpful, keep it up [~s.o.s~] +3

thanks for your prompt reply. i called initialized letterCount and the letters come up, except with 0 next to each one, so this i gather is where i need to actually get the characterCount happening so. I was thinking that maybe

while (!inStream.eof())
    {
          characterCount(ch, letterCount);
    }

will work... but it doesn't show up anything fullstop. This might be where the eof is messing me up.
I 'can' use eof though can't i, its just im using the incorrect code!?
thanks again

Yeah, you can use eof(). If you read the example in the link I posted, you might get duplicate data.

The problem with the code segment you just posted is that you're still not reading anything from the file. (You also have nothing to pass to characterCount() becaouse of it.) You could do something along the lines of:

string temp;
  while (inStream >> temp) // loop as long as you can read data from the file
  {
    for(int i = 0; i < temp.length(); ++i)
      characterCount(temp[i], letterCount); // pass each letter to characterCount()
  }
commented: very helpful indeed, helped me through my problem +1

ahhh how silly of me!! thanks, i managed to get it working! just fixed up my code a little and voila im seeing the results.
thanks for your help, appreciated!!! incrementing your rep :p

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.