-4
大家好在这个程序中似乎有错误,它显示错误的输出。它不是显示辅音和元音的数量,而是显示字符串的长度。这个程序有错误。请任何人都可以识别它?谢谢
#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值。
- A,A = 65,97
- E,E = 69,101
- I,I = 73,105
- O,O = 79111
- U,U = 85117
*修复我的代码*问题显式**在SO外的主题**。因此,用所有的警告和调试信息编译你的代码('g ++ -Wall -Wextra -g' with [GCC](http://gcc.gnu.org/)...),改进它不会得到警告,* *使用调试器'gdb' **(也可能使用其他工具,例如'valgrind')。 –
您在if语句之一中使用'='而不是'==',对于一件事 – ThunderStruct
,您应该避免硬编码字母的ASCII编码。今天,[UTF-8无处不在](http://utf8everywhere.org/)。所以写''a'',而不是97.再看一些[C++参考](http://en.cppreference.com/w/)。考虑[isalpha](http://en.cppreference.com/w/cpp/string/byte/isalpha) –