I seem to be having difficulty with my code, I have tried many different ways but I can't seem to figure out how to implement some things. I'm also not sure how to setup my menu so only certain functions are called.
1. Adding a if statement under ShowMenu to check to see if the string word is lowercase or uppercase. Also Is there a better method that allows spacing different string method?
2. Count Uppercase and Lowercase
3. Converting to Uppercase and lower case
I just can't seem to find out how to check to see my string is upper or lowercase. If i try to enter a space and another word it doesn't work then. If anyone can recommend a different string method I would appreciate it
#include <iostream> //cout and cin
#include <string> //for string use
#include <conio.h> //for getch
#include <cctype> // for library reference
usingnamespace std;
//Function Prototype
void CountVowels(string, int&);
void ShowMenu(string&, int);
int main()
{
//Variables
string word;
int choice = 0;
int vowels;
//Call Functions
ShowMenu(word, choice);
CountVowels(word,vowels);
_getch();
}
//***************************************************
// The function definition ShowMenu.
// Parameter word holds the word entered by user
// Parameter choice holds the users choice for menu
// A menu is displayed and asks the user for a response
//***************************************************
void ShowMenu(string& word, int choice)
{
cout <<"\t\tMENU\t\t" << endl
<<"======================================" << endl;
cout <<"1. Count Vowels" << endl
<<"2. Count uppercase" << endl
<<"3. Convert to uppercase" << endl
<<"4. Count lowercase" << endl
<<"5. Extract Word" << endl
<<"6. Quit" << endl;
//Get menu choice and get word from user.
cout << endl;
cout <<"Enter choice: ";
cin >> choice;
cout << endl;
cout << "Enter a word: ";
cin >> word;
}
//------------------------------------------------------------
// The function definition ShowMenu
// Counts the number of vowels in a word
// parameter word - a string of characters
// parameter vowelCount - number of vowels found in word
//------------------------------------------------------------
void CountVowels(string word, int& vowelCount)
{
vowelCount = 0;
int numLetters = word.length(); //get the number of letters in a word
for (int i = 0; i < numLetters; i++)
{
char letter = word[i]; //get the ith letter in the word
if (letter == 'a' || letter == 'e' ||
letter == 'i' || letter == 'o' ||
letter == 'u' || letter == 'y' )
{
vowelCount++;
}
}
cout << "There are:" << vowelCount << endl;
}
checking to see if its uppercase or lowercase: using <cctypes> you can use islower and isupper. im confused by the second part of one. do you mean split it up by spaces? i think string::tokenize does that. if not you can write one with <sstream>. not too hard. as to 2... for(char C : ur_string) if(isupper(C)) uppercount++ and just do tests like that for lower. use toupper and tolower for converting between upper and lower