大家好,我一直負責編寫一個程序來計算句子中'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
我不允許使用:/ –