First, when you call a function, you don't include its return type and/or its parameter(s) datatype(s). For example, the prototype for round()
is
double round(double x);
This means that round()
receives a double
and returns a double
. When we call it though, we call it like that
round(x);
Where x
is a double
variable. Watch the short on functions for more information!
Second, you should multiply the amount you receive from the user by 100
first, then round it using roundf()
or round()
because these functions, as their names suggest, round floating-point values to the nearest integer. And if you round the input first, then multiply it by 100
it'll always give you a multiple of 100
. For example, if I entered 1.99
, do you think your program should
- round
1.99
first which gives 2
- then multiply
2
by 100
which gives 200
or
- multiply
1.99
by 100
first which gives 199.0
- then round
199.0
which gives 199
Lastly, you're dividing by quarters, dimes, nickles and pennies anyway regardless of q
being >, < or = the divisor which is surely wrong since this is integer division and if q
is < the divisor, the result will be 0
. Probably some conditions will be sufficient to solve this.
Also, no need to use 4 variables for each type of coins. You could have used just one variable adding the number of coins of each type to it like
int total = 0;
// quarters
total += amount / quarters;
amount %= quarters;
// dimes
total += amount / dimes;
amount %= dimes;
and so on.
EDIT: you should only output the total number of coins followed by a newline character.