0

I've put together my answer for the cash problem set, but when I run the program and enter an amount for "change owed" it always outputs 1. Being new to all of this, I'm not sure what I'm doing wrong here. Any advice helps. Thank you!

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

int main(void)

    //prompt user for an amount of change
{   
    float c;
    int n;
    int coins = 0;
    do 
    {
        c = get_float("change owed: ");
    }
    while (c < 0);

    //change float to int
    n = round(c * 100);

    // compute coins

    if (n = 25, n > 25)
    {
        n = n - 25;
        coins = coins + 1;
    }

   else if (n = 10, n < 25)
    {
        n = n - 10;
        coins = coins + 1;
    }

    else if (n = 5, n < 10)
    {
        n = n - 5;
        coins = coins + 1;
    }

    else if (n = 1,  n < 5)
    {
        n = n - 1;
        coins = coins + 1;
    }

   printf("%i\n", coins);
}
1

There are serious issues with the way that you're trying to use IF statements. First of all, "n=x" is always true. A single "=" sign is an assignment operator. In other words, it always assigns the value on the right to the variable on the left, and will always evaluate as true.

If, instead, you want to check if two things are equal, you need to use the equality test operator, two equal signs, ==. That will test whether two values are equal and will return either true or false, depending on if they're equal or not.

Next, test conditions can't be separated by commas. A comma here is meaningless. The two test conditions have to be connected with either a logical AND, && or a logical OR, ||, or some other appropriate logical operator.

Finally, a hint. When should you use an if statement and when should you use a while loop?

This should help you debug your code.

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

| improve this answer | |
  • Thank you so much Cliff-- your help is much appreciated!! – js94 Jun 10 '19 at 17:04

You must log in to answer this question.

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