0

very new to this and trying to learn, any help might be appreciated. Got through all the errors but not getting the output i need.

#include <stdio.h>
#include <cs50.h>
#define QUARTER 25
#define DIME 10
#define NICKLE 5
#define PENNY 1

int main(void)
{
    float r = 0;
    float num;
    int c = 0;
    //Get Change, negative check
    num = get_float();
    if (num < 0.00)
    {
        printf("Retry: ");
        num = get_float();
    }
    else
    {
        r = (num * 100);
    }
    //Quarter minus change check
    do
    {
         r = (r - QUARTER);
         c++;
         printf("Remainder: %f Count: %i\n", r, c);
    }
    while (QUARTER <= r);
    //Dime minus change check
    if (r > 0.00)
    {
        do
        {
            r = (r - DIME);
            c++;
            printf("Remainder: %f Count: %i\n", r, c);
        }
        while (DIME <= r);
    }
    else printf("Remainder: %f Count: %i\n", r, c);
    // Nickle minus change check
    if (r > 0.00)
    {
        do
        {
            r = (r - NICKLE);
            c++;
            printf("Remainder: %f Count: %i\n", r, c);
        }
        while (NICKLE <= r);
    }
    else printf("Remainder: %f Count: %i\n", r, c);
    //Penny minus change check
    if (r > 0.00)
    {
        do
        {
            r = (r - PENNY);
            c++;
            printf("Remainder: %f Count: %i\n", r, c);
        }
        while (PENNY <= r);
    }
    else printf("Remainder: %f Count: %i\n", r, c);
}
1
  • Style note: when using #define for variables, the convention is to use all caps for your variable names. For example, "#define QUARTER 0.25" would be clearer than "#define q 0.25" and would probably improve your debugging by virtue of making the logic of your code more readable.
    – Peter
    Commented Jan 24, 2017 at 16:36

1 Answer 1

1

Your logic is flawed. In order to break out of a do...while loop, or any loop for that matter, there has to be something within the conditional statement that changes otherwise it will run forever. In your case, none of the variables q, t, f, o or n change at any time within your code.

If you have a value of change required and you want to see how many coins of a certain value are inside, you could do the following:

while the amount of change required is greater than the value of a coin
   subtract the value of the coin from change required
   add 1 to the coin count
repeat with other coin values until change required is 0

Also note the difference between a do...while loop and a while loop. A do...while loop will always run at least once, until it gets to the while statement at the end where it decides whether that condition is true or false. A while loop may run 0 times as the condition is evaluated before the loop is even run. Because of this, your code will add at least one coin of each value, whereas that may not actually be the desired outcome.

I would suggest that you work with one value of coin to start with, remove the other loops altogether and it will make it much easier to troubleshoot. Once it works for one type of coin, it is simple to repeat the loop for other values.

5
  • I tried doing that but can't seem to get it to loop unless entering "0" changed the code in original post to show the quarter only Commented Jan 24, 2017 at 19:51
  • Nevermind now i seems to have figured it out thanks to your advice! let me know if there's anything i can do to make it more elegant Commented Jan 24, 2017 at 21:48
  • This code will still have errors, as each loop will run without checking that there is enough remainder left. For example, if you enter 0.30, it will give you a remainder of -5! Remember what I said about the difference between do...while and while loops. You could also use this kind of loop for your user input as that currently only allows 1 retry before blindly accepting the value. Even then it will not assign anything to r on a retry. Commented Jan 24, 2017 at 22:52
  • so change the do...while and use while instead to ensure remainder is checked before it executes? Commented Jan 25, 2017 at 0:52
  • Exactly, you could also lose the if statements surrounding the loop, they are unneccesary. Commented Jan 25, 2017 at 7:51

You must log in to answer this question.

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