0

I am unsure why it is not giving me the output that I want because when I look at this to me it should work. If I posted too much code then tell me and I will take it down but I tried to limit it so it wouldn't go against the academic honesty. I have the code that prompts the user for input which I have saved in variable change. If you would like to see all of my code then let me know. I am new to coding but this pset1 has taken WAY too long for me to complete I struggled with mario too. So I don't know if it's just the way I am going about attacking the problem or what but any help would be greatly appreciated! Thanks in advanced.

EDIT so I have edited some of my code and I am displaying all of my code BUT it's not working still. I think that the reason has something to do with when I round? Or maybe the fact that I am using Do While loops? The problem is when I input .01 it gives out 4 and if I do 4.2 it gives out 7.

#include <stdio.h>
#include <cs50.h>
#include <math.h>

int main(void)
{
    float change;
do
{
    printf("How much change is owed?\n");
    change = GetFloat();
}
while(change <= 0);

change = roundf(change * .20);

float Q = .25;
int count = 0;
do
{
    change -= Q;
    count++;
}
while(change >= Q);
float D = .10;
do
{
    change -= D;
    count++;
}
while(change >= D);
float N = .5;
do
{
    change -= N;
    count++;
}
while(change >= N);
float P = .01;
do
{
    change -= P;
    count++;   
}
while(change >= P);
printf("%d\n", count);
}
2
  • What does it do when you test it? Assuming that you get a changee value from the user and just didn't post it, the first thing that I see is "float P = .1" should be "float P = .01".
    – Melde
    Sep 24, 2014 at 1:58
  • when I check it it just says that it compiles and it runs and then errors saying that the outputs are all wrong for everything. and then it says handles an input of foo correctly -1 correct and "".
    – Corith
    Sep 24, 2014 at 16:20

2 Answers 2

2

You multiplied the change by 100, converting them to pennies so if i have 0.10 changes left that will mean 10 pennies, but then you used Q = 0.25, D = 0.1 and so on, so they are not converted to pennies too.

I think that is not correct, don't you agree?

1
  • ohhhhh okay yeah I think I kinda understand what you are saying! that would explain a lot of my wrong outputs
    – Corith
    Sep 24, 2014 at 16:23
0

After doing round off and making pennies into dollar(or simmilar) you should divide them with the same quantity ... not by something like .5 or .25

hope you got what i want to point at you.

1
  • So after I do change = roundf(change * 100) where in my code should I divide? in each loop? So like I would do change -= Q and then divide?
    – Corith
    Sep 25, 2014 at 23:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .