0

I have created a program to count the syllables in a word inputted by the user. The user is to enter any amount of words followed by the enter key until the (#) key is entered, then the program is to display the words in a table followed by the syllable count for each word.
I am having issues with the "silent e" portion of my program.

        if (word_length - 1 == 'e')
        {
            vowels = vowels - 1;

It seems as though it is not able to pick up the last letter in the string of words. I have tried to move some of the if statements around to see if that helps and to better identify where the problem lies, and from what I have noticed, as stated earlier i believe it to be with the silent e portion of the code. It is difficult to find even the smallest errors in my code, so I am asking for another set of eyes to gaze over my code. Any help will be greatly appreciated. Also, I have yet to complete the formatting of my results table, so please over look that.

#include <iostream>
#include <string>
#include <iomanip>
#include <vector>

using namespace std;


int main()
{

    string word_1 = "create list";
    int vowels = 0;
    int word_length; // better to set .length() into variable to eliminate warnings
    int words_to_print = 0; // this will count how many words to print to use in for loop later

    /* 
    vector <variable type> name_of_vector[size of vector];

    creating vectors 
    leaving them empty for now
    */
    vector <string> words_saved;
    vector <int> number_of_syllables_saved;

    cout << "Enter 4 words from the English dictionary, to determine the amount of syllables each word has." << endl;
    cout << "Please enter [#] when finished, to create a list." << endl;
    cin >> word_1;

    while (word_1 != "#") // as long as user doesnt enter # you can enter a word and
    {                       // have it run thru the syllable logic
        word_length = word_1.length();
        words_to_print++;
        words_saved.push_back(word_1); 
        // ^ this saves the word into the next availabe index of vector for strings. 

        for (int i = 0; i < word_length ; i++) // length is a variable now instead of function syntax this
        {                                       // eliminates the <: signed/usnsigned mismatch warning below
            if ((word_1[i] == 'a') || (word_1[i] == 'e') || (word_1[i] == 'i') || (word_1[i] == 'o') || (word_1[i] == 'u') || (word_1[i] == 'y'))
            {
                vowels = vowels + 1;

                if ((word_1[i + 1] == 'a') || (word_1[i + 1] == 'e') || (word_1[i + 1] == 'i') || (word_1[i + 1] == 'o') || (word_1[i + 1] == 'u') || (word_1[i + 1] == 'y'))
                {
                    vowels = vowels - 1;

                    if (word_length - 1 == 'e')
                    {
                        vowels = vowels - 1;
                        if (vowels == 0)
                        {
                            vowels = vowels + 1;
                        }
                    }
                }
            }

        }
        number_of_syllables_saved.push_back(vowels);
        //^ this puts number of syllables into vector of ints
        vowels = 0; // this resets the amounts so it can count vowels of next word and not stack from previous word

        cin >> word_1; // this will reset the word and controls loop to print out chart if # is entered
    }


    // use a for loop to print out all the words

    cout << endl << endl << endl;
    cout << "Word: " << setw(30) << "Syllables: " << endl;

    for (int x = 0; x < words_to_print; x++)
    {
        cout << words_saved[x] << setw(20) << number_of_syllables_saved[x] << endl;
    }

    //system("pause");
    return 0;

}
  • 3
    word_length - 1 == 'e'. You compare a length and a char. Is it intended ? Do you rather means word_1[word_length-1] or something similar ? – Gojita Jan 24 '20 at 18:06
  • 1
    Accessing `word_1[i + 1]` when `i` is `word_length-1` is undefined. – molbdnilo Jan 24 '20 at 18:29
  • thank you for your response; no I was intending the latter, word_1[word_length - 1]. However, upon making that change, my program is still not picking up the silent e for most words. It does give the correct syllable count for "the", which I believe to be in relation to the 1st if statement, because there is only 1 vowel in the word. – user12776943 Jan 24 '20 at 18:39
  • @molbdnilo do you mind elaborating on what you mean by when "i is word_length - 1 it is undefined"? – user12776943 Jan 24 '20 at 18:45
  • @user12776943 It being undefined means that your program can do anything. If you're using C++11 or later, it's safe. – molbdnilo Jan 24 '20 at 20:14
  • Side notes: You shouldn't comment the obvious (`push_back() // this saves [...]`). Anybody with the least minimum of experience knows what `push_back` does. If that's not obvious, too, then rather write *why* you store the values. About [using namespace std](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). `setw()`: You might get more predictable results via e. g. `std::left << std::setw(30) << word << std::right << setw(3) << count`; unless `word` is too long, you get *always* indentation of 30 for the count. – Aconcagua Jan 27 '20 at 08:26

2 Answers2

1

You are comparing the character 'e' to the integer word_length - 1 instead of comparing it to the last character of your string as you intended.

You should replace if (word_length - 1 == 'e') with if (word_1[word_length - 1] == 'e').

  • however, when I made the appropriate changes, I am still not achieving the correct results if there is a silent e. – user12776943 Jan 24 '20 at 18:33
  • for example when I test "state, hare, kite"... ect. I am getting that they are 2 syllables and not 1. On the contrary, when I test a word like " the", I am getting the correct syllable count, 1. any suggestions? – user12776943 Jan 24 '20 at 18:36
1

Your nested if-statements don't get evaluated at the anticipated index. Say the input is "state".

At i = 0, word_1[i] = 's', doesn't pass the first if statement.

At i = 1, word_1[i] = 't', doesn't pass the first if statement.

At i = 2, word_1[i] = 'a', vowels becomes 1. The program does not proceed further down the nested if statements since word_1[i+1] is 't'.

At i = 3, word_1[i] = 't', doesn't pass the first if statement.

At i = 4, word_1[i] = 'e', vowels becomes 2. The program does not proceed further down the nested if statements since word_1[i+1] is garbage value.

As you can see, it never reaches the nested if-statements as you intended

MT756
  • 629
  • 3
  • 10
  • THANK YOU!! and I really appreciate the breakdown of your answer. Coming from a beginning programmer, your answer was easy to comprehend. Note to self (thanks to you) ** learn better debugging techniques and start with tracing your loops and iterations!** – user12776943 Jan 24 '20 at 19:51