Counting Characters

So I have an assignment and the purpose of it is to "Count the alphabetic characters in an input stream, display the totals."

And I've worked really hard to develop this code so far but I don't know what is going on with the error

#include < stdio.h>
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <iterator>
#include <string>
#include <math.h>


using namespace std;


int main()
{

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

Any help would be much appreciated. Thank you!

Richard
There's no version of compare taking a string iterator:
http://www.cplusplus.com/reference/string/string/compare/

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>
using namespace 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?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <fsteram>
#include <cstring>

using namespace std;

int main()
{
     fstream inFile;
     char ch;
     int frequencies[26]={0};

     inFile.open("SomeTextFile.txt")
     while(!inFile.eof())
     {
           inFile >> ch;
           if(isalpha(ch))
          { 
                 ch= tolower(ch);
                 frequencies[ch - 'a']++;
           }
     }

     for (int i=0;i<26;i++)
    {
          cout << "The number of occurrences of the letter ";
          cout << char('a'+i) << " is " << frequencies[i] << endl;
     }
}


I could replace the file stream with a string stream fed by a cin and do basically the same algorithm.
Last edited on
Doing that code gives me a debug assertion error

It references,

expresion: (unsigned int)(c+1) <= 256
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'?

What's your build platform?

The only way to debug is, after

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <fstream>
#include <cstring>
#include <cctype>
using namespace 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.
Last edited on
It's noting the problem on line 56

I'm also running visual studio 2008 but I can get the 2010 version if this is really a problem.
The code works as expected if it can find the file correctly. I didn't put any file checking stuff in.
Last edited on
Topic archived. No new replies allowed.