-5

我得到了以下代码(见下文)。工作正常,只有我想让一件事发生。我现在要求用户输入。如果输入为 0 或低于零,我想确保其余代码不会运行。

关于如何做到这一点的任何建议?

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


int
main(void)
{
float change = 0;
int quarter = 25, dime = 10, nickel = 5, penny = 1;
int quarterc = 0, dimec = 0, nickelc = 0, pennyc = 0; 


printf("For what amount should I perform the calculation\n");
change = GetFloat();
if (change == 0)
{
printf("No change!");
// And code should be terminated!
} else if (change < 0)

{
printf("This is a negative number");
// And code should be terminated!    
}

else {
printf("You entered this amount: %.1f\n", change);
}

change = change * 100;

        while (change >= quarter)
        {
            change = change - quarter;
            quarterc++;
        }

        while (change >= dime)
        {
            change = change - dime;
            dimec++;
        }

        while (change >= nickel)
        {
            change = change - nickel;
            nickelc++;
        }


        while (change >= penny)
        {
            change = change - penny;
            pennyc++;
        }

        // print number of minimum quarters, dimes, nickels, and pennies needed
        printf( "You owe the following number of coins as change:\n");
        printf( "quarters = %d\n" , quarterc);
        printf( "dimes    = %d\n" , dimec);
        printf( "nickels  = %d\n" , nickelc);
        printf( "pennies  = %d\n" , pennyc);

        // print total minimum number of coins needed for change
        printf("The total number of coins needed for change: %d\n", quarterc + dimec + nickelc + pennyc);
        }
4

1 回答 1

3

改变

// And code should be terminated!

return 0;
于 2014-04-27T09:13:50.963 回答