0

It compiles without any problems, but when I try to execute it, the Terminal crashes. Do I get in some infinite loops here?

I would appreciate some help - thanks a lot!

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

int main(void)

{
    // get user input
    float cash;
    do
    {
        cash = get_float("Please enter amount of dollars owed to you: ");
    }
    while (cash <= 0);

    // round cents to nearest penny
    int cents = round(cash * 100);

    // find coins within amount of cents

    int quarters;
    while (cents >= 25)
    {
        quarters = cents % 25;
    }
    return 0;

    int dimes;
    while (cents >= 10)
    {
        dimes = cents % 10;
    }
    return 0;

    int nickels;
    while (cents >= 5)
    {
        nickels = cents % 5;
    }
    return 0;

    int pennies;
    while (cents > 0)
    {
        pennies = cents % 1;
    }
    return 0;

    //print result amount of coins to screen
    printf("%i\n", quarters + dimes + nickels + pennies);
}
1

I can see a few bugs here - the one causing the infinite loop is your while loop.

The condition is while (cents >= 25) but the value of cents never changes/decreases, so this condition will be true forever.

For example, if cents = 40, this condition cents >= 25 is 40 >= 25 which will always evaluate to true unless you decrease the value of cents within your loop.

This also holds for your other while loops as well.

All the best!

You must log in to answer this question.

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