c - 如何在 Visual Studio 中修复 "return value ignored: ' scanf'"代码 C6031

标签 c

我对编码 C(以及一般编码)是全新的,所以我一直在练习一些随机程序。这个应该根据用户的年龄和所需的“区域”数量(他们想要走多远)来确定过境车票的成本(Translink Vancouver 价格)。我已经成功编译了它,但由于某种我无法弄清楚的原因,scanf 函数被忽略了。我该如何解决?请记住,我只编码了几天。谢谢!

int main(void) {

int zones;
int age;
double price = 0.00;

printf("Welcome to TransLink cost calculator!\n\n");
printf("Please enter the desired number of zones (1, 2, or 3) you wish to travel: ");
scanf("%d", &zones);

if (zones < 1) {
    printf("Invalid entry\n");
    price = 0.00;
}

else if (zones > 3) {
    printf("Invalid entry\n");
    price = 0.00;
}

else if (zones == 1) {

    printf("Please enter your age: ");
    scanf("%d", &age);

    if (age < 0.00) {
        printf("Invalid Aage");
    }
    else if (age < 5) {
        price = 1.95;
    }
    else if (age >= 5) {
        price = 3.00;
    }
}

else if (zones == 2) {

    printf("Please enter your age: ");
    scanf("%d", &age);

    if (age < 0) {
        printf("Invalid Aage");
    }
    else if (age < 5) {
        price = 2.95;
    }
    else if (age >= 5) {
        price = 4.25;
    }
}

else if (zones == 3) {

    printf("Please enter your age: ");
    scanf("%d", &age);

    if (age < 0) {
        printf("Invalid Aage");
    }
    else if (age < 5) {
        price = 3.95;
    }
    else if (age >= 5) {
        price = 4.75;
    }
}

printf("The price of your ticket is: $%.2f + tax\n", price);

system("PAUSE");
return 0;
}

最佳答案

在这里发表评论有点太多了。

我使用了一个 Visual C 版本,但它从不提示 scanf 的返回值。没有被使用。它所做的就是提示scanf不安全且已弃用(如果不是)。

MS 认为我应该使用它自己的“更安全”版本 scanf_s这甚至更难使用,而且 IMO 根本不安全——因为它不是同类替代品,而是采用不同的参数,因此在使用它时很容易出错。

一个随之而来的问题是编译器在每次使用 scanf 时都会发出警告。 (和一些其他功能)掩盖了其他警告。我按照建议通过添加 #define 来处理它在第一个库头包含之前。

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>

MS警告的还有其他问题,我实际上放置了三个#defines在每个文件的开头:
#define _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_DEPRECATE  
#define _CRT_NONSTDC_NO_DEPRECATE

#include <stdio.h>

现在相关的警告很容易看到。

关于c - 如何在 Visual Studio 中修复 "return value ignored: ' scanf'"代码 C6031,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58004209/

相关文章:

c - 如何在c中缩放数字/数字范围

c - 结构函数返回不完整类型

c - (C) 传递给 func 的指针与原始指针不同

c - 二进制流上带有 WHENCE 的 SEEK_END

c - 如何将#define 常量从.h 文件导入Ada?

arrays - 如何从c中的字符串数组中删除重复的字符串

c++ - FFTW 性能变化

c - 必须在必须使用函数的地方声明函数原型(prototype)吗?

c - C 中的多参数 pthread_create() 函数?

c - 为什么 C 编译器将 long 指定为 32 位,将 long long 指定为 64 位?