-2

How come my code is not printing the int called coins? I do not see any error while running the debugging system.

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

int main(void)
{  // getting the user validation  
    printf("please enter a amount of change \n");

    int change = get_int();
    int coins = 0;
    if (change > 0 && change <= 0  && change != 0)
        printf("Change: %i\n", change);

    if (change > 0)
    {
        printf("Change: %i\n", change);
    }

    if (change < 0 )
    {
        printf("please enter a positve number \n");
    }

    if (change == 0)
    {
        printf("Coins: 0 \n");
    }
    //were it counts the amount of coins


    float changef = (int)change;
    float qaurter = 0.25;
    float dime = 0.10;
    float nickel = 0.05;
    float penny = 0.01;



    while (changef / qaurter)
    {
     coins++;
     return changef - qaurter;
    }

    while (changef / dime)
    {
     coins++;
     return changef - dime;
    }

    while (changef / nickel)
    {
     coins++;
     return changef - nickel;
    }

    while (changef / penny)
    {
     coins++;
     return changef - penny;
    }
    printf("Coins: %i\n",coins);

}
Kevin J. Chase
  • 3,856
  • 4
  • 21
  • 43
  • In `if (change > 0 && change <= 0 && change != 0)` the first two conditions cannot *both* be true. – Weather Vane Jul 02 '17 at 18:27
  • Please learn to format your code, and post it formatted. It's annoying reading unformatted code. Use `%` which is the module operator. Beside, what do you mean by "is not printing?" When dealing with currency you also should not use `float` and `doubles`, that will lead to wrong results on the long term – Pablo Jul 02 '17 at 18:31
  • 1
    Please read [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) There are *many many* "change" questions here. It is rarely satisfactory to use floating point types. – Weather Vane Jul 02 '17 at 18:32
  • `float changef = (int)change;` does not make sense. Don#t use casts unless you 1) really have to **and** 2) **fully** understand what they do and why you have to use them. And never use floating point if you need exact results, e.g. when dealing with currencies. – too honest for this site Jul 02 '17 at 18:33
  • 2
    Your loops have `return` statements in them. Returning from `main()` is equivalent to exiting the program. You need to revise the code to avoid using `return` in the loops. – Jonathan Leffler Jul 02 '17 at 18:52
  • @JonathanLeffler I didn't read that far: it seems to be the answer to the title statement since `printf("Coins: %i\n",coins);` is never reached. – Weather Vane Jul 02 '17 at 18:55

1 Answers1

3

Your loops are written with return statements in them:

while (changef / qaurter)
{
    coins++;
    return changef - qaurter;
}

Returning from main() is equivalent to exiting the program. You need to revise the code to avoid using return in the loops. For example (including a spelling fix):

while (changef / quarter)
{
    coins++;
    changef -= quarter;
}

Though frankly, you could avoid loops altogether:

int extra = changef / quarter;
coins += extra;
changef -= extra * quarter;

However, there's more surgery to do before that's feasible since you only make change for integer numbers of dollars, but your have quarter and friends defined as float variables with fractional values. You either need to work in cents only (int quarter = 25;), or allow the user to enter a floating point value in the first place, or …

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 1
    As we know, reading a floating point value and using floating point arithmetics for this simple application is calling for more problems, because of floating point precision issues. But it is a very tool to teach a few basic concepts and pitfalls. – chqrlie Jul 02 '17 at 19:14