I am working on an assignment for class and cannot for the life of me figure out how to use letters to choose a case for a switch statement.
Here is my code
#include <iostream>
#include <cctype>
#include <cstdlib>
using namespace std;
// prototypes
void showMenu();
int getVowels(char*, int*, int);
int getLetters(char*, int);
int getConsonants(char*, int*, int, int, int);
int main()
{
const int SIZE = 81;
char string[SIZE];
char *strPtr;
int vowels = 0;
int *ptrVowels;
int letters = 0;
int consonants = 0;
int *prtConsonants;
char choice;
cout << "Enter a string of letters no more than " << (SIZE - 1) << " charicters long: ";
cin.getline(string, SIZE);
do
{
strPtr = &string[0];
ptrVowels = &vowels;
prtConsonants = &consonants;
consonants = 0; // reset consonants for run again
vowels = 0; // reset vowels for run again
letters = 0; // reset number of letters
//display Menu
showMenu();
//Get users choice
cin >> choice;
// validate Menu choice
while (choice < 1 || choice > 5)
{
cout << "Please enter A, B, C, D or E: ";
cin >> choice;
}
if (choice != 'e' || choice != 'E')
{
switch (choice)
{
case 'a': //get number of vowels in string
{
vowels = getVowels(strPtr, ptrVowels, vowels);
cout << "The number of vowels in the string is: " << vowels << endl;
break;
}
case 'b': // Get number of consonants
{
consonants = getConsonants(strPtr, ptrVowels, consonants, letters, vowels);
cout << "The number of consonants in the string is: " << consonants << endl;
break;
}
case 'c': //get number of letters
{
letters = getLetters(strPtr, letters);
cout << "The number of letters in the string is: " << letters << endl;
break;
}
case 'd':
{
cout << "Enter a string of letters no more than " << (SIZE - 1) << " charicters long: ";
cin.ignore(SIZE, '\n');
cin.getline(string, SIZE);
break;
}
}
}
} while (choice != 5);
return 0;
}
void showMenu()
{
cout << "Please select a menu option\n";
cout << "A) Count the number of vowels in the string.\n";
cout << "B) Count the number of consonants in the string.\n";
cout << "C) Count the number of vowels and consonants in the string.\n";
cout << "D) Enter another string.\n";
cout << "E) Exit the program.\n";
}
int getVowels(char *strPtr, int *ptrVowels, int vowels)
{
int count = 0;
while (strPtr[count] != '\0')
{
if (strPtr[count] == 'a' || strPtr[count] == 'A')
*ptrVowels += 1;
else if (strPtr[count] == 'e' || strPtr[count] == 'E')
*ptrVowels += 1;
else if (strPtr[count] == 'i' || strPtr[count] == 'I')
*ptrVowels += 1;
else if (strPtr[count] == 'o' || strPtr[count] == 'O')
*ptrVowels += 1;
else if (strPtr[count] == 'u' || strPtr[count] == 'U')
*ptrVowels += 1;
count++;
}
vowels = *ptrVowels;
return vowels;
}
int getLetters(char *strPtr, int letters)
{
int count = 0;
while (strPtr[count] != '\0')
{
if (isalpha(strPtr[count]))
letters += 1;
count++;
}
return letters;
}
int getConsonants(char *strPtr, int *ptrVowels, int consonants, int letters, int vowels)
{
letters = getLetters(strPtr, letters);
vowels = getVowels(strPtr, ptrVowels, vowels);
consonants = letters - vowels;
return consonants;
}
I am able to get it working with numerical values, but when I try to add alpha like is in there I cannot get it to work. I am sure I need to add a aton somewhere or something along those lines I just cannot figure out where.
Thanks for your help in advance.
Jay