#include <cs50.h>
#include <stdio.h>
#include <math.h>
int biggestCoin (int num);
int main (void){
float userinput;
int changeOwed, coins;
do {
userinput = get_float("How much do I owe you? \n");
} while (userinput < 0);
changeOwed = round(userinput * 100);
coins = 0;
do {
biggestCoin(changeOwed);
changeOwed = changeOwed - biggestCoin;
coins++;
} while (changeOwed < 0);
printf("%i", coins);
}
//returns biggest possible coin to return to customer based on remaining change owed
int biggestCoin(int changeOwed) {
int penny = 1;
int nickle = 5;
int dime = 10;
int quater = 25;
if (changeOwed <= quater) {
return quater;
} else if (changeOwed <= dime) {
return dime;
} else if (changeOwed <= nickle) {
return nickle;
} else {
return penny;
}
}
Add a comment
|
1 Answer
In the line changeOwed = changeOwed - biggestCoin;
, you subtract a function from an integer. I assume you meant changeOwed = changeOwed - biggestCoin(changeOwed)
instead, subtracting the return value of the function call instead.
After fixing that compiler error, what happens in your code if someone enters 0
?
-
Hey that resolved that error, thanks!! If I enter a zero I get: runtime error: signed integer overflow: -2147483625 - 25 cannot be represented in type 'int'. Commented Apr 2, 2020 at 8:34
-
For 0, I would expect 0 coins. Maybe you should check whether your conditions make sense (and whether a do..while loop for the second part makes sense)– BlauelfCommented Apr 2, 2020 at 8:55