您如何计算菜单中的辅音和元音数量

How do you count the number of consonants and vowels in a menu

本文关键字:菜单 何计算 计算      更新时间:2023-10-16

我在程序上遇到麻烦,它如何计数元音和辅音

有问题
#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] != ''; i++){
            word[i] = tolower (word[i]);
            for (int i=0; word [i] != ''; 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;
    }
}

好吧,这里有几个问题。首先,始终尝试提供更多信息,然后"有问题"。我只是将您的榜样复制到Visual Studio中,并且很快就可以弄清楚您的问题,但是有了更多信息,我可能不需要这样做。另外,整个问题都不需要大写。:)

so ...您的开关语句是在一个称为选择的变量上完成的。此变量是您用于选择菜单选项的变量。您需要在要测试的字符上运行开关语句。另外,您有两个循环,只需要一个。

现在,因为您正在选择该程序,并且有两个循环,因此每次通过每个循环选择总是" L"或" L",这是辅音输入字符串平方的长度。因此,您的响应是元音数量为0,因为它永远不会看到任何元音,并且输入字符串的长度平方是因为您有嵌套的循环,并且很多次都在计数" L"。

而不是使用switch语句,您可以使用stringstd::string::find方法:

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;
    }
}