[英]Counting syllables in a word C++
我創建了一個程序來計算用戶輸入的單詞中的音節。 用戶輸入任意數量的單詞,然后按回車鍵,直到輸入 (#) 鍵,然后程序將在表格中顯示單詞,然后顯示每個單詞的音節數。
我的程序的“silent e”部分有問題。
if (word_length - 1 == 'e')
{
vowels = vowels - 1;
似乎無法提取單詞串中的最后一個字母。 我試圖移動一些 if 語句,看看是否有幫助,並更好地確定問題所在,以及根據我所注意到的,如前所述,我相信它與代碼的無聲 e 部分有關。 在我的代碼中即使是最小的錯誤也很難找到,所以我要求另一雙眼睛注視我的代碼。 任何幫助將不勝感激。 另外,我還沒有完成我的結果表的格式化,所以請檢查一下。
#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;
}
您將字符 'e' 與整數 word_length - 1 進行比較,而不是按照您的意圖將其與字符串的最后一個字符進行比較。
您應該將if (word_length - 1 == 'e')
替換為if (word_1[word_length - 1] == 'e')
。
您嵌套的 if 語句不會在預期的索引處進行評估。 假設輸入是“狀態”。
在 i = 0 時,word_1[i] = 's',不會通過第一個 if 語句。
在 i = 1 時,word_1[i] = 't' 不會通過第一個 if 語句。
在 i = 2,word_1[i] = 'a' 時,元音變為 1。由於 word_1[i+1] 是 't',因此程序不會繼續向下嵌套 if 語句。
在 i = 3 時,word_1[i] = 't' 不會通過第一個 if 語句。
在 i = 4 時,word_1[i] = 'e',元音變為 2。由於 word_1[i+1] 是垃圾值,因此程序不會繼續向下嵌套 if 語句。
如您所見,它永遠不會像您預期的那樣到達嵌套的 if 語句
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.