EvilZone
Programming and Scripting => C - C++ => : Daemon 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.
#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
-
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.
-
I don't read C/C++ but don't do:
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?
-
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
-
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://en.wikipedia.org/wiki/Recursion_(computer_science))
http://www.danzig.us/cpp/recursion.html (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.
-
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 :)
-
Good job mate , add the single words frequency meter that tells you how many times a single character is repeated till entered value .
-
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 (http://en.wikipedia.org/wiki/Markov_chain) from your word counter project.