My problem: I am now able to get the program to compile, however, when i call the functions to count the number of consonants or vowels, the numbers are not what I expect.

Instructions: Write a function that accepts a pointer to a C-String as its argument. The function should count the number of vowels appearing in the string and return that number. Write another function that accepts a pointer to a C-string as its argument. This function should count the number of consonants appearing in the string and return that number.

Demonstrate these 2 functions in a program that performs the following steps:

1. The user is asked to enter a string.
2. The program displays the following menu:

A ) Count the number of vowels in the string.
B ) Count the number of consonants in the string
C ) Count the number of vowels and consonants in the string
D ) Enter another string
E ) Exit the program

3. The program should perform one of the two functions when A or B is selection.

Here is my code so far, I have tried to walk through it line by line to figure out why the function is not counting correctly, or should I say, what I have done to make the function count incorrectly. Here is my code so far:

//******************************************************************************
// Program:     Count.cpp                                                      *
// Programmer:  Paul J. Williams                                               *
// Date:        January 29, 2011                                               *
// Purpose:     This program will ask the user to enter a string and it will   *
//              count the number of vowels and constanants in that string.     *
//******************************************************************************

#include <iostream>
using namespace std;

// Function Prototype
int countVowels(char * str);
int countCons(char * str);

int main()
{
    const int SIZE = 81;          // Max size of the array
    //const int V_SIZE = 6;         // Size of the vowel array
    char userString[SIZE];
    //char vowels[V_SIZE] = {'a', 'e', 'i', 'o', 'u'};
    char choice;                  // To hold the menu choice
    char *strPtr = userString;    // Declare and initialize the pointer
    
    // Get the string from the user
    cout << "Please enter a string. (Maximum of 80 characters) :\n\n";
    cin.getline(userString, SIZE);
    
    // Display the menu and get a choice
    cout << "\n\nA. Count the number of vowels in the string \n";
    cout << "B. Count the number of consonants in the string \n";
    cout << "C. Count both the number of vowels and consonants in the string \n";
    cout << "D. Enter another string \n";
    cout << "E. Exit the program \n\n";
    cout << "Please make your selection: ";
    cin >> choice;
    
    // Menu selections
    if (tolower(choice) == 'a')
    {
         countVowels(userString);
         cout << "This string contains " << countVowels(userString);
         cout << " vowels. \n";        
    }
    else if (tolower(choice) == 'b')
    {
         countCons(userString);
         cout << "This string contains " << countCons(userString);
         cout << " consonants. \n";
    }
    else if (tolower(choice) == 'c')
    {
         cout << "This is not an actual requirement of the assignment.\n";
    }
    else if (tolower(choice) == 'd')
    {
         cout << "This is not an actual requirement of the assignment.\n";
    }
    else
    {
         cout << "This is not an actual requirement of the assignment.\n";
    }
        
    system("PAUSE");
    return 0;
}

//******************************************************************************
// Definition of countVowels. The parameter strPtr is a pointer that points to *
// a string.  The parameter char array [] is an array of vowels that the       *
// function searches for in the string.  The function returns the number of    *
// times the characters in the array appear in the string.                     *
//******************************************************************************

// Local variables
int countVowels(char* str)
{
    int times = 0;            // To hold times vowels appear
    const int size = 6;
    char vowels[size] = {'a', 'e', 'i', 'o', 'u', '\0'};
    char *vowelsPtr;

// Step through the string and count the occurrence of vowels
for(vowelsPtr = vowels; *vowelsPtr != '\0'; vowelsPtr++)
{
        while(*str != '\0')
         {
             if(tolower(*str) == *vowelsPtr)
             times++;
             str++;
         }
     return times;
}
}
//******************************************************************************
// Definition of countCons. The parameter strPtr is a pointer that points to ``*
// a string.  The parameter char array [] is an array of consonants that the   *
// function searches for in the string.  The function returns the number of    *
// times the characters in the array appear in the string.                     *
//******************************************************************************

// Local variables
int countCons(char *str)
{
    int times = 0;            // To hold times vowels appear
    const int size = 22;
    char consonants[size] = {'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm',
                             'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y',
                             'z', '\0'};
    char *consPtr;                           
    

// Step through the string and count the occurrence of vowels
for(consPtr = consonants; *consPtr != '\0'; consPtr++)
{
    while(*str != '\0')
            {
              if(tolower(*str) == *consPtr)
                 times++;
                 str++;
            }
     return times;
}
}

For each letter in the source string you have to check each character in the vowels or conconants string to see if the letter in the source string matches one of those.

// count vowels
times = 0;
for(; *str != '\0'; ++str)
{
   for(vowelsptr = vowels; *vowelsptr != '\0; vowelsptr++)
   {
      if( tolower(*str) == *vowelsptr )
      {
          ++times;
          brak;
      }
    }
}

Another, even simpler way to do it is to use strchr(), if you are allowed to use that function

times = 0;
for(; *str != '\0'; ++str)
{
   if( strchr(vowels,*str) != NULL)
       ++times;
}

I assume you can use arrays since you are already using pointers.

Are you also aware that each character value is just a number? 'A'=65, 'B'=66, 'a'=97...

You can set up an array of 256 set to zero and use each character as an index to increment the values. This will count each individual character -- letters, digits, spaces, everything.

Then once you've accumulated all the counts, simply add the ones you need: totVowels = count['a'] + count['e'] + ...

There are a few problems with the code you have posted.
1) In your main, where you call the functions, you are actually calling them twice. The first time on a line by itself, and then again in the cout statement (so, lines 41 and 47 could be commented out.)
2) In your count functions, the return statement is inside the for loop. As written, you are probably getting the count of the vowel 'a' and the count of the consonant 'b.' You should move your return from inside the for loop to just before the end of the routine (swapping lines 92 and 93 in one function and then lines 122 and 123 in the other.)

The other comments are good for enhancing the efficiency of the process.

Thank you guys, each one of you for your help. My code is now working as intended. I could not have done it without your help. Again, thanks!!!

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.