0
//I don't understand why answer shows incorrect

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

int main(void)

{
   
 {

    double n;

  int c=0;

do

{

    n=get_double("Cash Owed:");

}

    while 

     (n<=0);

        {

            n=round(n*100);
        }

        do

        {

           n=n-25;

           c++;

        }

        while        

            (n>=25);    

do

{    

   (n=n-10);

    c++;

}

while

    (n>=10);

         do

         {

             (n=n-5);

        c++;

}

         while

             (n>=5);

do    

{

    (n=n-1);

    c++;

}

    while 

        (n>=1);

        do 

        { printf("You need %i coins\n", c);

    }

        while 

            (n==0);

    }}
1

There are a few issues here.

First, it uses a floating point ( double n ) to store the amount of money. Then the code converts the dollar amount to cents (correctly) but then stores it back in the same floating point variable, thus defeating the purpose of eliminating the storage errors made by floating point numbers.

Next, the code uses do/while loops. This means that each loop will run at least once, no matter what. Say that the amount was $0.01. Since each loop runs at least once, it'll count 1 quarter, 1 dime, 1 nickel and 1 penny for one cent. It should be using while loops, not do/while loops.

Finally, I haven't looked at this year's pset, but are you outputting exactly what is asked for, or are you printing extra text, which would be checked as wrong, even if the numbers are right?

Finally, please get familiar with style50 to properly format the code. There's far too much whitespace and too many lines. Get rid of all the extra blank lines - there are too many. Next, the }, the 'while' and the while clause should all be on one single line together, not spread across 3 to 5 lines. Make it readable! Use style50 until you're experienced enough that it doesn't complain.

1
  • thank you so much on such great advice, and you're right i should out only the number it will be marked incorrect, I'll do the same – Chirag Khosla Jan 6 '20 at 23:29
0

Instead of double try getting the input from the user as float.

2
  • This wouldn't change anything. Both double and float are floating point types, and both are correct in this case. They need to preserve any fractional part of the amount entered - the change or fractional part of a dollar. Using double merely increases the range of numbers that can be entered. The only complication is this - if a sufficiently large number were entered, the code might fail when moving the amount to an int for being too large. – Cliff B Jul 22 '20 at 5:24
  • Yes both are floating point notations, you are correct, I myself didn't know about this when I too was doing this PSET – Shahroz Ali Jul 22 '20 at 5:26

You must log in to answer this question.

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