0

After running check50 on my code for 'cash' in pset1 I'm getting the following errors:

:( input of 1.6 yields output of 7
    did not find "7\n"
:( input of 23 yields output of 92
    did not find "92\n"
:( input of 4.2 yields output of 18
    timed out while waiting for program to exit

However when I run my program and use the same inputs it prompts me, as it should. So I want to submit my code, but I have to assume there's an error somewhere - would be great if someone could point it out.

My code is:

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

int main(void)

{
    //Prompt user for an amount of change
    float n;
    do
    {
        n = get_float("How much change from a dollar?: \n");
    }
    while (n <= 0.00 || 1.00 <= n);

    //Convert input to cents and assign to integer
    int c = round(n * 100);

    //Assign int values to cash
    int quarter = 25;
    int dime = 10;
    int nickel = 5;
    int cent = 1;
    int count = 0;

    //Check if a quarter (or more) is valid change
    while (c >= quarter)
    {
        c = c - quarter;
        count = count+1;
    }
    //Check if a dime (or more) is valid change
    while (c >= dime)
    {
        c = c - dime;
        count = count+1;
    }
    //Check if a nickel (or more) is valid change
    while (c >= nickel)
    {
        c = c - nickel;
        count = count+1;
    }
    //Check if a cent (or more) is valid change
    while (c >= cent)
    {
        c = c - cent;
        count = count+1;
    }
    //Print the count of coins
    printf("Minimum number of coins for change is %d\n", count);

}

Thanks for any help!

Edit: don't know what's happened to the formatting... The includes are cs50, stdio and math

0

Why does the code reject valid inputs of $1 or more?

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

1
  • Man, that's so annoying. For some reason reading through the problem I assumed it was only change for a dollar that the program wanted. Sure enough, changed the code to: while (n <= 0.00); And the check runs smoothly. Thanks for the help.
    – Matt Domm
    May 8 '18 at 13:33
0
#include<cs50.h>
#include<math.h>
#include<stdio.h>
int main(void)
{
    float b;
do
{
    b=get_float("change owed:\n");
}
while(b<=0.00);
{ 
    int cents= round(b* 100);
{
int coins=0;
while(cents>=0.25)
{ coins++;
cents=cents-0.25;
}
while(cents>=0.10)
{ coins++;
cents=cents-0.10;
}
while(cents>=0.05)
{ coins++;
cents=cents-0.05;
}
while(cents>=0.01)
{ coins++;
cents=cents-0.01;
}
{
printf("%i\n",coins);
}
}
}
}

I incurred the same error although having bill/change owed(b) as b<=0.00. could anyone help me as to where im wrong.

0

I altered the code. since I already did *100 it was giving me errors as I checked my loop for 0.25/0.01etc as it should've been 25/10etc cents in accordance to the rounding up part I did prior to it.

You must log in to answer this question.

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