Holle,我按照我的C ++ PrimerPlus 6键入了一个c ++程序,当我运行它时,结果对我来说是不可思议的,你能告诉我它有什么问题吗?
#include <iostream>
#include <cctype>
int main()
{
using namespace std;
cout << "Enter your message and type @ to an end." << endl;
char ch;
int digit;
int alpha;
int lower;
int supper;
int punct;
cin.get(ch);
while ( ch != '@' )
{
if ( isdigit(ch) )
digit++;
else if ( isalpha(ch) )
alpha++;
else if ( islower(ch) )
lower++;
else if ( isupper(ch) )
supper++;
else
punct++;
cin.get(ch);
}
cout << "Digits: " << digit << endl << "Alpha: " << alpha
<< endl << "Lower: " << lower << endl << "Supper: "
<< supper << endl << "Puncts: " << punct << endl;
return 0;
}
它显示了那些:
答案 0 :(得分:2)
无论字符ch
是低位还是高位,它都会被计为字母,并会计入alpha
if ( isdigit(ch) )
digit++;
else if ( isalpha(ch) )
alpha++; // All alphabet will count to here
else if ( islower(ch) )
lower++; // this won't run
else if ( isupper(ch) )
supper++; // this won't run
else
punct++;
cin.get(ch);
由于您没有初始化lower
和supper
,因此它们的值是随机的,因此您会看到一些不可预测的结果