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

It looks like you're doing the switch / case right, or at least if it isn't, it doesn't have anything to do with int versus char as far as I can see, but you're not doing line 47 right:

while (choice < 1 || choice > 5)

Note that in line 24 you've declared choice to be type char, so if the user types in '1', choice is going to hold 49, or the ASCII value of '1'. Stick single quotes around '1' and '5' in line 47.

Line 52:

if (choice != 'e' || choice != 'E')

This is always going to evaluate to true due to the != and || signs.

Lines 57, 61: You don't need these brackets. case blocks aren't like if blocks where you need to denote where the block to execute starts and stops with brackets. Ditto for your other case statements. I can't remember exactly how break statements work, so deleting the brackets may or may not affect anything, but the brackets are not necessary.

Line 87 - Like I mentioned above, choice holds the ASCII value so when the user enters '5', choice won't contain 5, but instead will contain 53 (ASCII for '5'), so it looks like an infinite loop in line 87.


What exactly is this program doing and what exactly is it SUPPOSED to do?

OK well I got the choice thing figured out, it was like you said, brackets and the char. I am having problems with the do while loop and the choice to exit the program.

The assignment is to create functions that count the number of consonants and vowels in a string while passing by a pointer. That much I got working fine it is this menu that is really giving me a hard time. I need to validate the input

function to show menu

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

then to verify input

while (choice < '1' || choice > '5')
		{
			cout << "Please enter A, B, C, D or E: ";
			cin >> choice;
		}

I am sure this is not right but cannot figure another way...

after this statement I need to put in the switch statement, but it should only run if the user dose not enter e so I added an if

if (choice != 'e' || choice != 'E')

the end of the program is the end of the do while for the menu driven program

} while (choice != 'e' || choice != 'E');

I guess my question is how do I verify that a user has selected a char instead of a number? I have the whole program working except for the verification of menu choices and the loop working properly.

thanks again for your help
Jay

here is the program again for completeness

#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':
			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':
			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':
			case 'c': //get number of letters
					letters = getLetters(strPtr, letters);
					cout << "The number of letters in the string is: " << letters << endl;
					break;				

			case 'D':
			case 'd': //enter a new string
					cout << "Enter a string of letters no more than " << (SIZE - 1) << " charicters long: ";
					cin.ignore(SIZE, '\n');
					cin.getline(string, SIZE);
					break;
			
			case 'E':
			case 'e': break;		

			}
		}
	} while (choice != 'e' || choice != 'E');
	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;
}

OK well I got the choice thing figured out, it was like you said, brackets and the char. I am having problems with the do while loop and the choice to exit the program.

The assignment is to create functions that count the number of consonants and vowels in a string while passing by a pointer. That much I got working fine it is this menu that is really giving me a hard time. I need to validate the input

function to show menu

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

then to verify input

while (choice < '1' || choice > '5')
		{
			cout << "Please enter A, B, C, D or E: ";
			cin >> choice;
		}

I am sure this is not right but cannot figure another way...

after this statement I need to put in the switch statement, but it should only run if the user dose not enter e so I added an if

if (choice != 'e' || choice != 'E')

the end of the program is the end of the do while for the menu driven program

} while (choice != 'e' || choice != 'E');

I guess my question is how do I verify that a user has selected a char instead of a number? I have the whole program working except for the verification of menu choices and the loop working properly.

thanks again for your help
Jay

here is the program again for completeness

#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':
			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':
			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':
			case 'c': //get number of letters
					letters = getLetters(strPtr, letters);
					cout << "The number of letters in the string is: " << letters << endl;
					break;				

			case 'D':
			case 'd': //enter a new string
					cout << "Enter a string of letters no more than " << (SIZE - 1) << " charicters long: ";
					cin.ignore(SIZE, '\n');
					cin.getline(string, SIZE);
					break;
			
			case 'E':
			case 'e': break;		

			}
		}
	} while (choice != 'e' || choice != 'E');
	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;
}

Like I mentioned on the last post, the condition in line 52 always evaluates to true:

if (choice != 'e' || choice != 'E')

You probably want && instead of || .

Lines 47 - 51. If you want to check that the user enters 'A' through 'E', don't compare the input to '1' through '5'. With a char, you can actually do this:

while (choice < 'A' || choice > 'E')

Take advantage of the toupper function from the cctype libarary. For error messages, isdigit and isalpha functions can come in handy. toupper will change any lower case letters to upper-case so there are fewer things to compare. I would design your code as follows:

do
{
    cin >> choice;
    choice = toupper (choice);
    switch (choice)
    {
         case 'A':  // code
         case 'B': // code
         case 'C': // code
         case 'D': // code
         case 'E': // code
         default:  // code for displaying error messages
    }
}
while (condition);

wow, I cant believe it was that easy. I guess I need reading lessons, just did what you said in the first post and everything works great.

also the toupper function saved me a lot of headache.

Thanks again for your help!
Jay

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.