| | |
character count
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Aug 2006
Posts: 13
Reputation:
Solved Threads: 0
hey guys,
i am trying to get my code to count the number of characters in a file. However, the program is continuously going through the characters (so it goes from a-z thousands of times) when i want it to say how many times 'a' occured and 'b' and so on, in the text file.
When i change my while loop to:
i manage to get all the words to display with the amount of characters in each word. I am struggling to make the transition from calculating how many in a word to how many of each letter.
Any help will be appreciated.
i am trying to get my code to count the number of characters in a file. However, the program is continuously going through the characters (so it goes from a-z thousands of times) when i want it to say how many times 'a' occured and 'b' and so on, in the text file.
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> #include <string> using namespace std; void characterCount(char ch, int list[]); void writeTotal(int list[]); void initialize(int list[]); int main() { int letterCount[26]; string fileName; ifstream inStream; inStream.open("C:\\data.txt"); if (inStream.fail()) { cout << "could not open input file"; return 1; } while (!inStream.eof()) { writeTotal(letterCount); } system("PAUSE"); return 0; } void initialize(int list[]) { for(int j=0; j<26; j++) list[j]=0; } void characterCount(char ch, int list[]) { int index; ch=tolower(ch); index=static_cast<int>(ch)-static_cast<int>('a'); if (0 <= index & index <26) list[index]++; } void writeTotal(int list[]) { int index; for(index=0; index<26; index++) cout<<static_cast<char>(index +static_cast<int>('a')) <<"("<<list[index]<<")"<<endl; }
C++ Syntax (Toggle Plain Text)
while(!inStream.eof()) { inStream>>fileName; cout<<fileName<<" ("<<fileName.size()<<")"<<endl; }
Any help will be appreciated.
First off, lets look at two code segments. The first is your original while loop, the second is writeTotal.
Your loop is based of whether you've reached the end of the file. If you have not, you call writeTotal. That outputs the total from the array. It doesn't change the file, so you keep looping (thousands of times, if you let it go that long).
Second, using eof() might not be the best way to check for file state (see http://www.daniweb.com/techtalkforum...155265-18.html; and kudos to Dave Sinkula for posting it in another recent thread.
)
[edit:] So I tinkered with the code a little bit to get it working, and there were some things I noticed. You don't initialize letterCount, so whatever values you get as your result will likely be on the order of -1203455802 or similar. That can be fixed by calling
Next, you never call characterCount. This is the critical part of your program. I'd recommend that you change it to accept a string instead of a character, but that's up to you. Either way, there's also no check in place to make sure the char is a letter or not. You can
Third, writeTotal should not be inside the loop, unless you want to see all 26 counts each time one of them changes. A little extreme in my opinion.
C++ Syntax (Toggle Plain Text)
/* ... */ // the while loop: while (!inStream.eof()) { writeTotal(letterCount); } /* ... */ // writeTotal void writeTotal(int list[]) { int index; for(index=0; index<26; index++) cout << static_cast<char>(index+static_cast<int>('a')) << "(" << list[index] << ")" << endl; }
Second, using eof() might not be the best way to check for file state (see http://www.daniweb.com/techtalkforum...155265-18.html; and kudos to Dave Sinkula for posting it in another recent thread.
)[edit:] So I tinkered with the code a little bit to get it working, and there were some things I noticed. You don't initialize letterCount, so whatever values you get as your result will likely be on the order of -1203455802 or similar. That can be fixed by calling
initialize(), or just having int letterCount[26] = {0}; and the compiler will autofill the array with 0.Next, you never call characterCount. This is the critical part of your program. I'd recommend that you change it to accept a string instead of a character, but that's up to you. Either way, there's also no check in place to make sure the char is a letter or not. You can
#include <cctype> to get the isalpha(char) function. Third, writeTotal should not be inside the loop, unless you want to see all 26 counts each time one of them changes. A little extreme in my opinion.
Last edited by Infarction; Sep 14th, 2006 at 3:38 am.
•
•
Join Date: Aug 2006
Posts: 13
Reputation:
Solved Threads: 0
thanks for your prompt reply. i called initialized letterCount and the letters come up, except with 0 next to each one, so this i gather is where i need to actually get the characterCount happening so. I was thinking that maybe
will work... but it doesn't show up anything fullstop. This might be where the eof is messing me up.
I 'can' use eof though can't i, its just im using the incorrect code!?
thanks again
C++ Syntax (Toggle Plain Text)
while (!inStream.eof()) { characterCount(ch, letterCount); }
I 'can' use eof though can't i, its just im using the incorrect code!?
thanks again
Last edited by insamd; Sep 14th, 2006 at 4:03 am.
Yeah, you can use eof(). If you read the example in the link I posted, you might get duplicate data.
The problem with the code segment you just posted is that you're still not reading anything from the file. (You also have nothing to pass to characterCount() becaouse of it.) You could do something along the lines of:
The problem with the code segment you just posted is that you're still not reading anything from the file. (You also have nothing to pass to characterCount() becaouse of it.) You could do something along the lines of:
C++ Syntax (Toggle Plain Text)
string temp; while (inStream >> temp) // loop as long as you can read data from the file { for(int i = 0; i < temp.length(); ++i) characterCount(temp[i], letterCount); // pass each letter to characterCount() }
![]() |
Similar Threads
- Front Page error when opening .HTM file (HTML and CSS)
- Delphi character count (Pascal and Delphi)
- keeping a frequency of each character entered. (C)
- I'm not that bright! (C)
- Count.Java (Java)
- Perl Whitespaces (Perl)
Other Threads in the C++ Forum
- Previous Thread: having touble can anyone help
- Next Thread: using ifstream, counting elements of each line in a dat file
| Thread Tools | Search this Thread |
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count database delete deploy developer dll download dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph gui homeworkhelp iamthwee ifstream image input int java lib library linker list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






