954,483 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Logical Problem in the Program

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

Software guy
Junior Poster
158 posts since May 2008
Reputation Points: 16
Solved Threads: 18
 

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

stevanity
Posting Whiz in Training
293 posts since Oct 2010
Reputation Points: 43
Solved Threads: 28
 

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?

Software guy
Junior Poster
158 posts since May 2008
Reputation Points: 16
Solved Threads: 18
 

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?

stevanity
Posting Whiz in Training
293 posts since Oct 2010
Reputation Points: 43
Solved Threads: 28
 

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

Software guy
Junior Poster
158 posts since May 2008
Reputation Points: 16
Solved Threads: 18
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You