我也尝试添加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));
}
scanf()函数的第一个参数错误。格式字符为int的“%d”。您输入“&d”。您可以调用系统(“暂停”)以保持终端打开并查看结果。
...
scanf("%d", &q);
...
system("PAUSE");