简体   繁体   English

如何修复函数中的条件?

[英]How do I fix the conditions within my function?

When I run my code, the output only prints the else statement.当我运行我的代码时,输​​出只打印else语句。 How can I fix that?我该如何解决?

#include <stdio.h>

void printIt();
int num;

int main(){
    int num;
    printf("What is your annual income? ", &num);
    scanf("%d", &num);
    printIt();
}

void printIt() {                                      // If user makes over 90,000, machine will print congratulations.
                                                       // If user makes less than 90,000 machine will print keep going.
    if(num > 90000){
        printf("Congratulations, you are doing great!");
        return;
    }
    else{
        printf(" You will make $90,000, if you keep going.");
        return;
    }
}

You have two variables both called num .您有两个变量都称为num One is created in main and is local to main .一个是在main创建的,并且是main本地。 The other is global.另一个是全球性的。 Look for int num;寻找int num; in your code and you'll see that there are two of them.在您的代码中,您会看到其中有两个。

I would suggest you get rid of the global num and instead pass num to printIt .我建议你摆脱全局num ,而是将num传递给printIt The easier (but worse) solution is to get rid of the local num in main .更简单(但更糟糕)的解决方案是去掉main中的本地num

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM