![]() |
| | #1 |
| Registered User Join Date: Oct 2001
Posts: 37
| Character counting program 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 << " "; } |
| TankCDR is offline | |
| | #2 |
| Registered User 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;
}
matheo917 |
| matheo917 is offline | |
| | #3 |
| Just because Join Date: Jan 2002
Posts: 2,502
| |
| ygfperson is offline | |
| | #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. |
| TankCDR is offline | |
| | #5 |
| Just because Join Date: Jan 2002
Posts: 2,502
| i can read it, it just looks nicer when it looks different from regular conversation |
| ygfperson is offline | |
| | #6 |
| ¡Amo fútbol! Join Date: Dec 2001
Posts: 2,123
| Don't include fstream and iostream in the same program because fstream includes iostream. |
| golfinguy4 is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Help with number counting program | Turtal | C Programming | 11 | 04-25-2009 02:40 PM |
| need help with 'character counting from a .txt file' | master_vst | C Programming | 5 | 11-09-2008 02:17 PM |
| Program about counting how many times a word appeared in a txt file | jamesjackxie | C Programming | 2 | 12-02-2007 05:40 AM |
| Character handling help | vandalay | C Programming | 18 | 03-29-2004 05:32 PM |
| character occurrence program not working | Nutshell | C Programming | 6 | 01-21-2002 10:31 PM |