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;
}
}