Author Topic: Word Counter [C++]  (Read 3142 times)

0 Members and 1 Guest are viewing this topic.

Offline Daemon

  • VIP
  • Baron
  • *
  • Posts: 845
  • Cookies: 153
  • A wise man fears a gentle mans anger
    • View Profile
Word Counter [C++]
« on: July 03, 2012, 05:06:19 am »
I'm sure your all more than capable of writing this for yourself, but why do that when I already have one written? Simply copy and paste, then run. I used CodeBlocks to develop it in case that matters.

Functions: Enter the location/name of a textfile. Then the number of words you wish to find, finally the words you wish to count and then let the program do the rest.


Code: (cpp) [Select]
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;


struct wordsToFind   //keeps track of the keywords and how many of each their are
{
    string word;
    int count;
};


int main ()
{

    int numberOfWords;
    wordsToFind *keyWords;
    bool goOn=true;
    char answer;

    ifstream inStream;
    string input;
    string fileName;

    while (goOn==true)
    {


        cout << "What is the name of the file to read from?" << endl;
        cin >> fileName;

        fileName += ".txt";
        inStream.open(fileName.c_str());







        cout << "How many words would you like to count? " << endl;
        cin >> numberOfWords;
        cin.ignore();

        keyWords= new wordsToFind[numberOfWords];

        string templine;

        for (int i=0; i<numberOfWords; i++)  //assigns keywords and saves memory for them
        {

            cout << "Enter word number " << i+1 << endl;
            getline(cin, templine);
            keyWords[i].word = templine;
            keyWords[i].count=0;

        }
        for (int i=0; i<numberOfWords; i++)
        {
            cout << keyWords[i].word << endl;
        }

        if (inStream.is_open()) //checks to see if you can open the file (does it exist?)
        {
            while ( inStream.good() ) //keep reading until you are at the end of the file
            {

                inStream >> input;

                for (int i=0; i<numberOfWords; i++)
                {
                    if (input==keyWords[i].word)   //checks the words in the file against the keywords you have assigned in your array
                    {
                        keyWords[i].count++;
                    }
                }

            }
            inStream.close();
        }
        else cout << "Unable to open file";


        for (int i=0; i<numberOfWords; i++)
        {
            cout << keyWords[i].word << ": ";
            cout << keyWords[i].count << endl;
        }

        cout << "Would you like to use this program on another file? [Y]es or [N]o" << endl;
        cin >> answer;

        if (answer=='Y' || answer=='y')
        {
            goOn=true;
        }
        if (answer=='N' || answer=='n')
        {
            goOn=false;
        }


    }

    cout << "\nThank you for using the word count program. Goodbye." << endl;

    delete [] keyWords;
    return 0;
}


I just figured it's time to stop browsing and start interacting. So here is something I can give back to you guys, it ain't much. But it's something

Cheers!

Daemon
« Last Edit: July 17, 2012, 05:24:04 pm by Daemon »
This lifestyle is strictly DIY or GTFO - lucid

Because sexploits are for h0edays - noncetonic


Xires burns the souls of HF skids as a power supply

Offline techb

  • Soy Sauce Feeler
  • Global Moderator
  • King
  • *
  • Posts: 2350
  • Cookies: 345
  • Aliens do in fact wear hats.
    • View Profile
    • github
Re: Word Counter [C++]
« Reply #1 on: July 03, 2012, 05:55:46 am »
Looks good so far.


For an update, try and make it recursive. Will be good practice. Or add other features, such as frequency of words used, most used letter, and so on.


Anyway, keep coding, and show us what you got.
>>>import this
-----------------------------

Offline Kulverstukas

  • Administrator
  • Zeus
  • *
  • Posts: 6627
  • Cookies: 542
  • Fascist dictator
    • View Profile
    • My blog
Re: Word Counter [C++]
« Reply #2 on: July 03, 2012, 08:04:43 am »
I don't read C/C++ but don't do:
Code: [Select]
if (answer=='Y' || answer=='y')Instead, use the ".toLowercase()" function (it's like that in Java).

The code needs more spaces too, and I bet you are not following the conventions at all, or is the code supposed to be jumbled?

Offline Daemon

  • VIP
  • Baron
  • *
  • Posts: 845
  • Cookies: 153
  • A wise man fears a gentle mans anger
    • View Profile
Re: Word Counter [C++]
« Reply #3 on: July 03, 2012, 08:09:58 am »
Jumbled how? I thought i spaced it out really well compared to other snippets of C++ i've seen from my course last semester.
Am I missing some conventions?


And thanks for the tips guys, i'll look into it. I know I'm not an excellent programmer yet. But I'm working on it!

*edit, i looked up recursive. thanks techb
« Last Edit: July 03, 2012, 08:11:57 am by Daemon »
This lifestyle is strictly DIY or GTFO - lucid

Because sexploits are for h0edays - noncetonic


Xires burns the souls of HF skids as a power supply

Offline techb

  • Soy Sauce Feeler
  • Global Moderator
  • King
  • *
  • Posts: 2350
  • Cookies: 345
  • Aliens do in fact wear hats.
    • View Profile
    • github
Re: Word Counter [C++]
« Reply #4 on: July 03, 2012, 08:13:07 am »
Jumbled how? I thought i spaced it out really well compared to other snippets of C++ i've seen from my course last semester.
Am I missing some conventions?


And thanks for the tips guys, i'll look into it. I know I'm not an excellent programmer yet. But I'm working on it!

*edit, i looked up recursive. thanks techb


http://en.wikipedia.org/wiki/Recursion_(computer_science)


http://www.danzig.us/cpp/recursion.html


I seen the edit as I was posting a reply. The examples on the second link will help at least.
>>>import this
-----------------------------

Offline Daemon

  • VIP
  • Baron
  • *
  • Posts: 845
  • Cookies: 153
  • A wise man fears a gentle mans anger
    • View Profile
Re: Word Counter [C++]
« Reply #5 on: July 03, 2012, 08:23:27 am »
Lol that was the link i clicked off of google. I should know better than to ask before consulting the all-knowing google. Thanks for finding that though techb. I'll definitley look into it :)

This lifestyle is strictly DIY or GTFO - lucid

Because sexploits are for h0edays - noncetonic


Xires burns the souls of HF skids as a power supply

Offline Frankenstinejoe

  • NULL
  • Posts: 3
  • Cookies: 0
  • GoOoing BLaCk ... !!!
    • View Profile
Re: Word Counter [C++]
« Reply #6 on: July 24, 2012, 12:32:41 pm »
Good job mate , add the single words frequency meter that tells you how many times a single character is repeated till entered value .
Asta Lavista Baby ...!!!

Offline Xires

  • Noob Eater
  • Administrator
  • Knight
  • *
  • Posts: 379
  • Cookies: 149
    • View Profile
    • Feed The Trolls - Xires
Re: Word Counter [C++]
« Reply #7 on: September 17, 2012, 02:55:41 pm »
At some point, soon, I'll pull the pasted code apart and show ways to improve upon it.  For the time being, however, let me suggest that you try creating a Markov Chain from your word counter project.
-Xires