C++ 这个程序有一个错误。有人能认出它吗?谢谢
大家好,这个程序似乎有一个错误,因为它显示了错误的输出。它不是显示辅音和元音的数量,而是打印字符串的长度C++ 这个程序有一个错误。有人能认出它吗?谢谢,c++,C++,大家好,这个程序似乎有一个错误,因为它显示了错误的输出。它不是显示辅音和元音的数量,而是打印字符串的长度 #包括 使用命名空间std;//显示字符串的元音和辅音的程序 int count元音(字符a[] { 整数计数=0; 对于(int i=0;a[i]!='\0';i++) { 如果((a[i]>=65&&a[i]=97&&a[i]=65&&a[i]=97&&a[i]您的代码有两个问题: 检查是否相等。若要检查两个值是否相等,应使用双等于==,而不是单个等于。单个等号表示赋值,而不是相等检查
#包括
使用命名空间std;//显示字符串的元音和辅音的程序
int count元音(字符a[]
{
整数计数=0;
对于(int i=0;a[i]!='\0';i++)
{
如果((a[i]>=65&&a[i]=97&&a[i]=65&&a[i]=97&&a[i]您的代码有两个问题:
检查是否相等。若要检查两个值是否相等,应使用双等于==
,而不是单个等于。单个等号表示赋值,而不是相等检查
在count|u辅音的逻辑条件下。a!=1 | | a!=2
将始终计算为true
“修复我的代码”问题在SO上明显偏离主题。因此,使用所有警告和调试信息编译您的代码(g++-Wall-Wextra-g
with…),对其进行改进以不获取警告,并使用调试器gdb
(可能还有其他工具,例如valgrind
)在一个if语句中,您使用“代码> >=/COD>而不是<代码>=<代码>,对于一个THNBTW,您应该避免硬编码ASCII字母编码。今天,写“<代码>‘A’/CODE>,不是97。也可以使用一些。考虑使用STD::String,不是char数组。@ BaselStaynkvigy外观调试确实有帮助,但它没有提供W的原因。为什么它显示了一个错误,这就是我在这里发布的原因。我不明白你为什么这么说。瞧,你投了反对票,你把我的问题搁置了,但没有给出相关的解决方案。你不认为你的答案是“离题”吗?学会对寻求帮助的人做出正确的回应。还要尽量礼貌。这很重要。
#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;
}