0
votes

I've also tried adding fflush(stdout); after the printf statement and that didn't do anything, and neither did using fflush(stdin) before the scanf

#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
What exact incorrect behaviour are you observing? Give the exact input, expected behaviour and actual behaviour.kaylum
#include <conio.h> add thiskedar sedai
Note: return '0'; probably does not do what you think it does. ITYM return 0;.Jens
Renatus, the biggest mistake is not fully enabling warnings. Save time. Enable them all. scanf("&d", &q); should get a warning such as "warning: too many arguments for format [-Wformat-extra-args]"/chux - Reinstate Monica

3 Answers

2
votes

The first parameter of scanf() function is wrong. The format caractere is "%d" for int. You put "&d".

You can call a system("PAUSE") to keep the terminal open and see the result.

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

scanf() sample and reference: http://www.cplusplus.com/reference/cstdio/scanf/

1
votes

You have passed wrong format specifier for integer, change this scanf("&d", &q); to scanf("%d", &q);

1
votes

The parameter of scanf("%d",&q)
There should be % before datatype.
Datatype - d for int
&d to %d