string letters[ 26 ] = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
// count is an array of 26 integers and the elements of array count have been initialized to zero
string::iterator my_iter;
int count = 0;
int alpha = 0;
string sent;
cout << "Please input a sentence or series of characters:";
cin >> sent;
cout << endl;
while (alpha < 26)
{
for (my_iter = sent.begin(); my_iter != sent.end(); my_iter++)
{
if (letters[alpha].compare(my_iter) != 0)
{
count++;
}
}
cout << "The number of occurences of the letter " << letters[alpha] << "is " << count << endl;
count = 0;
alpha++;
}
return 0;
}
This is the error that I'm getting from it...
error C2664: 'int std::basic_string<_Elem,_Traits,_Ax>::compare(const std::basic_string<_Elem,_Traits,_Ax> &) const' : cannot convert parameter 1 from 'std::_String_iterator<_Elem,_Traits,_Alloc>' to 'const std::basic_string<_Elem,_Traits,_Ax> &'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>
1> ]
1> and
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Alloc=std::allocator<char>
1> ]
1> and
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>
1> ]
1> Reason: cannot convert from 'std::_String_iterator<_Elem,_Traits,_Alloc>' to 'const std::basic_string<_Elem,_Traits,_Ax>'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Alloc=std::allocator<char>
1> ]
1> and
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>
1> ]
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
Make the string array a character array.
Then again, it isn't strictly necessary anyway:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
cout << "Please input a sentence or series of characters: ";
string sentence;
getline(cin,sentence);
int frequencies[26]={0};
for (string::iterator it=sentence.begin();it!=sentence.end();++it)
if (*it>='a' && *it<='z')frequencies[*it-'a']++;
for (int i=0;i<26;i++)cout << "The number of occurrences of the letter " << char('a'+i) << " is " << frequencies[i] << endl;
}
I figured I was trying to do something incorrect with the compare aspect. It's so ridiculous how shorter and more....elegant yours is. I'm a psych major that is forced to take a c++ class so hopefully that excuses some of my ignorance.
Is there a way that I could solve this problem interpreting the characters as numbers, since C language considers all characters to have an integer value.
Lastly, with this program it does not consider uppercase words so I'm trying to force the string into lowercase before it is evaluated but Im failing. I'll try to figure it out on my own but i appreciate the help.
So the one problem I'm finding with this code is that it doesn't accomodate a whole file or stream. It just does one line. Do you know why this is the case?
Do you know which line it gives it on?
I am trying to figure out how that might of happened.... ch is a value between 0-127 is alpha makes sure its A...Z or a...z, tolower makes it uniform. ch - 'a'?
#include <iostream>
#include <fstream>
#include <cstring>
#include <cctype>
usingnamespace std;
int main()
{
fstream inFile;
char ch;
int frequencies[26]={0};
inFile.open("SomeTextFile.txt");
while(!inFile.eof())
{
inFile >> ch;
cout << "Char: " << ch << "-- " << (int)ch << " --Count =";
// if(isalpha(ch))
if( ((ch>='a')&&(ch <= 'z')) ||
((ch>='A')&&(ch <= 'Z')) )
{
ch= tolower(ch);
cout << ++frequencies[ch - 'a'] << endl;
}
else cout << endl;
}
for (int i=0;i<26;i++)
{
cout << "The number of occurrences of the letter ";
cout << char('a'+i) << " is " << frequencies[i] << endl;
}
}
What you are telling me is 'isalpha(ch)' failed. This is probably my smallest case yet to find that stack corruption bug in gcc/mingw, with the calls to the standard c++ c lib.