Like Tree7Likes

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

This is a discussion on I want my program to count all the words, and characters within a file within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> #include <fstream> #include <iomanip> #include <string> #include <cstdlib>   using namespace std;   void getFileName(ifstream& input) //opens ...

  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
    DRK
    DRK is offline
    Registered User
    Join Date
    Jun 2009
    Posts
    111
    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
    DRK
    DRK is offline
    Registered User
    Join Date
    Jun 2009
    Posts
    111
    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
    Posts
    23,027
    You can use std::map to map strings to integers for counting.
    And fix your indentation.
    rogster001 likes this.
    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
    22,304
    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".
    Elysia and rogster001 like this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    22,304
    Yes, because you did not specify the array size, hence the array size is the number of elements in the initialiser, which is 1.
    Elysia likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    2,590
    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.
    Elysia likes this.
    Code:
    namespace life
    {
        const bool change = true;
    }

  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
    22,304
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 1 of 4 1234 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 06-16-2012, 01: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-23-2011, 12:57 AM
  4. count words program problem
    By Tony654321 in forum C Programming
    Replies: 8
    Last Post: 10-19-2004, 12:23 PM
  5. Replies: 2
    Last Post: 05-05-2002, 02:38 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21