C++ isspace和isalpha不是';t计数

C++ isspace和isalpha不是';t计数,c++,C++,我的程序应该计算输入字符串中元音、辅音、数字和空格的数量。这只是计算数字的数目。请帮忙 #include<iostream> using namespace std; void counter(char string[], int count[]){ int check_letter, check_digit, check_space; for(int i = 0; i < 99; i++){ check_letter = isalpha(st

我的程序应该计算输入字符串中元音、辅音、数字和空格的数量。这只是计算数字的数目。请帮忙

#include<iostream>
using namespace std;

void counter(char string[], int count[]){
    int check_letter, check_digit, check_space;
    for(int i = 0; i < 99; i++){
        check_letter = isalpha(string[i]);
        if(check_letter == 1){
            string[i] = tolower(string[i]);
            if(string[i] == 'a' || string[i] == 'e' || string[i] == 'i' ||
                    string[i] == 'o' || string[i] == 'u'){
                count[0] = count[0] + 1;
            } else{
                count[1] = count[1] + 1;
            }
        }
        check_digit = isdigit(string[i]);
        if (check_digit == 1){
            count[2] = count[2] + 1;
        }
        check_space = isspace(string[i]);
        if(check_space == 1){
            count[3] = count[3] + 1;
        }
    }
}

main(){
    char string[100] = {};
    int count[4] = {};
    cout << "Please enter a string: ";
    cin.get(string, 100);
    cin.get();
    cout << string;
    counter(string, count);
    cout << "There are " << count[0] << " vowels.\nThere are " << count[1] <<
            " consonants.\nThere are " << count[2] << " digits.\nThere are " <<
            count[3] << " spaces.";
}
#包括
使用名称空间std;
无效计数器(字符字符串[],整数计数[]){
整数校验字母、校验数字、校验空格;
对于(int i=0;i<99;i++){
检查字母=isalpha(字符串[i]);
如果(检查字母==1){
string[i]=tolower(string[i]);
如果(字符串[i]='a'| |字符串[i]='e'| |字符串[i]='i'||
字符串[i]=“o”|字符串[i]=“u”){
计数[0]=计数[0]+1;
}否则{
计数[1]=计数[1]+1;
}
}
检查数字=isdigit(字符串[i]);
如果(检查数字==1){
计数[2]=计数[2]+1;
}
检查_space=isspace(字符串[i]);
if(检查_空格==1){
计数[3]=计数[3]+1;
}
}
}
main(){
字符字符串[100]={};
整数计数[4]={};

cout您的问题在于假设
isalpha
isdigit
等。当它们匹配时返回
1
:他们的文档称它们返回的“真”值为非零值,“假”值为零

例如,来自:

返回值

如果字符是字母字符,则为非零值,否则为零


如果将结果存储在
bool
中,或直接在布尔上下文中测试它们(例如
If(…)
条件,然后将为您完成转换。

您的问题是假设
isalpha
isdigit
等。匹配时返回
1
:他们的文档说他们返回的“真”值为非零值,“假”值为零

例如,来自:

返回值

如果字符是字母字符,则为非零值,否则为零


如果您将结果存储在
bool
中,或者直接在布尔上下文中测试它们(例如
If(…)
条件,那么转换将为您完成。

问题是
检查字母
检查数字
检查空格
都应该是
bool
,而不是
int

因此,更改
bool check\u letter、check\u digital、check\u space;
以及
if(check\u letter)
而不是
if(check\u letter==1)
等等


另外,请记住,“字符串”不是一种非常聪明的变量命名方式…

问题是
检查字母
检查数字
检查空格
都应该是
bool
而不是
int

因此,更改
bool check\u letter、check\u digital、check\u space;
以及
if(check\u letter)
而不是
if(check\u letter==1)
等等


另外,请记住,“string”不是一种非常聪明的变量命名方式…

我修改了您的程序,它对我来说运行良好。您可以直接在if()语句中检查isalpha()isdigit()的返回值,就像它们是布尔值一样

#include<iostream>
using namespace std;

void counter(char string[], int count[]){
    for(int i = 0; i < 99; i++){
        if(isalpha(string[i])){
            string[i] = tolower(string[i]);
            if(string[i] == 'a' || string[i] == 'e' || string[i] == 'i' ||
                    string[i] == 'o' || string[i] == 'u'){
                count[0] = count[0] + 1;
            } else{
                count[1] = count[1] + 1;
            }
        }
        if (isdigit(string[i])){
            count[2] = count[2] + 1;
        }
        if(isspace(string[i])){
            count[3] = count[3] + 1;
        }
    }
}

main(){
    char string[100] = {};
    int count[4] = {};
    cout << "Please enter a string: ";
    cin.get(string, 100);
    cin.get();
    cout << string;
    counter(string, count);
    cout << "There are " << count[0] << " vowels.\nThere are " << count[1] <<
            " consonants.\nThere are " << count[2] << " digits.\nThere are " <<
            count[3] << " spaces.";
}
#包括
使用名称空间std;
无效计数器(字符字符串[],整数计数[]){
对于(int i=0;i<99;i++){
if(isalpha(字符串[i])){
string[i]=tolower(string[i]);
如果(字符串[i]='a'| |字符串[i]='e'| |字符串[i]='i'||
字符串[i]=“o”|字符串[i]=“u”){
计数[0]=计数[0]+1;
}否则{
计数[1]=计数[1]+1;
}
}
if(isdigit(字符串[i])){
计数[2]=计数[2]+1;
}
if(isspace(string[i])){
计数[3]=计数[3]+1;
}
}
}
main(){
字符字符串[100]={};
整数计数[4]={};

我已经修改了你的程序,它对我来说运行良好。你可以直接在if()语句中检查isalpha()isdigit()的返回值,就像它们是布尔值一样

#include<iostream>
using namespace std;

void counter(char string[], int count[]){
    for(int i = 0; i < 99; i++){
        if(isalpha(string[i])){
            string[i] = tolower(string[i]);
            if(string[i] == 'a' || string[i] == 'e' || string[i] == 'i' ||
                    string[i] == 'o' || string[i] == 'u'){
                count[0] = count[0] + 1;
            } else{
                count[1] = count[1] + 1;
            }
        }
        if (isdigit(string[i])){
            count[2] = count[2] + 1;
        }
        if(isspace(string[i])){
            count[3] = count[3] + 1;
        }
    }
}

main(){
    char string[100] = {};
    int count[4] = {};
    cout << "Please enter a string: ";
    cin.get(string, 100);
    cin.get();
    cout << string;
    counter(string, count);
    cout << "There are " << count[0] << " vowels.\nThere are " << count[1] <<
            " consonants.\nThere are " << count[2] << " digits.\nThere are " <<
            count[3] << " spaces.";
}
#包括
使用名称空间std;
无效计数器(字符字符串[],整数计数[]){
对于(int i=0;i<99;i++){
if(isalpha(字符串[i])){
string[i]=tolower(string[i]);
如果(字符串[i]='a'| |字符串[i]='e'| |字符串[i]='i'||
字符串[i]=“o”|字符串[i]=“u”){
计数[0]=计数[0]+1;
}否则{
计数[1]=计数[1]+1;
}
}
if(isdigit(字符串[i])){
计数[2]=计数[2]+1;
}
if(isspace(string[i])){
计数[3]=计数[3]+1;
}
}
}
main(){
字符字符串[100]={};
整数计数[4]={};

是的,你可以,但是
int check\u letter,check\u digital,check\u space;
没有用……是的,你可以,但是
int check\u letter,check\u digital,check\u space;
没有用。。。