Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
2.75/5 (4 votes)
See more:
#include "StdAfx.h"
#include<string>
#include<iostream>
using namespace std ;

int main()
{
 
	string sentence = " " ;
	
	string aa[100]  ;
	float a=0,b=0,c=0,d=0,e=0,f=0,g=0,h=0,i=0,j=0,k=0,l=0,m=0,n=0,o=0,p=0,q=0,r=0,s=0,t=0,u=0,v=0,w=0,x=0,y=0,z=0;
	int ii=0;
	//int counter = 0 ;
	float maxlenght = sentence.length() - 1 ;

	cout << "Enter your sentences ( End the sentence with . ): " << endl ;
	getline(cin,sentence);


	cout << " Your sentence has " << sentence.length() - 1 << " charactures ." << endl ;

	for(ii=0 ; ii<sentence.length();ii++)
	{
		
	if( sentence[ii] == 'a' )
	{
		a++;	
	}
	if( sentence[ii] == 'b' )
	{
		b++;	
	}
	if( sentence[ii] == 'c' )
	{
		c++;	
	}
	if( sentence[ii] == 'd' )
	{
		d++;	
	}
	if( sentence[ii] == 'e' )
	{
		e++;	

	}
	if( sentence[ii] == 'f' )
	{
		f++;	
	}
	if( sentence[ii] == 'g' )
	{
		g++;	
	}
	if( sentence[ii] == 'h' )
	{
		h++;
	}
	if( sentence[ii] == 'i' )
	{
		i++;	
	}
	if( sentence[ii] == 'j' )
	{
		j++;	
	}
	if( sentence[ii] == 'k' )
	{
		k++;	
	}
	if( sentence[ii] == 'l' )
	{
		l++;
	}
	if( sentence[ii] == 'm' )
	{
		m++;	
	}
	if( sentence[ii] == 'n' )
	{
		n++;	
	}	
	if( sentence[ii] == 'o' )
	{
		o++;	
	}	
	if( sentence[ii] == 'p' )
	{
		p++;	

	}
    if( sentence[ii] == 'q' )
	{
		q++;	
	}
    if( sentence[ii] == 'r' )
	{
		r++;	
	}
	if( sentence[ii] == 's' )
	{
		s++;	
	}
	if( sentence[ii] == 't' )
	{
		t++;	
	}
					
	if( sentence[ii] == 'u' )
	{
		u++;	
	}
	if( sentence[ii] == 'v' )
	{
		v++;	
	}
	if( sentence[ii] == 'w' )
	{
		w++;	
	}
	if( sentence[ii] == 'x' )
	{
		x++;	
	}
	if( sentence[ii] == 'y' )
	{
		y++;	
	}
	if( sentence[ii] == 'z' )
	{
		z++;	
	}
	}
	cout<<"a "<<a<<endl;
	cout<<"b "<<b<<endl;
	cout<<"c "<<c<<endl;
	cout<<"d "<<d<<endl;
	cout<<"e "<<e<<endl;
	cout<<"f "<<f<<endl;
	cout<<"g "<<g<<endl;
	cout<<"h "<<h<<endl;
	cout<<"i "<<i<<endl;
	cout<<"j "<<j<<endl;
	cout<<"k "<<k<<endl;
	cout<<"l "<<l<<endl;
	cout<<"m "<<m<<endl;
	cout<<"n "<<n<<endl;
	cout<<"o "<<o<<endl;
	cout<<"p "<<p<<endl;
	cout<<"q "<<q<<endl;
	cout<<"r "<<r<<endl;
	cout<<"s "<<s<<endl;
	cout<<"t "<<t<<endl;
	cout<<"u "<<u<<endl;
	cout<<"v "<<v<<endl;
	cout<<"w "<<w<<endl;
	cout<<"x "<<x<<endl;
    cout<<"y "<<y<<endl;
	cout<<"z "<<z<<endl;
	float hh  ;
	 hh = a / maxlenght  ;
	cout << "yay" << hh;
}
Posted

There are too many things wrong with this code. I suggest you spend a lot more time reading your programming guides and learn:
how to use integers for counting,
how to use characters (or calculated values) for array indices
how to design sensible loops and decision blocks
etc.
 
Share this answer
 
Comments
Niklas L 7-Jun-11 7:05am    
Good general advice. 5.
fjdiewornncalwe 7-Jun-11 9:55am    
Agreed
Oh dear.

A suggestion for you: Set up two arrays: one with 26 characters in, filled from a string "abcdefgh...". The other with 26 integers, all preset to zero.
For each character in the input string, loop through the characters string and see if it is in there. Increment the corresponding integer in the array if it is.
Then when you print them, loop through again, printing the character and the count:
for (int iCount = 0; iCount < acCharsLengthl iCount++)
   {
   cout<<acChars[iCount]<<" appeared "<<aiCounts[iCount]<<" times."<<endl;
   }
Your code will be smaller, quicker, and easier to maintain. Then try to work out what your other problem is!
 
Share this answer
 
Comments
Chandrasekharan P 7-Jun-11 6:59am    
very well explained. I appreciate that and my 5
You know there are more compact ways to get the job done (and never use float to count integers quantities). For instance

C++
int count[256] = {0};
for(int n=0 ; n<sentence.length();n++)>
{
  count[sentence[n]]++;
}
for (int n=0; n<256; n++)
{
  if ( isalpha((char)n))
  {
    cout << (char) n << " has " <<  count[n] << " occurrences " << endl;
  }
}
 
Share this answer
 
v2
Comments
Niklas L 7-Jun-11 7:04am    
Fived.
Febil Chacko Thanikal 7-Jun-11 7:15am    
good one
Stefan_Lang 7-Jun-11 9:47am    
int count[256] = {0};
Does this do what I think you intended? Honest question - I am really not aware this would initialize the whole array.
CPallini 7-Jun-11 16:33pm    
A bit tricky, but works: just the first element is initialized with the provided initializer, the other elements are initialized with, well, 0.
Stefan_Lang 8-Jun-11 5:07am    
Wow, I've actually found that in THE book: the presence of the initializer forces the initialization of the whole array, but if the initializer list doesn't match the array size, missing values are initialized with 0.

Funny that over the course of more than 2 decades I never came across that trick before... (and never thought of it myself either :o )
The initialized value to variable "maxlenght" is zero. That is used for division.
And the string length is not assigned after the user input.
 
Share this answer
 
Comments
[no name] 7-Jun-11 7:53am    
He means

//float maxlenght = sentence.length() - 1 ;

cout << "Enter your sentences ( End the sentence with . ): " << endl ;
getline(cin,sentence);

////////////////////////////////////////////////////
float maxlenght = sentence.length() - 1 ;
////////////////////////////////////////////////////
1. learn to use loops, so you'll end up with code something more like in Solution 3. The mere length of your code makes it at least double as hard to scan for possible errors.

2. learn to read error messages - in your case you should have received a 'division by zero' message. Moreover, this is what you should have posted in your question, this is vital information immediately related to your problem!

3. learn to use your IDE and the debugger to navigate to the exact place where the error occurs - that would have shown you the line of the code:
hh = a / maxlenght  ;


With all this information it's much easier to arrive at the conclusion presented in solution 4.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900