Hi,
I have been working on this small console applications for 2-3 days now but I cannot find the problem.

The Task is that I have to read a file which has sentences in english and I just have to get the frequency for each character and print it out on the screen like this:
Character Frequency
a 1

etc.

Now the problem with my code is that it is counting the first character properly but for all other characters I get a zero Value, Here is my code:
It is a function..

void Character_Freq()
{
  ifstream In_File; 
  char c;
  int i,count;
  In_File.open("infile.txt");

  cout<<"Characters\tFrequency"<<endl;

  if(In_File.is_open())
  {
	  for(i=97;i<=122;i++)
	  {
		  count = 0;
          c = (char)i;

		while(!In_File.eof())
		{
			if((char)In_File.get()== c)
			{
			  count++;
			}
			else
			{
			//Don't Count 
			}
		}
                
                  In_File.close();

		cout<<(char)i<<"\t\t"<<count<<endl;
		
		In_File.open("infile.txt");
	  }
	  
  
  }

  else
  {
  
    cout<<"\nFile was unreadable";
  
  }

  In_File.close();
}

Now I don't know what I am missing, it would be really helpful if someone can spot the mistake thanks.

And another thing is that I am not allowed to use any more variables in this program.
So in other words I can only use One Char, two Ints and One file streamer

Thanks in Advance

I dont know wats wrong with ur code. But when i executed it myself it worked perfectly except for the fact that it doesnt cound uppercase characters. for that u can use the tolower() function on the characters read from the file and then compare it with c.

Take a look:
CODING:

#include <iostream>
#include <fstream>
//#include <ctype>


using namespace std;

void Character_Freq()
{
  ifstream In_File("infile.txt");
  char c;
  int i,count;
 // In_File.open("infile.txt");

  cout<<"Characters\tFrequency"<<endl;

  if(In_File.is_open())
  {
	  for(i=97;i<=122;i++)
	  {
		  count = 0;
          c = (char)i;

		while(!In_File.eof())
		{
			if(tolower((char)In_File.get())== c)
			{
			  count++;
			}
			else
			{
			//Don't Count
			}
		}

                  In_File.close();

		cout<<(char)i<<"\t\t"<<count<<endl;

		In_File.open("infile.txt");
	  }


  }

  else
  {

    cout<<"\nFile was unreadable";

  }

  In_File.close();


}

int main()
{
    Character_Freq();
    return 0;
}

Input file was:

WiBit was a website
University in Baltimore

Output was:

Characters Frequency
a 3
b 3
c 0
d 0
e 4
f 0
g 0
h 0
i 7
j 0
k 0
l 1
m 1
n 2
o 1
p 0
q 0
r 2
s 3
t 4
u 1
v 1
w 3
x 0
y 1
z 0

That's strange..

I am using MS Visual C++ compiler and I am using windows 7 and my System is 64bit.

What compiler are you using?

Im using gcc 4.2 and win 7 and 32 bit.

Do u get any warnings ?? Have u created the file already and entered some data into it?

I don't get any warnings and yes I have created the file already. I don't have a single clue why it is behaving like this... Anyone with deep knowledge of Compilers and OS's might be able to figure this out

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.