Thread: My first assignment with a class, please point me in the right direction

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

    My first assignment with a class, please point me in the right direction

    Hello all,

    This is my very first assignment using classes, and I am having a wee bit of a problem with it. I do not want you to correct the code but if if you could point me in the right direction I would appreciate the help.

    The assignment involves "writing a C++ class to calculate the frequency of letters, number of vowels, number of uppercase letters and number of lower case letters in a word. Prompt the user to enter a word up to 10 characters and display the frequency of each of the letters, number of vowels, number of uppercase letters and number of lower case letters in that word."

    By the way I am using Bloodshed DevC++ v.4.9.9.2

    Basically I want to call a simple function that will not return any values but will simply print the frequency of letters to the screen (everything else prior to that works fine by the way).

    Here is my code thus far;

    Code:
    #include <iostream>
    #include <cctype>    // Not sure if this is really needed here 
    #include <string>
    
    using namespace std;
    
    class Word           // Declaring the class object
       {
        public:
        
               char WordArray[25];
               
               // Public member function
               
               void FrequencyOfLetters();
               
               //---------------------------------------------------
               // Function to calculate number of uppercase letters
               //---------------------------------------------------
               
        unsigned int NumberOfUppercase()
        {
                 int i,UppercaseTotal;
                 int size = strlen(WordArray);
                 
                 for(int i=0; i<size; i++)
                 { if (isupper(WordArray[i]))
                            UppercaseTotal++;
                 }
                  return UppercaseTotal;
        } 
        //----------------------------------------------------
        // Function to calculate number of lowercase letters
        //----------------------------------------------------
        
        unsigned int NumberOfLowercase()
        {
                 int i,LowercaseTotal;
                 int size = strlen(WordArray);
                 
                 for(int i=0; i<size; i++)
                 { if (islower(WordArray[i]))
                            LowercaseTotal++;
                 }
                  return LowercaseTotal;
        } 
        //-------------------------------------------
        // Function to calculate number of wowels 
        //--------------------------------------------
        
        unsigned int NumberOfVowels()
        {
                 int i,VowelTotal;
                 int size = strlen(WordArray);
                 
                 for(int i=0; i<size; i++)
                 { if ((WordArray[i]=='a') || (WordArray[i]=='A') || (WordArray[i]=='e') 
                       || (WordArray[i]=='E') || (WordArray[i]=='I') || (WordArray[i]=='i')
                       || (WordArray[i]=='o') || (WordArray[i]=='O') || (WordArray[i]=='u')
                       || (WordArray[i]=='U') || (WordArray[i]=='y') || (WordArray[i]=='Y'))
                            VowelTotal++;
                 }
                  return VowelTotal;
        } 
     //--------------------------------------------------------------   
     // Function to calculate an print the frequency of letters 
     // Parameter: Input Word
     // Action prints an array listing the frequency of letters
     //--------------------------------------------------------------
        
      void FrequencyOfLetters()
        {
        int size = strlen(WordArray);
        char alphabet[50]="abcdefghijklmnopqrstuvwxyz";
        int letter_count[26];
        for(int i=0; i<26; i++)
        {
            letter_count[i]=0;
        }
        for(int i=0; i<size; i++)
        {
           for(int j=0; j<=26; j++)
            {
                if(WordArray[i]==alphabet[j])
                {
                    letter_count[j]++;
                }
            }
        }            
        for(int j=0; j<26; j++)
            {
              if (letter_count[j]>0)
              cout<<alphabet[j]<<" "<<letter_count[j]++<<endl;
            }
      }        
           
    };                   
    //----------------------------                              
    // begin main processing
    //----------------------------
    
    int main()
    {
        Word Input;
        
        char line[25];
        
        cout<<"Enter a string 25 Char. long Max : ";
        cin >> line;
        strcpy(Input.WordArray,line);
        
        cout<< "The word you entered " << line << " has " << endl; 
        cout<< Input.NumberOfUppercase() << " Uppercase Letters" << endl;
        cout<< Input.NumberOfLowercase() << " Lowercase Letters" << endl;
        cout<< Input.NumberOfVowels() << " Vowels" << endl;
        
        Input.FrequencyOfLetters();
            
      // Pause to look at the results
      
         system("pause");
         return 0;
              
    } // end main
    Now at the moment I am stuck because the program will not compile. It is giving this lone error;

    Code:
    82 C:\C++Course\A1-1.cpp `void Word::FrequencyOfLetters()' and `void Word::FrequencyOfLetters()' cannot be overloaded
    Can you point me to what it is I am doing wrong? There is a piece of this puzzle that does not fit I know it, but I cannot find it. There is something I don't quite comprehend yet about using public member functions while passing a charater array to it.

    Thanks in advance,

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Interesting. Typically, we would either define a member function in the class declaration, or leave it as a prototype, e.g.,
    Code:
    class ClassWithDefinedMemberFunction
    {
    public:
        void foo()
        {
            // ...
        }
    };
    
    // or
    
    class ClassWithMemberFunctionPrototype
    {
    public:
        void foo();
    };
    
    void ClassWithMemberFunctionPrototype::foo()
    {
        // ...
    }
    In this case, it looks like you are doing both, so the compiler is interpreting as an attempt to overload the same member function with the same signature.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    class Word           // Declaring the class object
    {
    public:
        
        char WordArray[25];
               
        // Public member function
               
        void FrequencyOfLetters();
    
        ...
    Try deleting that bit in red.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    May 2004
    Posts
    10
    That did the trick !

    Thanks a lot to both of you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class assignment
    By tmnismo91 in forum C Programming
    Replies: 18
    Last Post: 06-19-2007, 07:00 PM
  2. 6 measly errors
    By beene in forum Game Programming
    Replies: 11
    Last Post: 11-14-2006, 11:06 AM
  3. Point me in the right direction
    By vampje in forum C++ Programming
    Replies: 0
    Last Post: 06-07-2006, 03:52 AM
  4. Need help to build network class
    By weeb0 in forum C++ Programming
    Replies: 0
    Last Post: 02-01-2006, 11:33 AM
  5. problem with Class Point.
    By utoots in forum C++ Programming
    Replies: 2
    Last Post: 03-28-2003, 03:10 PM