0

I worked very hard on Cash and finally produced compilable code and correct output. But when I ran check50 I got these results, shown here. Please help. My code is shown below, in case you need it.

#include<cs50.h>
#include<math.h>
#include<stdio.h>
int main(void)
{
    int n = 0;
    do
    {
        n = (get_float("Change owed: ")* 200);
    }
    while (n < 0);
    int q = ((n / 25) % 10);
    int d = ((q / 10) % 10);
    int r = ((d / 5) % 10);
    int c = (n % 10);
    int t = q + d + n + c;
    int j = t * 2;
    int i = j / 100;
    printf("%i\n", i);
}

Update:

I edited my logic like so, shown below, based on Cliff B's answer.

#include<cs50.h>
#include<math.h>
#include<stdio.h>
int main(void)
{
    int n = 0;
    do
    {
        n = (get_float("Change owed: ") * 200);
    }
    while (n < 0);
    int q = ((n / 25) % 10);
    int d = ((q / 10) % 5);
    int r = ((d / 5) % 5);
    int c = (n % 5);
    int t = q + d + n + c;
    int j = t * 2;
    int i = j / 100;
    printf("%i\n", i);
}

Here are my new check50 results.

#include<cs50.h>
#include<math.h>
#include<stdio.h>
int main(void)
{
    int n = 0;
    do
    {
        n = (get_float("Change owed: ") * 200);
    }
    while (n < 0);
    int q = ((n / 25) % 10);
    int d = ((q / 10) % 5);
    int r = ((d / 5) % 1);
    int c = (n % 5);
    int t = q + d + n + c;
    int j = t * 2;
    int i = j / 100;
    printf("%i\n", i);
}

Update: I changed my logic but it didn't help.

#include<cs50.h>
#include<math.h>
#include<stdio.h>
int main(void)
{
    int n = 0;
    do
    {
        n = (get_float("Change owed: ") * 200);
    }
    while (n < 0);
    int q = ((n / 25) % 10);
    int d = ((q / 10) % 5);
    int r = ((d / 5) % 1);
    int c = (n % 1);
    int t = q + d + n + c;
    int j = t * 2;
    int i = j / 100;
    printf("%i\n", i);
}

Update: Another futile tweak.

2

Time for some tough love. The logic in this code makes no sense. Take this for example:

int q = ((n / 25) % 10);

Say the amount given was $0.75. That makes n = 150. n/25 = 6. 6%10 = 6. But the actual number of quarters would be 3. I can't see how you got correct answers unless you just got lucky. I did run some tests and found some correct answers, but not because the logic was right.

Strangely, it does seem to be calculating quarters correctly. I'll have to study the code later to see why that works.

However, the do/while loop is correct. It could be done with a while loop too.

I suggest that you keep the do/while loop but start over on the calculation of the number of coins.

| improve this answer | |
2

Your problem might be more logical than with just code. The pset seems to ask more how many quarters, dimes, nickels, and pennies at a minimum can you get to the exact change needed. This requires you to at least do some subtraction with n. You want to constantly subtract n with the maximum amount of cash you can subtract (25, 10, 5, or 1) without going below 0.

In any other case though, you might also be doing a while loop wrong, it should look like this:

while (n < 0)
{
    //put code here that will run if n < 0
}

Good luck on this pset though!

| improve this answer | |

You must log in to answer this question.

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