sorry for the long title I'm currently doing cs50's problem set 1's Greedy and I've run into an issue. It seems that every cent works except for one's that need to use .01 cent. I've changed my code to display what the command gets when I give it 0.01 cents. Here's the code:
#include <cs50.h>
#include <math.h>
#include <stdio.h>
int main (void)
{
int changeamount = 0;
printf("Amount to be converted to change: ");
float change = GetFloat();
float roundf(float change);
for (changeamount = changeamount - 1 + 1; change >= .25; changeamount++)
{
change = change - .25;
}
for (changeamount = changeamount - 1 + 1; change >= .10; changeamount++)
{
change = change - .10;
}
for (changeamount = changeamount - 1 + 1; change >= .05; changeamount++)
{
change = change - .05;
}
for (changeamount = changeamount - 1 + 1; change >= .01; changeamount++)
{
change = change - .01;
}
change = change + changeamount;
printf("%.50f\n", change);
}
It only doesn't work whenever I input an amount of money which would take the 1 cent to show (like: 1.01 or 0.41). Here's what it tells me when I input 0.01:
0.00999999977648258209228515625000000000000000000000
I've tried rounding it using:
float round(float change)
I've also tried converting it to an int and then rounding it:
int changeint = change * 100 / 100
Both of which didn't end up helping. So any tips/hints at what's wrong with the code would be great! (I don't want to get a direct answer as that could be considered cheating)
EDIT: Here's the updated code:
#include <cs50.h>
#include <math.h>
#include <stdio.h>
int main (void)
{
int changetwentyfive = 0;
int changeten = 0;
int changefive = 0;
int changeone = 0;
int changeamount = 0;
printf("Amount to be converted to change: ");
float changefloat = GetFloat();
if (changefloat < 0)
{
do
{
printf("Amount to be converted to change: ");
changefloat = GetFloat();
}
while(changefloat < 0);
}
int change = changefloat * 100;
for (changeamount = changeamount - 1 + 1; change >= 25; changeamount++)
{
change = change - 25;
changetwentyfive++;
}
for (changeamount = changeamount - 1 + 1; change >= 10; changeamount++)
{
change = change - 10;
changeten++;
}
for (changeamount = changeamount - 1 + 1; change >= 5; changeamount++)
{
change = change - 5;
changefive++;
}
for (changeamount = changeamount - 1 + 1; change >= 1; changeamount++)
{
change = change - 1;
changeone++;
}
printf("%d\n", changeamount);
printf("25: %d, 10: %d, 5: %d, 1: %d", changetwentyfive, changeten, changefive, changeone);
}
Hope you can help me.