您如何计算菜单中的辅音和元音数量
How do you count the number of consonants and vowels in a menu
我在程序上遇到麻烦,它如何计数元音和辅音
有问题#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
语句,您可以使用string
和std::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;
}
}
相关文章:
- 为什么"do while"循环不断退出,即使条件计算结果为 false?
- 递归函数计算序列中的平方和(并输出过程)
- (C++)分析树以计算返回错误值的简单算术表达式
- 我的字符计数代码计算错误.为什么
- 在计算中使用二的幂有多有利可图
- 如何计算文件中的"columns"数?
- 计算排序向量的向量中唯一值的计数
- 函数何时会在c++中包含stack_Unwind_Resume调用
- 如何使用 std::累积在 C++ 中计算总和立方体
- 无法获取菜单选择以运行函数.C++
- 使用Qt C++计算类似Git的SHA1哈希
- OpenCV C++.快速计算混淆矩阵
- cpp二进制搜索问题,计算给定数组中输入元素的出现次数
- C++如何计算用户输入的数字中的偶数位数
- 如何计算数据类型的范围,例如int
- 类似枚举的计算常量
- 计算每个节点的树高,帮助我解释这个代码解决方案
- 多个If语句与使用逻辑运算符计算条件的单个语句的比较
- Python中的for循环与C++有何不同
- 您如何计算菜单中的辅音和元音数量