这个程序有错误。请有人认出来吗?感谢

时间:2017-12-03 08:33:28

标签: c++

嗨,所有这个程序似乎都有错误,因为它显示错误的输出。它不是显示辅音和元音的数量,而是打印字符串的长度。

#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. 我,我= 73,105
  4. O,o = 79,111
  5. U,u = 85,117

1 个答案:

答案 0 :(得分:2)

您的代码有两个问题:

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