根据输入执行一段代码

时间:2014-04-27 09:10:54

标签: c cs50

我得到了以下代码(见下文)。工作正常,只有我想让一件事发生。我现在要求用户输入。如果输入为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);
        }

1 个答案:

答案 0 :(得分:3)

更改

// And code should be terminated!

return 0;