大家好,我一直负责编写一个程序来计算句子中'a'字符的数量。我可以使用的最复杂的代码是for循环和switch语句。这是迄今为止的代码。如果我把cout放在do中,那么它会说123等,但是cout甚至不会在do-while循环之后显示。我使用ascii值表来确定字母a的值。我在输出时遇到了问题,只能欣赏一些反馈。计算字符的麻烦
int main()
{
char lettertofind;
int letteramt=0;
cout<<"Enter a sentence\n";
cin>>lettertofind;
do
{
cin>>lettertofind;
if(lettertofind == 65||97){
letteramt++;
}
}while(lettertofind != '\n');
cout<<"There are"<<letteramt<<" a's in that sentence"<<endl;
return 0;
}
使用'std :: count_if'。 – chris
我不允许使用:/ –