我对 C++ 很陌生,我需要编写一个程序来计算数字、字符、空格符号和其他符号的数量。我决定从数位数开始。到目前为止,这是我的代码:
int main() {
int n = 0;
int numb = 0;
ifstream read("Data.txt");
n = symbCounter(n, read);
numb = numbCounter(n, read, numb);
cout << numb; // this is for quick testing
return 0;
}
int symbCounter(int &n, ifstream &read) {
char ch;
while (!read.eof()) {
read.get(ch);
n++;
}
return n;
}
int numbCounter(int &n, ifstream &read, int counter) {
char sk[n];
for (int i = 0; i < n; i++) {
read.get(sk[i]);
if (sk[i] == '1' || sk[i] == '2' || sk[i] == '3' || sk[i] == '4' || sk[i] == '5' || sk[i] == '6' || sk[i] == '7' || sk[i] == '8' || sk[i] == '9' || sk[i] == '0')
counter++;
};
return counter;
}
但是控制台给我的值是 0。我做错了什么?以及如何只计算字符,不包括数字或空格?提前致谢。