main executes up until you make a menu selection (line 54), then seems to get stuck in an infinite loop. I'm not sure if the issue is in my switch statement (57-88) or in the code of the methods it calls.
//Sarah Wilderman
//COP 2222
//Week 2 - Chapter 10
//
//
//"Vowels and Consonants"
/*
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 the 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 the number.
Demonstrate these two functions in a program as defined in the text on page 608.
*/
//Inclusions
#include <iostream>
#include <iomanip>
#include <cstring>
usingnamespace std;
//Prototype vowel function
int findVowels(char *);
int findConsonants(char *);
//Drumroll, please, here comes the main function!
int main()
{
//Declare a constant variable, STRINGLENGTH, that will allow a maximum of 80 characters plus the null terminator.
constint STRINGLENGTH = 81;
//Declare a variable to store the entered string and restrict it's length to 80 characters plus the null terminator.
char inputString[STRINGLENGTH];
//Declare storage variables for vowels and consonants.
int numVow, numCons;
char option;
//Prompt user for input and store it.
cout << "Please enter a word or sentence. (Max 80 characters) \n";
cin.getline(inputString, STRINGLENGTH);
do
{
//Display the menu
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 both the vowels and consonants in the string\n";
cout << "D) Enter another string\n";
cout << "E) Exit the program\n";
//Prompt the user for a menu selection
cout << "\nPlease enter your selection: \n";
//Store selection in a variable.
option = cin.get();
//Open a switch statement
switch (option)
{
case'A':
case'a':
numVow = findVowels(inputString);
cout << "There are " << numVow << " vowels!";
break;
case'B':
case'b':
numCons = findConsonants(inputString);
cout << "There are " << numCons << "consonants!";
break;
case'C':
case'c':
numVow = findVowels(inputString);
cout << "There are " << numVow << " vowels!";
numCons = findConsonants(inputString);
cout << "There are " << numCons << "consonants!";
break;
case'D' :
case'd' :
cout << "Enter a new String, please";
cin.getline(inputString, 81);
break;
case'E':
case'e':
break;
default:
cout << "Please enter a valid option.";
continue;
}
} while(option != 'E' || option != 'e');
system("PAUSE");
return 0;
}
//Defining function findVowels
int findVowels(char *userString)
{
//Declare and initialize a storage variable to zero.
int vowels = 0;
//Step through the character string passed into the function
while (*userString != '\0')
{
//If the character is any of those listed (vowels), add them to the value of the vowels variable
if (*userString == 'a' || *userString == 'A' || *userString == 'e' || *userString == 'E' || *userString == 'i' || *userString == 'I' ||
*userString == 'o' || *userString == 'O' || *userString == 'u' || *userString == 'U')
{
vowels++;
}
}
//Return the value counted to point-of-call.
return vowels;
}
//Defining function findConsonants
int findConsonants(char *userString)
{
//Declare and initialize a storage variable to zero
int consonants = 0;
//Step through the C-String provided
while (*userString != '\0')
{
//I stole this from the previous method - if the character is NOT any of those listed, add it to the value of the consonants variable.
if (*userString != 'a' || *userString != 'A' || *userString != 'e' || *userString != 'E' || *userString != 'i' || *userString != 'I' ||
*userString != 'o' || *userString != 'O' || *userString != 'u' || *userString != 'U')
{
consonants++;
}
}
//Return the value counted to point-of-call
cout << consonants;
return consonants;
}
In findVowels(...) and findConsonants(...) the while loop goes forever or not at all because userString isn't changed and hence has always the same value.
There's no such thing as "closing a thread". The thread will remain open for posting until a certain length of time has passed, at which point it gets archived.
But if you want to indicate that your problem is solved, the convention is to change the thread icon to the green tick.