0

The picture is what the terminator says when greedy runs

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

#define QUARTER 25;
#define DIME 10;
#define NICKEL 5;

int main(void)
{

    float given = 0;
    int cent = 0;
    int quarter = 0;
    int dime = 0;
    int nickel = 0;
    int leftover = 0;
    int count = 0;

    do
    {
        printf("O hai! how much change is owed?\n");
        given = GetFloat();
    }
    while( given <= 0 );

    cent = (int) round(given * 100);

    // quarters
    quarter = cent / quarter;
    leftover = cent % quarter;

    // dimes
    dime = leftover / dime;
    leftover = leftover % dime;

    // nickels
    nickel = leftover / nickel;
    leftover = leftover % nickel;

    count = quarter + dime + nickel + leftover;

    printf("%d\n", count);
    return 0;

}
2
  • In the future, please also inclue a description of the problem in the text, as well as a description of what you have done to localize the problem (which line you think is causing the problem) and what you have done to resolve the problem. Simply dropping code and "Please find my problem" is frowned upon in this forum. Unlike other forums, this is a teaching forum and the goal is to lead you to the solution, not to find it for you. This is not an admonishment, it is instruction to help you use this forum correctly in the future. – Cliff B Sep 16 '15 at 16:17
  • My apologies, it's my first time using this website <3 thank you so much. – Gamila Essam Sep 16 '15 at 19:35
2

You have a "divide by zero" exceptiion.

In your code, you've defined QUARTER as 25. You also declare quarter and initialize it to 0. Then you have the following line:

quarter = cent / quarter;

C is case sensitive, so it allows both the definitiion of QUARTER and the declaration of quarter. You used the wrong one in the division, generating the error. Clearly, you wanted to do this:

quarter = cent / QUARTER;

Creating defines and variables with the same name like this is a very bad and dangerous practice, for the reason you just demonstrated. It's too easy to confuse them!

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

2
  • Thank you so much, this really helped me out greatly <3 – Gamila Essam Sep 16 '15 at 19:47
  • Actually, Gamila did it correctly. If given is ever positive, it is a valid number. If it is less than 0, the negative value caused the program to ask again. This is one of the tests in check50. If the input is invalid, you want to have the loop execute again! As for =0, that's a judgement call. There's no test for it, but "You don't get any change, buddy!" ;-) – Cliff B Sep 17 '15 at 2:59

You must log in to answer this question.

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