嗨,所有这个程序似乎都有错误,因为它显示错误的输出。它不是显示辅音和元音的数量,而是打印字符串的长度。
#include<iostream>
using namespace std; // program to display the vowels and consonants of a string
int countvowels(char a[])
{
int count =0;
for(int i =0; a[i]!='\0';i++)
{
if((a[i]>=65 && a[i]<=90) || (a[i]>=97 && a[i]<=122)) // ignores digits,special characters
{
if((a[i]=65) || (a[i]=69) || (a[i]=73) || (a[i]=79) || (a[i]=85) || (a[i]=97) || (a[i]=101) || (a[i]=105) || (a[i]=111) || (a[i]=117) ) //ignores consonants
{
count++; //counts filtered out vowels
}
}
}
return count; // returns number of vowels which will be captured by x of main function
}
int countconsonants(char a[])
{
int count =0;
for(int i =0; a[i]!='\0';i++)
{
if((a[i]>=65 && a[i]<=90) || (a[i]>=97 && a[i]<=122)) // ignores digits,special characters
{
if((a[i]!=65) || (a[i]!=69) || (a[i]!=73) || (a[i]!=79) || (a[i]!=85) || (a[i]!=97) || (a[i]!=101) || (a[i]!=105) || (a[i]!=111) ||(a[i]!=117) ) //ignores vowels
{
count++; //counts filtered out consonants
}
}
}
return count; // returns number of consonants which will be captured by y of
main function
}
int main()
{
char a[100];
cout<<"Enter the string"<<endl;cin.get(a,100);
int x = countvowels(a);
int y = countconsonants(a);
cout<<"Number of vowels is"<<x<<endl; //nothing much to say about this part of the program. x just displays it and y does the same
cout<<"Number of consonants is"<<y<<endl;
return 0;
}
以下是元音的ASCII值。
答案 0 :(得分:2)
您的代码有两个问题:
==
而不是单个等于。单个等号表示赋值不等同检查a != 1 || a != 2
将始终评估为真