1

im struggling with pset1 cash exercise, when im defining a calculate_quarters() and other in the same way i got an error. the task is to find out the number of quarters, dimes, nickels and pennies to reach owed cents. this is my code, im sure something is off with my loop. (im very new to the coding world so please explain as if im newborn)

int get_cents(void)
{
    int cents;
    do
    {
        cents = get_int("Change owed: ");
    }
    while (cents < 0);
    return cents;
}

int calculate_quarters(int cents)
{
    for(int quarters = 0; cents >= 25; quarters++);
    {
        cents = cents - 25
    }
    return quarters;
}

int calculate_dimes(int cents)
{
    for(int dimes = 0; cents >= 10; dimes++);
    {
        cents = cents - 10;
    }
    return dimes;
}

int calculate_nickels(int cents)
{
    for(int nickels = 0; cents >= 5; nickels++);
    {
        cents = cents - 5;
    }
    return nickels;
}

int calculate_pennies(int cents)
{
   for(int pennies = 0; cents >= 5; penniess++);
    {
        cents = cents - 1;
    }
    return pennies;
}

2 Answers 2

4

Well, I had to think carefully about what the code is doing. That mostly comes from not using a best practice.

Here's the best-practice guideline on whether to use a FOR loop or a WHILE loop. When you need to loop a fixed or well defined number of times (such as 1 to whatever is in a variable), the ideal choice is a FOR loop. BUT, when code needs to loop until a certain condition is met and the number of loops required is not well defined, then the code should use a WHILE or DO/WHILE loop. (While loop may not run at all, DO/WHILE is guaranteed to run at least once.)

This is a case where you want to loop until you don't have enough change to count another coin such as a quarter. That means that a while loop is the loop of choice here....

while (cents >= 25)
{
    // do what needs to be done to count coins and decrease cents left. 
}

What you did above may be technically correct (not sure if the count of the type of coin would be correct or off by one, or what other issues you're having), but it's not clear what's going on. A while loop will be quite clear and obvious, plus simpler to implement. Always go for simple.

Also, there's at least one missing semicolon that will prevent it from being compiled. ;-)

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

2

The problem is that you are putting semicolons ';' at the end of your for loops. That makes them just evaluate the expression in the loop and so each would just be an infinite loop. that's bad.. you generally don't want to put a semicolon at the end of a for statement unless the for statement is ITSELF doing the work you want; most times you want the body of block enclosed by brackets to be evaluated for each iteration of the loop.

Theres also two errors in your pennies loop and function .. see if you can spot it. It wont compile as a result of one of them.

3
  • Good catch! No idea how I missed that!
    – Cliff B
    Commented Aug 4, 2023 at 22:34
  • 1
    well you seem to catch literally everything else here so I think it's ok if you miss something once in a while :-) and for what its worth I upvoted your answer as it was also good and I didnt want to be repetitive I agree 100% with everything you said.
    – UpAndAdam
    Commented Aug 4, 2023 at 22:39
  • @CliffB sorta off topic but could you review some edits? I'm unable to edit any of the new posters unformatted code's and it's getting brutal. trying to help but not enough posts to earn reputation to be more helpful on this site.. I wish more points would carry over from StackOverflow to this site
    – UpAndAdam
    Commented Aug 18, 2023 at 16:01

You must log in to answer this question.

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