Thread: help with letter counting part of program

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    10

    help with letter counting part of program

    got most of this too work, i talked to my teacher and he gave me some hints but i cannot get it full. The idea is to get the program to count specifically how many "a"'s there continueing through all the 26 letters. I cannot figure out exactly out how to get that part to work. I want to use the function "tolower" to get the letters all lower case when going into the program. we just need to count the number of each specific letter not distinguish between upper and lower case. A file is being read into a file.
    here the code

    Code:
    
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <iomanip>
    using namespace std;
    
    
    void printArray(const int [], int);
    
    int main ()
    {
    	unsigned int strlength;
    	int alphabet[26] = {0};
    	int charnum = 0;
    	char fileName[30];
    	char str[26];
    	char letter = 'a';
    	
    
    	cout << "Enter the name of the input file"; // opens file
    
    	cin >> fileName;
    
    	ifstream infile(fileName);
    
    	if ( !infile ) 
    	{
    		cerr << "Cannot open the input file .\n "; // error occurs if bad file name
    
    		return (1);
    	}
    	
    	while ((infile >> str) && (*str != EOF)) // continues until end of file
    	{										//counts char without spaces
    		string line;
    		getline(infile, line);
    		int lentemp = line.size();
    		charnum += lentemp;
    	}
    	cout << "The number of char without spaces is " << charnum << endl;
    
    
    	while ((infile >> str) && (*str != EOF))//counting the number of each letter
    	{ 
    		if (97 <= (static_cast<int>(tolower('a'))) <= 122)
    		
    		alphabet[static_cast<int>('a') - 97]++;
    	}		
    	printArray(alphabet,  26 ); // prints historgram for frequency of letter
    
    	
    
    	infile.close ();
    
    	for (int i = 1; i <= 26; i++)
    	{
    		cout << "alpha[ " << i << "] = " << alphabet[i] << endl; 
    	}		
    	return ( 0 );
    
    }
    
    void printArray(const int a[], int size) // function for historgram
    {
    	for (int i= 0; i < size; i++)
    	{ 
    	if ( i % 20 == 0 )
    		cout << endl;
    		cout << setw(2) << a[i];
    	}
    }
    Thats the entire program the part im having problems with is
    Code:
    while ((infile >> str) && (*str != EOF))//counting the number of each letter
    	{ 
    	if (97 <= (static_cast<int>(tolower('a'))) <= 122)
    		
    	alphabet[static_cast<int>('a') - 97]++;
    	}
    alphabet is an array that should count all the letters from a to z and count the occurance of each.
    please help, im very confused
    thanks very much
    goo

  2. #2
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    The first problem with the code is your if statement. Unfortunately you cannot do multiple comparisons, you need to break them up into separate statements. For example, you can't do:
    Code:
    if (9<=x<=15)
    you need to use the AND statement:
    Code:
    if (x>=9 && x<=15)
    The second problem is the statement:
    Code:
    static_cast<int>(tolower('a'))
    You don't want to check the integer value of 'a' you want to check the integer value of a particular character in your input string.

  3. #3
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    Ok counting letters in a sentence very simple indeed.


    It should be straight forward to adapt when using files. I'll leave that and the conversion from upper to lower case to you.

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    
    int main()
    {
        int lettercount[27];
        char alphabet[28]={"abcdefghijklmnopqrstuvwxyz"};
        
        //initialise lettercount
        for (int a=0; a<26; a++)
        {
            lettercount[a]=0;
        }
        
        char sentence[81]={"treenef"};
        //use function to automatically determine
        //the size of the sentence;
        int size=strlen(sentence);
        
        for(int i=0; i<size; i++)//iterate through sentence
        {
            for(int j=0; j<26; j++)
            {
                if(sentence[i]==alphabet[j])//check what letter
                {
                    lettercount[j]++; //increment letter count
                }
            }
        }
        
        //print out results
        cout<<sentence<<endl;
         for (int a=0; a<26; a++)
        {
            
            cout<<alphabet[a]<<":";
            cout<<lettercount[a]<<" ";
            cout<<""<<endl;
        }
        
        
        
        int stop;
        cin>>stop;
    }

  4. #4
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    I don't know about you but I got this far:

    I've encrypted the source code to prevent you copying it.

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <iomanip>
    
    using namespace std;
    
    void treenef( float [],int);
    void treen_ef(float [], int);
    
    int main ()
    {
    	unsigned int strlength;
    	char tre_en_ef[27] ={"abcdefghijklmnopqrstuvwxyz"};
    	char fileName[30];
    	char str[26];
    	
    	
        float trre_en_ef[27];
        for(int a=0; a<26; a++)
        {
            trre_en_ef[a]=0;
        }    
    	cout << "Enter the name of the input file:"; // opens file
    
    	cin >> fileName;
    
    	ifstream infile(fileName);
    
    	if ( !infile ) 
    	{
    		cerr << "Cannot open the input file .\n "; 
    
    		return (1);
    	}
    	
    	while ((infile >> str) && (*str != EOF)) // continues until end of file
    	{										
    	    int trre_en_efs=strlen(str);
    	    for (int a=0; a<trre_en_efs; a++)
    	    {
    	        for(int b=0; b<26; b++)
    	        {
    	            if(str[a]==tre_en_ef[b])
    	            {
    	                trre_en_ef[b]++;
    	            }
                }        
    	                
    	    }    
    	    	
    	}
    		
    	
       
    	
    
    	infile.close ();
    
    	for (int i = 0; i < 26; i++)
    	{
    		cout << "alpha[ " << tre_en_ef[i] << "] = " 
            << trre_en_ef[i] << endl; 
    	}
       //call function
       treenef(trre_en_ef,26);
       
       cout<<""<<endl;
     
        int tree;
        cin>>tree;		
    	return ( 0 );
    
    }
    /*========================================
      This function treenefs the data
      which is necessary to print out the
      histogram
      ========================================*/
    
    void treenef( float a[],int trre_en_efs)
    {
        //get tree
        float b[26];
        float t_re=0;
        for(int i=0; i<trre_en_efs; i++)
        {
            if(a[i]>t_re)
            {
                t_re=a[i];
            }
        }
       
        
        for (int i=0; i<trre_en_efs; i++)
        {
            b[i]=(a[i]/t_re)*20;
            
        }     
        //call function print array
        treen_ef(b,trre_en_efs);       
    }     
    
        
    /*=======================================================
      This function prints out the histogram
      It is slightly more complicated than a horizontal
      histogram
      =======================================================*/        
    void treen_ef(float a[], int trre_en_efs) // function for historgram
    {
        char tre_en_ef[27] ={"abcdefghijklmnopqrstuvwxyz"};
        char tr_re_en_efs[27][21];
        char new_tr_re_en_efs[21][27];
     
        for (int i= 0; i < trre_en_efs; i++)
    	{ 
    	   for(int j=0; j<20; j++)
    	   {
    	       if(j<=int(a[i]))
    	       {
    	       tr_re_en_efs[i][j]='*';
    	       }  
               else
               {
                   tr_re_en_efs[i][j]=' ';
               }      
           }        
                       
    	}
    
        
       
        int y=19;
        for (int i= 0; i < 20; i++)
    	{  int x=0;
    	   for(int j=0; j<26; j++)
    	   {
    	       new_tr_re_en_efs[i][j]=tr_re_en_efs[x][y];
    	       x++;
    	       
    	       
    	   }
    	   
    	  y--;
        
        } 
           cout<<""<<endl;
    	   cout<<"                       ======================="<<endl;
           cout<<"                       |  H I S T O G R A M  |"<<endl;
           cout<<"                       ======================="<<endl;
          for (int i= 0; i < 20; i++)
    	  { 
    	   for(int j=0; j<26; j++)
    	   {
    	       cout<<new_tr_re_en_efs[i][j]<<setw(3);
    	   }
           cout<<""<<endl;
          } 
          
    	
    	
    	
    	cout<<""<<endl;
    	cout<<"---------------------------------------------------------";
    	cout<<"-------------------";
    	cout<<""<<endl;
    	 for (int i= 0; i < trre_en_efs; i++)
    	{ 
    
    		cout << tre_en_ef[i]<<setw(3);
    		
    	}
    	
    	
    }

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    Quote Originally Posted by treenef
    I don't know about you but I got this far:

    I've encrypted the source code to prevent you copying it.

    yea, that's gonna take a lot of work to copy it. you might need a binary editor to copy that. or maybe something more powerful, something - something with find/replace. certainly it would be much easier to just do the entire program by yourself. find/replace is a complex tool which should be used with the utmost care.
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  6. #6
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    Code:
    certainly it would be much easier 
    to just do the entire program by yourself.
     find/replace is a complex tool which should 
    be used with the utmost care.

    very true

    Still it's sufficiently confusing to try and blag it of as his own though.

  7. #7
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    Beware EOF! I think it works here, though.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  8. #8
    Registered User
    Join Date
    Sep 2001
    Location
    Fiji
    Posts
    212
    a quick thing i noticed, don't declare a variable inside the loop. You are better off declaring the line before the loop. You can reset the variable value inside the loop but not declare it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with number counting program
    By Turtal in forum C Programming
    Replies: 11
    Last Post: 04-25-2009, 02:40 PM
  2. why the control goes to else part in this program
    By vapanchamukhi in forum C Programming
    Replies: 2
    Last Post: 01-13-2009, 07:09 AM
  3. Replies: 2
    Last Post: 12-25-2003, 01:31 AM
  4. !!!Urgent Help on a group project!!!!!
    By AmeenR in forum C Programming
    Replies: 3
    Last Post: 12-13-2003, 09:22 PM
  5. Problem with letter and word counting
    By wordup in forum C Programming
    Replies: 3
    Last Post: 10-09-2002, 04:02 PM