473,396 Members | 1,760 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

count the frequency of each letter of the alphabet in a file

3
so i'm attempting to make a program that reads in a file and then counts the frequency of each letter of the alphabet in the file and then outputs the letter with the frequency into a different file..
i didn't include the first part of the program because i'm pretty sure it's correct... i really have no clue what's wrong with this so any help would be great

Expand|Select|Wrap|Line Numbers
  1.     vector<int> occurrencesVec(26);
  2.     int i;
  3.     for(;;)
  4.     {
  5.         fin >> i;
  6.         if (fin.eof()) break;
  7.         else
  8.         {
  9.         if (('A'<=i<='Z') || ('a'<=i<='z'))
  10.         {
  11.             char lowercase = tolower(i);
  12.             occurrencesVec[lowercase - 'a']++;
  13.         }
  14.         }
  15.     }
  16.     for (int i = 1; i < occurrencesVec.size(); i++)
  17.     {
  18.         fout <<  i << occurrencesVec[i];
  19.     }
  20. }
  21.  
Dec 3 '07 #1
5 4520
Ganon11
3,652 Expert 2GB
1) Why are you reading in to an integer if there are going to be characters? Try making i a char.

2) Are there words in this file, or just letters separated by whitespace? If there are words, you may want to try fin.get rather than the >> operator.

Other than these minor things, your code looks fine. What exactly is not working?
Dec 3 '07 #2
svd
3
1) Why are you reading in to an integer if there are going to be characters? Try making i a char.

2) Are there words in this file, or just letters separated by whitespace? If there are words, you may want to try fin.get rather than the >> operator.

Other than these minor things, your code looks fine. What exactly is not working?
the file is just a bunch of words seperated by whitespaces, so i changed the fin >> i; to fin.get(i).. is that what you meant?

to answer your second question, the program builds fine, but when I "start without debugging" after I enter the infile name and the outfile name, a pop-up comes up that says "Debug assertion failed... vector subscript out of range"

it still does this after changing the >>
Dec 3 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
file is just a bunch of words seperated by whitespaces, so i changed the fin >> i; to fin.get(i).. is that what you meant?

to answer your second question, the program builds fine, but when I "start without debugging" after I enter the infile name and the outfile name, a pop-up comes up that says "Debug assertion failed... vector subscript out of range"

it still does this after changing the >>
The >> operator cannot be used for strings. It skips whitespace so all you get is one word of the string. If that's your situation, then you are OK. Otherwise, use cin.getline() to fetch multiple word strings.

The debug assertion failure is almost certainly using the vector operator[] with an invalid index number. The only values you can use are the ones between 0 and size()-1 of the vector. Is this a case of an uninitialized index??
Dec 3 '07 #4
svd
3
The >> operator cannot be used for strings. It skips whitespace so all you get is one word of the string. If that's your situation, then you are OK. Otherwise, use cin.getline() to fetch multiple word strings.

The debug assertion failure is almost certainly using the vector operator[] with an invalid index number. The only values you can use are the ones between 0 and size()-1 of the vector. Is this a case of an uninitialized index??
what does uninitialized index mean? i'm trying to look at all the letters individually in the paragraph and put the frequency for each letter into a vector
Dec 3 '07 #5
weaknessforcats
9,208 Expert Mod 8TB
If you are using:
Expand|Select|Wrap|Line Numbers
  1. vector<int>  data;
  2.  
  3. cout << data[256];    //ERROR. There is no element 256
  4.  
Usually, the 256 is the value of a variable:
Expand|Select|Wrap|Line Numbers
  1. vector<int>  data;
  2.  
  3. cout << data[i];    //ERROR. 
  4.  
and the i has a bad value.

For arrays, the value between the [] is called the index value.
Dec 3 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Dung Ping | last post by:
Such as: <script> //code aaaa zzzz ccc
8
by: Pete | last post by:
I need to write a program to do a frequency analysis on a string of text. Firstly I need to get the alphabet A to Z for comparison but I'm not sure how to go about this. Is the following the...
8
by: Mansi | last post by:
Given the following declaration: String letter = "A"; Is there a way that I can increment letter so that "B" is returned? Is there a way that I can add an offset so that let's say, "G" is...
19
by: jason_box | last post by:
I'm alittle new at C and I'm trying to write a simple program that will record the frequency of words and just print it out. It is suppose to take stdin and I heard it's only a few lines but I'm...
1
by: Rahul | last post by:
Hi, I tried many option, but did not found solution. Same Validation Error showing. If I have convert one table, it can easily converted. But If I have paste another table in same file, then...
2
by: Protoman | last post by:
How would I write a user defnable letter swapping algorithm? I've written an Enigma encrypting program, and I need to redo the plugboard function. Right now, the letter swapping is fixed. I need to...
4
evilmonkey
by: evilmonkey | last post by:
Is there a way to use index of in java to pull the number of occurrences of every letter in a user defined sentence? I can brute force this but is there a faster more elegant way to achieve the same...
3
by: waynejr25 | last post by:
can anyone debug my program and get it to run. #include <fstream> #include <iostream> #include <string> #include <cstdlib> #include <map> using namespace std;
13
by: umpsumps | last post by:
Hello, Here is my code for a letter frequency counter. It seems bloated to me and any suggestions of what would be a better way (keep in my mind I'm a beginner) would be greatly appreciated.. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.