这个程序有错误。请任何人都可以识别它?谢谢

大家好在这个程序中似乎有错误,它显示错误的输出。它不是显示辅音和元音的数量,而是显示字符串的长度。这个程序有错误。请任何人都可以识别它?谢谢

#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值。

  1. A,A = 65,97
  2. E,E = 69,101
  3. I,I = 73,105
  4. O,O = 79111
  5. U,U = 85117

回答:

你有两个问题与您的代码:

  1. 在检查EQU先进而精湛。要检查两个值是否相等,您应该使用double等于==而不是一个等于。一个等号表示分配不平等检查
  2. 在count_consonants中的逻辑条件。 a != 1 || a != 2将始终评估为真

以上是 这个程序有错误。请任何人都可以识别它?谢谢 的全部内容, 来源链接: utcz.com/qa/260067.html

回到顶部