Counting characters in a program C++ NEED ASSISTANCE

May 9, 2012 at 11:58pm
I am wondering what syntax to use in order to count how many characters there are in my .txt file. In my .txt file is the alphabet (ABCDEFGHIJKLMNOPQRSTUVWXYZ), and that is all.

Here is what I have so far:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{

string letters = "";



ifstream inFile;
inFile.open("Intermediate21.txt");

if(inFile.is_open())
{
getline(inFile, letters);

cout << letters << endl;

inFile.close();
}
else
cout << "cannot open file." << endl;


system("pause");
return 0;

}



So I am able to display the .txt file but I also have to cout the amount of characters there are also (26).


Thanks.

Last edited on May 10, 2012 at 2:23am
May 10, 2012 at 12:31am
I suppose that you mean alpha characters. Otherwise you can use member function size() to determine how many characters the string contains. If so than you should use isalpha function. For example

1
2
int count = std::count_if( letters.begin(), letters.end(),
                                      [] ( char c ) { return ( std::isalpha( c ) ); } );
Last edited on May 10, 2012 at 12:32am
May 10, 2012 at 12:41am
And where would I put that :o

inside of the if(inFile.is_open()) statement?
May 10, 2012 at 12:58am
1
2
3
4
5
6
7
8
9
10
11
12
if(inFile.is_open())
 {
 getline(inFile, letters);

 cout << letters << endl;
 
cout << std::count_if( letters.begin(), letters.end(),
                                  [] ( char c ) { return ( std::isalpha( c ) ); } )
       << endl;

 inFile.close();
 }
May 10, 2012 at 1:05am
Okay.. and then do I need to replace anything inside the [] or anything because it won't compile lol

I get:

expected primary-expression before '[' token
expected primary-expression before ']' token
expected primary-expression before "char"
May 10, 2012 at 10:19am
It seems that your compiler does not support the new C++ Standard that is lambda expressions.
You can change the lambda expression to a functional object.

1
2
3
4
5
6
7
8
9
10
11
12
struct count_alpha: std::unary_function<char, bool>
{
   bool operator ()( char c ) const
   {
      return ( std::isalpha( c ) );
   }
};

//....

std::cout << std::count_if( letters.begin(), letters.end(), count_alpha() ) 
              << std::endl;
May 10, 2012 at 1:18pm
It's really sufficient to just call isalpha directly, with a disambiguating cast:

std::count_if( letters.begin(), letters.end(), (int(*)(int))std::isalpha )

unless you're being excessively pedantic and coding for the imaginary standards-compliant implementation where <cctype>'s std::isalpha takes defaulted arguments past the int.

Also, the above samples count the letters per line. To count the entire file, iterate over the entire file:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <fstream>
#include <cctype>
#include <iostream>
#include <iterator>
#include <algorithm>

int main()
{
    std::ifstream inFile("Intermediate21.txt");
    std::cout << std::count_if( std::istreambuf_iterator<char>(inFile),
                                std::istreambuf_iterator<char>(),
                                (int(*)(int))std::isalpha ) << '\n';
}
Topic archived. No new replies allowed.