我刚开始学习 C++,这是我为练习而编写的程序:
#include <iostream>
#include <string>
using namespace std;
int main ()
{
int uppercase=0, lowercase=0, digits=0, other=0, i=0;
int character;
char* string;
cout << "Enter a string!\n";
cin.getline(string, 20);
while(true)
{
character = int(*(string+i));
if (character==0)
{
break;
}
if (character > 64 && character < 91)
{
uppercase++;
}
if (character > 96 && character < 122)
{
lowercase++;
}
if (character > 47 && character <58)
{
digits++;
}
else
{
other++;
}
i++;
}
cout << "Upper case " << uppercase << "\n";
cout << "Lower case " << lowercase << "\n";
cout << "Digits " << digits << "\n";
cout << "Others " << other << "\n";
return 0;
}
程序在完成打印结果后崩溃。我在这里错过了一些非常明显的东西吗?
附带问题:变量“其他”总是增加,即使它不应该增加。我使用 else 语句是否错误?