I have been trying to solve the cash problem but i think i'm running into problems with scoping between prototype, main and function updating the cents variable. I have tried a variety of variations and mostly wind up with errors. This is the version that compiles:
#include <cs50.h>
#include <stdio.h>
int get_change(void);
int calculate_quarters(int cents);
int calculate_dimes(int cents);
int calculate_nickles(int cents);
int calculate_pennies(int cents);
int main(void)
{
int cents = get_change();
int quarters = calculate_quarters(cents);
int dimes = calculate_dimes(cents);
int nickles = calculate_nickles(cents);
int pennies = calculate_pennies(cents);
int coins = quarters + dimes + nickles + pennies;
printf("Coins: %i \n", coins);
}
int get_change()
{
int change = 0;
do
{
change = get_int("Change owed: ");
}
while (change < 0);
printf("Change input: %i \n", change);
return change;
}
int calculate_quarters(cents)
{
int quarters = 0;
printf("Change before quarters: %i \n", cents);
while (cents >= 25)
{
quarters++;
cents -= 25;
}
printf("Quarters: %i \n", quarters);
printf("Change Remaining after quarters: %i \n", cents);
return quarters;
}
int calculate_dimes(cents)
{
int dimes = 0;
printf("Change before dimes: %i \n", cents);
while (cents >= 10)
{
dimes++;
cents -= 10;
}
printf("Dimes: %i \n", dimes);
printf("Change Remaining after dimes: %i \n", cents);
return dimes;
}
int calculate_nickles(cents)
{
int nickles = 0;
printf("Change before nickles: %i \n", cents);
while (cents >= 5)
{
nickles++;
cents -= 5;
}
printf("Nickles: %i \n", nickles);
printf("Change Remaining after nickles: %i \n", cents);
return nickles;
}
int calculate_pennies(cents)
{
int pennies = 0;
printf("Change before pennies: %i \n", cents);
while (cents >= 1)
{
pennies++;
cents -= 1;
}
printf("Pennies: %i \n", pennies);
printf("Change Remaining after pennies: %i \n", cents);
return pennies;
}
Output:
Change Owed: 55
Change input: 55
Change before Quarters: 55
Quarters: 2
Change remaining after quarter: 5
Change before dimes: 55
Dimes: 5
Change remaining after dimes: 5
Change before nickles: 55
nickles: 11
Change remaining after nickles: 0
Change before pennies: 55
Pennies: 55
Change remaining after pennies: 0
Coins 73