scanf无法正确读取int

问题描述 投票:0回答:1

我也尝试添加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));
}
c scanf
1个回答
0
投票

scanf()函数的第一个参数错误。格式字符为int的“%d”。您输入“&d”。您可以调用系统(“暂停”)以保持终端打开并查看结果。

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

© www.soinside.com 2019 - 2024. All rights reserved.