Thread: Character counting program

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    37

    Character counting program

    I am working or trying to figure out what the best way would be to count different characters in a file.


    I need to open a file, read the file 1 character at a time, counting the different letters of the alphabet. Upper and lowercase letters count as the same, IE: 'A' and 'a' are the same.

    Anything besides letters are totaled up together except spaces.

    I attempted to convert a program that counts the frequency of words that you enter but I get several errors. I believe that these errors occur because I am trying to use a character instead of a string that the book used.

    Before you start criticizing the code, please remember that I am a beginner C++ programmer and feel that this is over my head.
    Can anyone help?

    If this is not the best or simplist way to accomplish this, any suggestions?


    #include <iostream>
    #include <string>
    #include <vector>
    #include <set>
    #include <map>
    #include <iterator>
    #include <algorithm>
    #include <cctype>
    #include <fstream>

    using namespace std;

    char toLower(char ch) { return tolower(ch); }
    string & ToLower(string & st);
    void display(const string & s);

    int main()
    {

    vector<char> file;


    // create output stream object for new file
    ifstream fin("class.dat");

    // check to make sure the file is opened
    if(!fin.is_open())
    {
    cerr << "Could not open file for input!" << endl;
    return 0;
    }

    char ch;

    while (fin.get(ch)) //takes 1 ch at a time from file 1
    file.push_back(ch);


    fin.close();

    // Place words in set

    set<char> wordset;

    transform(file.begin(), file.end(),
    insert_iterator<set<char> > wordset,
    wordset.begin()), ToLower);

    cout << "Letters and Count" << endl;
    for_each(wordset.begin(), wordset.end(), display);
    cout << endl;


    // place word and frequency in map
    map<char, int> wordmap;
    set<char>::iterator si;
    for (si = wordset.begin(); si != wordset.end(); si++)
    wordmap[*si] = count(file.begin(), file.end(),
    *si);

    // display map contents
    cout << "Letter frequency:\n";
    for (si = wordset.begin(); si != wordset.end(); si++)
    cout << *si << ": " << wordmap[*si] << endl;


    return 0;

    }

    string & ToLower(string & st)
    {
    transform(st.begin(), st.end(), st.begin(), toLower);
    return st;
    }

    void display(const string & s)
    {
    cout << s << " ";
    }

  2. #2
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    i would love to dig into your code but i just don't have enough strength today..
    sorry for not much effort, i'll try to redeem myself at the next occasion

    try this out......maybe it can suite you...

    Code:
     #include <fstream>
    #include <iostream>
    #include <deque>
    #include <string>
    #include <cstdlib>
    #include <algorithm>
    using namespace std;
    
    typedef unsigned short USHORT;
    
    void main()
    {
    
    deque<string> text;
    string tempStr[100];
    
    ifstream fin("c:\\My Documents\\prose.txt");
    ofstream fout("c:\\My Documents\\together.txt");
    
    if(!fin.is_open() || !fout.is_open()){
    cerr << "\n\aError: Unable to open prose.txt for input.";
    exit(EXIT_FAILURE);
    }
    else{
    // Enter ad write records to the file and deque
                    USHORT i = 0;
    while(i < 100)
       {
          getline(fin,tempStr[i], char(32));
          tempStr[i].resize(4);
    
          for (int j=0;j<tempStr[i].size();j++)
            {
                tempStr[i][j] = tolower(tempStr[i][j]);
            }
    
          text.push_front(tempStr[i]);
          i++;
       } // End of while(i < 25)...
    } // End of else...
    
    
    
    deque<string>::iterator itr;
    sort(text.begin(),text.end());
    
    // Count the number of times a given word appears
    // in the deque of string objects.
    itr = text.begin();
    cout << endl;
    
    while(itr < text.end())
       {
            cout << "The word --- " << *itr << " --- appears "
                 << count(text.begin(),text.end(),*itr)
                 << " times. " << endl;
    
    
            fout << "The word --- " << *itr << " --- appears "
                 << count(text.begin(), text.end(), *itr)
                 << " times. " << endl;
    
         itr++;
    
    }
    
    // Display the contents of the deque
    itr = text.begin();
    while(itr < text.end()) cout << endl << *itr++;
    
    
    int yu;
    cin >> yu;
    }
    hope this helps...


    matheo917

  3. #3
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    Standard reply:
    AAAAAH!! NO CODE TAGS!!!
    http://www.cprogramming.com/cboard/s...threadid=13473

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    37

    RE: Code Tags

    I read the post on code tags and did not the:

    [tag]




    [end tag]

    on the sample code. As I see it, the code tags are to help make the code readable. Indentation and whitespace also help make the code more readable. Even as a beginner, I may not understand exactly what the code accomplishes, i can surely read through it. Those who cannot understand it, probably cannot help me.

    I am not trying to be sarcastic, but complaining about code tags when the code is mostly readable seems petty.

    Sorry if I offend anyone.

  5. #5
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    i can read it, it just looks nicer when it looks different from regular conversation

  6. #6
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    Don't include fstream and iostream in the same program because fstream includes iostream.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with number counting program
    By Turtal in forum C Programming
    Replies: 11
    Last Post: 04-25-2009, 02:40 PM
  2. need help with 'character counting from a .txt file'
    By master_vst in forum C Programming
    Replies: 5
    Last Post: 11-09-2008, 02:17 PM
  3. Replies: 2
    Last Post: 12-02-2007, 05:40 AM
  4. Character handling help
    By vandalay in forum C Programming
    Replies: 18
    Last Post: 03-29-2004, 05:32 PM
  5. character occurrence program not working
    By Nutshell in forum C Programming
    Replies: 6
    Last Post: 01-21-2002, 10:31 PM