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. Couning Uppercase and Lowercase

  3. Converting from Uppercase to Lowercase and vice versa

I just can't seem to find out how to check to see my string is upper or lowercase. My string only accepts one word and no spaces. I can't figure out how to have the menu so each choice calls a certain function.

' #include <iostream> //cout and cin
#include <string>  //for string use
#include <conio.h> //for getch
#include <cctype> // for library reference
using namespace 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;
}

`

Recommended Answers

All 6 Replies

Should this have a reference to int?

void ShowMenu(string& word, int choice);

Something like.

void ShowMenu(string& word, int & choice)

For your purposes you're probably better off returning the value you want rather than passing an address to the function. Your code becomes much more readable and easier to understand. Here's a few simple improvements to your code:

#include <iostream> //cout and cin
#include <string> //for string use
#include <conio.h> //for getch
#include <cctype> // for library reference
using namespace std;
//Function Prototype
int CountVowels(string);
char ShowMenu();
int main()
{
    //Variables
    string word;
    char choice = '0';
    int vowels;
    //Call Functions
    while(true)
    {
        cout << "Enter a word: ";
        cin >> word;
        choice = ShowMenu();
        switch(choice)
        {
        case '1':
            vowels = CountVowels(word);
            cout << "There are:" << vowels << endl;
            break;
        case '2':
            break;
        case '3':
            break;
        case '4':
            break;
        case '5':
            break;
        default:
            return 0;
        }
    }
    _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
//***************************************************
char ShowMenu()
{
    char choice = '0';
    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;
    while(choice < '1' || choice > '6')
    {
        cout <<"Enter choice: ";
        cin >> choice;
        cout << endl;
    }
    return choice;
}
//------------------------------------------------------------
// 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
//------------------------------------------------------------
const string AllVowels = "aeiouy";
int CountVowels(string word)
{
    int 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
        for(int i = 0; i < AllVowels.length(); i++)
        {
            char c = AllVowels[i];
            if(letter == c)
            {
                vowelCount++;
                break;
            }
        }
    }
    return vowelCount;
}

I moved the display of the results out of your function. This enhances portability, since you can plug this function into any project and let the specific circumstances determine how the results are treated. Notice too the changes to the menu routine. With this format to add or remove an item, make the changes in the menu and only change the conditions for a valid input and the switch block. Also this makes it very easy to perfect each function before doing the next, since the code will compile and run without the other functions in there.

Here's a reference to the C++ toupper function.

Here's a reference to the tolower function.

To check for upper(65-90) or lower(97-122) use the ASCII code ranges that correspond.

Thanks for the recommendation!

But what I can't figure out is how do I make a toupper function when using a string? and same goes for lowercase

Also is there another method of using string, becaue I can only type in one word however I want to allow the program to accept two words and spaces

I just cant figure out how to make a string equal a variable. ex:

char letter = //string here

if (isupper(letter))
   cout << “Letter is uppercase.”;
else
   cout << “Letter is lowercase.”;

Loop through the string and use the toupper/tolower function on each character.

Okay, I looped the string but I can't figure out how to toupper each character of the string.

Thanks!

You get each character the same way you did it for counting the vowels use the index of the string, word[i]. Just pass that to toupper, toupper(word[i]).

To accept a string that takes more than one word the functions should still work. However keep in mind that if you're gonna use whole sentences that some functions might need to filter out the punctuation in order to work right.

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.