Thread: I want my program to count all the words, and characters within a file

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    45

    I want my program to count all the words, and characters within a file

    Code:
    #include<iostream>
    #include<fstream>
    #include<iomanip>
    #include<string>
    #include<cstdlib>
     
    usingnamespace std;
     
    void getFileName(ifstream& input) //opens file
    {
        string fileName;
        cout << "Enter the file name: ";
        cin >> fileName;
        input.open(fileName.c_str());    
    if( !input )
            {
                cout << "Incorrect File Path" << endl;
                exit (0);
            }
    }
    void countWords(ifstream& input)  //counts words in file
    {
    bool notTrue = false;
        string words;
    int i = 0;
    while( notTrue == false )
        {
    if( input >> words )
            {
                i++;
            }
    elseif( !(input >> words) )
                notTrue = true;
        }
        cout << "There are " << i << " words in the file." << endl;
    }
    void countChars(ifstream& input, char storeCharacters[])  //counts amount of characters in file
    {
    int i = 0;
    int counter = 1;
        string line;
        
    for( i=0; i<counter; i++ )
                {
                    input.get(storeCharacters[i]);
                    counter++;
    if ( !input )
                        counter--;
                }
    
    }
    void sortChars()  //sorts characters
    {
    }
    void printCount()  //prints
    {
    }
    int main()
    {
        ifstream input;
        ofstream output;
    char storeCharacters[] = {0};
        getFileName(input);
        countWords(input);
        countChars(input, storeCharacters);
    return 0;
    }
    

  2. #2
    Registered User
    Join Date
    Apr 2013
    Posts
    45
    I am stuck under the countChar() function. I do not know how to read in all the characters from file into a char array. I will need to read all the characters in and sort them later using the char array. Please help.

  3. #3
    Registered User
    Join Date
    Jun 2009
    Posts
    120
    You may use set container to store and sort objects.
    Code:
    #include <set>
    
    void countWords(ifstream& input)  //counts words in file
    {
        set<string> words_set;
        string word;
        while (input >> word)
        {
            words_set.insert(word);
        }
        cout << "There are " << words.size() << " words in the file.\n";
        set<string>::const_iterator it;
        for (it = words_set.begin(); it < words_set.end(); ++it)
        {
            cout << *it << '\n';
        }
    }

    Quote Originally Posted by thomann061 View Post
    Code:
    notTrue = true;
    Have you ever heard of an oxymoron?

  4. #4
    Registered User
    Join Date
    Jun 2009
    Posts
    120
    Correction:
    set won't count all the words, because duplicate words will be ignored, you may use vector instead.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You can use std::map to map strings to integers for counting.
    And fix your indentation.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Apr 2013
    Posts
    45
    Quote Originally Posted by Elysia View Post
    You can use std::map to map strings to integers for counting.
    And fix your indentation.
    I have not learned "map." I would like some advice on how to put characters into an char array. Since this code should work on any size of file, I am having trouble placing each individual character into an array.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Then learn how to use it. For counting characters, std::map or std::unordered_map is quite unnecessary, but such a mapping container is very appropriate for counting "words".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Apr 2011
    Posts
    62
    Quote Originally Posted by thomann061 View Post
    I have not learned "map." I would like some advice on how to put characters into an char array. Since this code should work on any size of file, I am having trouble placing each individual character into an array.
    well, right now you are initializing your storage like this:
    Code:
    char storeCharacters[] = {0};
    what size do you expect that array to be?

  9. #9
    Registered User
    Join Date
    Apr 2013
    Posts
    45
    I thought that was used to initialize the array, and give it a starting point. Are you telling me it only has room for one character?

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes, because you did not specify the array size, hence the array size is the number of elements in the initialiser, which is 1.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Apr 2013
    Posts
    45
    That makes a lot more sense. I have been playing around with the strcpy() function... How could I set it up to copy a line of text into a char array?

  12. #12
    Registered User
    Join Date
    Apr 2013
    Posts
    45
    For example, I cannot copy a string "line" into an array of characters...
    Code:
    getline(input, line);
    				strcpy(storeCharacters, line);
    

  13. #13
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by thomann061 View Post
    That makes a lot more sense. I have been playing around with the strcpy() function... How could I set it up to copy a line of text into a char array?
    why do you want to use an array of characters? this is C++. just use std::string. it works very much like a char array, but it's much safer.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  14. #14
    Registered User
    Join Date
    Apr 2013
    Posts
    45
    I have to use an array of some sort. Are you saying to use a string array?

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Elkvis suggested that you use std::string as a substitute for an array of char. Internally, std::string would have a dynamic array of char.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 06-16-2012, 12:51 PM
  2. Replies: 2
    Last Post: 12-02-2011, 09:19 PM
  3. c program to count words
    By bigwillys111 in forum C Programming
    Replies: 3
    Last Post: 04-22-2011, 11:57 PM
  4. count words program problem
    By Tony654321 in forum C Programming
    Replies: 8
    Last Post: 10-19-2004, 11:23 AM
  5. Replies: 2
    Last Post: 05-05-2002, 01:38 PM