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 -
vector<int> occurrencesVec(26);
-
int i;
-
for(;;)
-
{
-
fin >> i;
-
if (fin.eof()) break;
-
else
-
{
-
if (('A'<=i<='Z') || ('a'<=i<='z'))
-
{
-
char lowercase = tolower(i);
-
occurrencesVec[lowercase - 'a']++;
-
}
-
}
-
}
-
for (int i = 1; i < occurrencesVec.size(); i++)
-
{
-
fout << i << occurrencesVec[i];
-
}
-
}
-
5 4520
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?
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 >>
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??
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
If you are using: -
vector<int> data;
-
-
cout << data[256]; //ERROR. There is no element 256
-
Usually, the 256 is the value of a variable: -
vector<int> data;
-
-
cout << data[i]; //ERROR.
-
and the i has a bad value.
For arrays, the value between the [] is called the index value.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Dung Ping |
last post by:
Such as:
<script>
//code
aaaa
zzzz
ccc
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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;
|
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..
...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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...
| |