0

I have just run check50 and well the main part of my code is not working and I'm thinking it's because I'm not using the round function correctly. Please help.

Here's my code (ignore the constant printing of change owed and coins, I was trying to find out where I was going wrong!:

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

    int main(void)
  {
   // Prompt user for amount owed and store this value
   float dollars;
   do 
  {
   printf ("Hey, how much change is owed?\n");   
   dollars = GetFloat();
  }
  while (dollars <= 0);

// If acceptable value, convert from dollars to cents (x100) and round  the value
float convert = dollars * 100;
float change = round(convert);
int cents = change;
int quarter = 25;
int dime = 10;
int nickel = 5;
int penny = 1;
int coins = 0;
int owed = 0;

// countdown from change owed to zero and count number of coins used
if (cents >= quarter)
{
   owed = cents - quarter;
   coins++;
   printf("%d\n", owed);
   printf("%d\n", coins);
}
   else if (cents >= dime)
{
    owed = cents - dime;
    coins++;
    printf("%d\n", owed);
    printf("%d\n", coins);
}
  else if (cents >= nickel)
{
    owed = cents - nickel;
    coins++;
    printf("%d\n", owed);
    printf("%d\n", coins);
}
  else if(cents >= penny)
{
    owed = cents - penny;
    coins++;
    printf("%d\n", owed);
    printf("%d\n", coins);
}
// print number of coins used to screen
    printf("%i\n", coins);
return 0;
 }

And here are the results of check50:

~/workspace/pset1/ $ check50 2015.fall.pset1.greedy greedy.c
:) greedy.c exists
:) greedy.c compiles
:( input of 0.41 yields output of 4
  \ expected output, but not "16\n1\n1\n"
:( input of 0.01 yields output of 1
  \ expected output, but not "0\n1\n1\n"
:( input of 0.15 yields output of 2
  \ expected output, but not "5\n1\n1\n"
:( input of 1.6 yields output of 7
  \ expected output, but not "135\n1\n1\n"
:( input of 23 yields output of 92
  \ expected output, but not "2275\n1\n1\n"
:( input of 4.2 yields output of 18
  \ expected output, but not "395\n1\n1\n"
:) rejects a negative input like -.1
:) rejects a non-numeric input of "foo"
:) rejects a non-numeric input of ""

Thank you in advance

-1

First, any extra printf statement will cause check50 to fail no matter what, so they need to be commented out to run check50.

The code has serious logic issues. It contains a string of if/else if constructs. That means that only one of these if statements will execute. Any value of 25 cents or more will execute the first if statement. Between 10 and 24 will execute the else if for dimes, and so on. But to be clear, they are all mutually exclusive. The code block for only one type of coin will execute. All the others will not.

On top of that, each if or else if code block will execute only once, so it will never count beyond 1 for each type of coin.

Combine these two issues, the final coin count will always be exactly 1 coin.

You might want to look at how you might use while loops in this pset. On the good side, you got the conversion from float to int part right. That's what most people get wrong. Happy coding!

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

1
  • Thank you so much, have completed this now and I see what you mean about the serious logic issues! I was tearing my hair out, so thanks so much for your insight – Sofia Dec 8 '16 at 15:33
0

You need to print out the output as a single number that is total no of changes used. You need to increment the 'coins' variable in each loop if needed and in the end,print the total incremented value. Hope it helps.

2
  • thanks for your reply, so does the coin++ not increment in the if statements? would I need to do something like coins_used = coin + 1 thereby counting and storing the value within another variable? – Sofia Dec 7 '16 at 19:08
  • and does my use of the round() function make sense, thanks very much – Sofia Dec 7 '16 at 19:09

You must log in to answer this question.

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