hi,
I'm trying to create a program that will read a file and then check the file for each letter of the alphabet and then output the letter and the number of times it appears in the file, into a new file... this is what i have so far but it's not working.. if anyone could help that would be nice!
thanks
int main()
{
cout << "\n To find the number of occurrences of each letter in the alphabet in a specific file,"
<< "enter the file name:";
string inFile;
cin >> inFile;
cout << "\\nEnter the name of the output file:";
string outFile;
cin >> outFile;
ifstream fin\(inFile.data\(\)\);
assert \(fin.is_open\(\)\);
ofstream fout\(outFile.data\(\)\);
assert \(fout.is_open\(\)\);
vector<int> numoccurrences\(26\);
int i;
char letter;
for\(;;\)
\{
fin >> letter;
char lowercase = tolower\(letter\);
if \('a'<= lowercase <='z'\)
numoccurrences[ lowercase - 'a']\+\+;
\}
char ch = 0;
for \(int i = 1; i < numoccurrences.size\(\); i\+\+\)
\{
fout << ch << numoccurrences[i];
ch\+\+;
\}
I like the use of C++ to make a simple problem more convoluted.
#include <stdio.h>
int main(int argc,char **argv) {
static int occur[256];
int i=' ';
while (1) {
int c=getchar();
if (c==EOF) break;
occur[(unsigned char)c]++;
}
while (i < 127) {
if (occur) printf("%c: %d\n",i,occur);
i++;
}
return 0;
}
i'm not really sure what alot of that stuff means.. i'm new to C++... i don't know what arcg and **argv mean.. also is this just for a paragraph of 256 letters? and why do you assign int i = 32? and i also dont know what return 0 does...
the problem is when i start without debugging, after i've entered the file names, an popup comes up that says debug assertion failed.. vector subscript is out of range
ch is supposed to be counting from 'a' to 'z' and then outputting all the numbers
this is due today so don't worry about trying anymore, but thanks anyways!