2014-01-27 24 views
-3

輔音和元音的數量,有什麼問題在如何計算元音和輔音你怎麼算的我有我的程序麻煩菜單

#include<iostream> 
using namespace std; 

int main(){ 
    int num[10],even = 0,odd = 0; 
    char choice; 
    int vowelcount = 0; 
    int concount = 0; 
    string word; 

    cout<<"MENU:"<<endl<<"[N]umber"<<endl<<"[L]etter"<<endl<<"Choice : "; 
    cin>>choice; 

    switch(choice){ 
    case 'n': case 'N': 
     cout << "Enter 10 integers: \n"; 
     for(int i = 0; i < 10; i++) { 
      cin >> num[i]; 
      if((num[i] % 2) == 0) { 
       even++; 
      } 
     } 
     odd = 10 - even; 
     cout << "Even: " << even << endl; 
     cout << "Odd: " << odd << endl; 
     system("pause"); 
     cout<<"Do you want to repeat the program ? Y/N "; 
     break; 

    case 'l': case 'L': 
     cout<< "Enter 10 Letters : \n"; 
     cin>> word; 
     for (int i=0; word [i] != '\0'; i++){ 
      word[i] = tolower (word[i]); 
      for (int i=0; word [i] != '\0'; i++) 
       switch(choice){ 
       case 'A' : 
       case 'E' : 
       case 'I' : 
       case 'O' : 
       case 'U' : 
        vowelcount++; 
        break; 
       default: 
        concount++; 
       } 
     } 
     cout<<" total vowels = " <<vowelcount << endl; 
     cout<<" total consonant = " <<concount << endl; 
     system("pause"); 
     return 0; 
    } 
} 
+0

是否有錯誤?或者你只是得到錯誤的數字? – user1950929

+0

讓你的工作更容易,看看這裏:http://en.cppreference.com/w/cpp/algorithm/count – olevegard

+0

@olevegard這顯然是一個練習,使用內置函數不會幫助他學習基礎技術。 – Barmar

回答

0

而不是使用一個switch語句,你可以這裏

std::string vowels = "AEIOUaeiou"; 
std::string consonants = "BCDFGHJKLMNPQRSTVWXYZbcdfghjklmnpqrstvwxyz"; 

if (vowels.find(letter) != std::string::npos) 
{ 
    ++vowelcount; 
} 
else 
{ 
    if (consonants.find(letter) != std::string::npos) 
    { 
     ++consonantcount; 
    } 
} 
1

好了,以下幾個問題:使用stringstd::string::find方法。首先,總是嘗試提供更多信息,然後「出現問題」。我只是簡單地將你的例子複製到visual studio中,並且很快就能夠找出你的問題,但是更多的信息我可能不需要這樣做。另外,整個問題不需要大寫。 :)

因此......您的switch語句正在對稱爲choice的變量上完成。該變量是您用來選擇菜單選項的變量。你需要在你正在測試的角色上運行你的switch語句。此外,你有兩個循環,你只需要一個。

現在,因爲你正在選擇的程序上運行,並且你有兩個循環,每次循環選擇總是'l'或'L'是輔音,但它的運行次數相等到輸入字符串的長度的平方。所以你對元音數量的回答是0,因爲它從來沒有看到任何字符,而且你的輸入字符串的長度是平方的,因爲你有嵌套循環,並且它多次計算'L'。