C Isalpha需要一个标识符

C Isalpha需要一个标识符,c,C,这是我的密码: #include <stdio.h> #include <ctype.h> main(){ float input; printf("Input: "); scanf("%f", &input); if (isalpha(input) || (input) < 0)){ printf("Input is an alphabet or is lesser than 0"); } else

这是我的密码:

#include <stdio.h>
#include <ctype.h>

main(){
    float input;
    printf("Input: ");
    scanf("%f", &input);
    if (isalpha(input) || (input) < 0)){
        printf("Input is an alphabet or is lesser than 0");
    } else {
        printf("Input is correct. %f is a number larger than 0", input);
    }
}
#包括
#包括
main(){
浮点输入;
printf(“输入:”);
scanf(“%f”,&input);
if(isalpha(输入)| |(输入)<0)){
printf(“输入为字母或小于0”);
}否则{
printf(“输入正确。%f是大于0的数字”,输入);
}
}
我想让代码检测
input
是大于0的数字还是字母表。但是,我遇到了以下错误:

8:错误:应为标识符

这对我的代码执行意味着什么?我应该如何成功地运行代码

  • 括号的打开/关闭不正确

  • 也许您的ide/编译器正在处理它,但它应该是
    int main()

  • isalpha()
    将使用浮点值异常运行。尽量避免这样


  • 首先,您缺少声明main的
    int

    int main()
    
    此外,您的括号过多

    if (isalpha(input) || (input) < 0)){
    
    if(isalpha(输入)| |(输入)<0)){
    

    Scanf使用%f读取浮点数。您的程序将接受任何ascii字符,我想这不是您的意图。

    如果,请更正
    中的括号:

    if ( isalpha(input) || (input < 0) )
    

    我仍然不确定您需要什么,但您可以尝试这样做作为起点。它不会处理所有可能的输入,并且会错误地将输入(如
    #42
    )分类为字母表或小于0,这是值得怀疑的,但您可以在这一点上进行迭代,并希望获得更完善的版本

    #include <stdio.h>
    #include <ctype.h>
    
    int main(){
        float input;
        printf("Input: ");
        if (scanf("%f", &input) && input >= 0){
            printf("Input is correct. %f is a number larger than 0", input);
        } else {
            printf("Input is an alphabet or is lesser than 0");
        }
    }
    
    提示用户:

    printf("Input: ");
    
    此条件由两部分组成:第一部分是
    scanf
    ,它将尝试读取
    input
    ,如果成功,将计算为1,即
    true
    ,因此将计算第二部分
    input>=0
    ,如果
    input
    确实是>=0,则打印第一条消息

    if (scanf("%f", &input) && input >= 0){
        printf("Input is correct. %f is a number larger than 0", input);
    
    否则我们打印第二条消息

    } else {
        printf("Input is an alphabet or is lesser than 0");
    }
    

    此处匹配括号数:
    (isalpha(输入)| |(输入)<0))
    输入的类型为
    浮点型。为什么要检查字母表?需要检查
    scanf()的返回值
    输入是否成功。是否希望用户能够输入字母,例如
    a
    ?您不能使用
    %f
    格式。您能给我们一个打印第一条消息和打印第二条消息的输入示例吗?我的输入也有相同数量的括号
    (isalpha(input)| |(input)<0))
    确切地说,您的括号太多了。如果(isalpha(input)| |(input)<0),则应为
    if{
    但我们认为,除了代码不可编译之外,代码还有更基本的问题。这将无法处理用户输入的内容不能被
    浮点值所接受,而是以字符开头的情况,例如“3x”@EricPostpischil:我不确定OP是否在寻找这种输入验证。不过这将是一个很好的补充。你可能想发布一个答案,包括其他角落的案例。这将非常有帮助。
    
    if (scanf("%f", &input) && input >= 0){
        printf("Input is correct. %f is a number larger than 0", input);
    
    } else {
        printf("Input is an alphabet or is lesser than 0");
    }