0

i'm having some trouble here, not even sure if my loop is correct but i'm also confused as to why I got this error about the why the cents variable can not be modified, if anyone can explain to me what i'm exactly doing wrong here that would be so helpful, thanks so much.

here's my code

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

int main (void)
{
    float dollar;
    int cents;
    do
    {
        dollar = get_float ("Change owed: ");
        cents = round(dollar * 100);
    }
    while (dollar < 0.0 );

    int change = 0;

    for (int quarter = 0; cents =- 25; change++)
    {
        for (int dime = 0; cents - 10; change++)
        {
            printf("%i\n", change);
        }
            for (int nickel = 0; cents - 05; change++)
            {
                printf("%i\n", change);
            }
                for (int penny = 0; cents - 01; change++)
                {
                    printf("%i\n", change);
                }
        printf("%i\n", change);
    }

}
1

You have a consistent problem in all of your for loops.

A for loop has three clauses, as follows, separated by semicolons:

for( initialization ; test condition ; incrementation )

The test condition clauses in all of those for loops are invalid because they are assignment statements, not tests. A test condition will use comparison operators such as ==, >, >=, <, <=, or !=.

The clauses using operators like =- or - are mathematical operators, not comparison operators. The =- operator places the result back in the var to it's left. This is a valid assignment statement. The problem there is that any and all valid assignments ALWAYS evaluate to TRUE when used in a test condition.

The minus operator is just a subtraction operator and the clauses with it don't put the result anywhere. (No = sign and no var to put the result in.) That's why it's invalid.

Now, it is possible to have multiple sub-clauses in each of the three clauses. For example:

int k = 0;
for(int i=2, j=40-x ;  i<n && j>=15 ;  i++, j--, k = i + j)
{
    //some code here
}

Note that the three main clauses are separated by semicolons but the sub-clauses are separated by commas in the first and third clause, and all sub-clauses in the (middle) test clause must be combined somehow, usually with && and/or ||.

You would probably benefit greatly from a google search on how to implement a for loop and how to implement a while loop. (I'm anticipating similar issues.) ;-)

Style note: The second, third and fourth for loops should be indented to the same level. All three are inside the first loop, so should be indented once, but are not further nested, so they should be at the same indentation level. If they were further nested multiple levels deep, then they would need the extra indentation.

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

4
  • thanks so much for your quick response! thank you for quickly explaining the conditions of the loop function, i should have remembered but forgot the test condition clause. :) it seems at least for what i'm trying to do here that a loop won't work, at least how i'm visualizing it. a while loop seems more in line with what i'm trying to do
    – user31964
    Mar 12 at 8:49
  • No problem. ;-) Also, why does the code have the calculations for dimes, nickels and pennies nested inside the for loop for quarters????? That just won't work.
    – Cliff B
    Mar 12 at 9:01
  • i seemed to have mostly solve this but am really stumped on after checking the problem set two answers seem to be off by a single digit. input of 0.15 yields output of 2 only returns 1. input of 23 yields output of 92 returns 93. what's happening here?
    – user31964
    Mar 12 at 9:58
  • No idea without seeing the new code. New problem deserves a new question.
    – Cliff B
    Mar 12 at 21:01

You must log in to answer this question.

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