hi; i need to count d number of occurrences of all the chars in a file. i can't figure out how to count how many of each char there are. this wat i have up to now.
i need the to print only the ASCII code from 0-127

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

ifstream fin;
 	 void count(int & w );
 void main()

{
int sum[128];
int w;	
char s;
char charin;
char infile[20];

cout<< "enter file name"<< endl;
cin>> infile;
cout<<infile;

cout<< " " << endl;
	
	fin.open(infile);
	
	
	if ( fin.fail())
	 { 
		 cout<< " cannot find file" << endl;
		 exit(0);
	} 
else
{	
while(!fin.eof())
{
	fin.get(charin);
	cout<< charin;

	w=static_cast<int>(charin);
	s=static_cast<char>(w);

	 cout<< "= "<<w<<" ";
	 

	 void count ( int & w);
	 {	 for (int i=0; i<128 ; i++);
		 {
			 if(w==i)
			 {
				 sum[i]++;
				 
			 }
		 }
	 }	
	 }
	
	  }  
 }

>>void main()
read this

As for your problem:

int main()
{
   int sum[256] = {0};
   ifstream in("filename");
   char ch;
   while( in.get(&ch) )
   {
         if(ch > 0)
             ++sum[ch];
   }
   // now just display all the rows in sum that are greater than 0
}

Some addition.
The char type may be signed or unsigned in C++ (it's an implementation-defined issue).
If char is unsigned, the code if (ch > 0) does not work (all chars >= 0).
Portable way (one of) to do it:

if ((ch & ~0177) == 0)
{ ... // OK, ASCII char

Be careful: isascii(ch) from <ctype.h> is not standard function/macros.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.