scanf无法正确读取int

时间:2020-04-11 11:03:54

标签: c scanf

我也尝试添加fflush(stdout);在printf语句之后,它什么也没做,在scanf之前也没有使用fflush(stdin)

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>


float cost(int qty)
{
    if (qty <= 20)
        return qty * 23.45;
    if (qty >= 21 && qty <= 100)
        return qty * 21.11;
    if (qty > 100)
        return qty * 18.75;
    return '0';
}

// void main() VS Only
int main()
{
    int q;

    printf("Enter quantity of books wanted: ");
    scanf("&d", &q);

    printf("\n\nThe total cost for purchasing books is: $%0.2f\n\n", cost(q));
}

3 个答案:

答案 0 :(得分:2)

scanf()函数的第一个参数错误。 格式字符为int的“%d”。 您输入“&d”。

您可以调用系统(“ PAUSE”)以保持终端打开并查看结果。

...
scanf("%d", &q);
...
system("PAUSE");

scanf()示例和参考: http://www.cplusplus.com/reference/cstdio/scanf/

答案 1 :(得分:1)

您传递了错误的整数格式说明符,请将此scanf("&d", &q);更改为scanf("%d", &q);

答案 2 :(得分:1)

scanf("%d",&q)的参数
数据类型前应该有%
数据类型-d的{​​{1}}
int&d

相关问题