c - 为什么我的程序会给我多余的输出? C程序

标签 c if-statement generator currency

我制作了一个支票生成器程序。无论您在 scanf 中输入多少,都会以文字形式输出。例如,如果我要输入“1234.56”,我的输出将是“一千二百三十四美元和... 56 美分”,或者如果我想输入金额“0.01”,输出将是“零美元和... ... 1 美分”。该程序运行完美,但是有一个小问题,如果我想输入金额“9909”,它将输出“九千九百九十九美元和... 0 美分”。输出应该是“九千九百九元和...0美分”。请帮忙!

代码如下:

#include <stdio.h>

void printNum(int);
void printNum2(int);

int main()
{

    int a = 0;
    int b = 0;
    int c = 0;
    int d = 0; 
    int num = 0;
    int printcents; //To convert the float "cents" to an integer.

    float inclusive;
    float cents;

    printf("Welcome to the IPC144 Cheque Generator!!\n");
    printf("PAY TO THE ORDER OF... amahmood29 (018359133)\n");
    printf("Enter a monetary value from $0.01 to $9999.99 inclusive: ");
    scanf("%f", &inclusive);

    if(inclusive < 0.00 || inclusive >= 10000.00) {
        printf("Sorry, cannot create cheque for that amount, try again next time!\n");
    }
    else
    {                                             
        a = inclusive / 1000;                          //This data is replacing our variable by diving whatever the vaulue is by either 1000, 100, 10.
        inclusive = inclusive - (a*1000);
        b = inclusive / 100; 
        inclusive = inclusive - (b*100);
        if ( inclusive > 19 ){
            c = inclusive / 10; 
            inclusive = inclusive - (c*10);
        }
        else
        {
            c = inclusive;
            d = 0;
        }
        d = inclusive;
        num = inclusive;
        cents = (inclusive - num)*100; //To calculate our "Cents" with numerals.
        printcents = cents;


        /*Printing if the variables are in the thousands, hundreds, tens or ones categories.*/
        if (c == 0 && b == 0 && a == 0){
        printf("Zero Dollars and ... %d Cents\n", printcents);
    }
    else{
        if (a > 0){ 
            printNum(a);  
            printf("Thousand ");
        }
        if (b > 0){
            printNum(b);
            printf("Hundred ");
        }
        printNum2(c);   
        if (d >= 0){
            printNum(d);
            printf("Dollars and ... ");
        }

        printf("%d", printcents);
        printf(" Cents\n");
    }
}
}
void printNum(int x)  //Created functions to easily output various if statements.
{

    if ( x == 1)
        printf("One ");
    else if ( x == 2)
        printf("Two ");
    else if (x == 3)
        printf("Three ");
    else if (x == 4) 
        printf("Four ");
    else if (x == 5)
        printf("Five ");
    else if (x == 6)
        printf("Six ");
    else if (x == 7)
        printf("Seven ");
    else if (x == 8)
        printf("Eight ");
    else if (x == 9)
        printf("Nine ");

    }

void printNum2(int x)
{
    if ( x == 10)
        printf("Ten ");
    else if ( x == 11)
        printf("Eleven ");
    else  if ( x == 12)
        printf("Twelve ");
    else if ( x == 13)
        printf("Thirteen ");
    else if (x == 14)
        printf("Fourteen ");
    else if (x == 15)
        printf("Fifteen ");
    else if (x == 16)
        printf("Sixteen ");
    else if (x == 17)
        printf("Seventeen ");
    else if (x == 18)
        printf("Eighteen ");
    else if (x == 19)
        printf("Nineteen ");
    else if (x == 2)
        printf("Twenty ");
    else if (x == 3)
        printf("Thirty ");
    else if (x == 4)
        printf("Forty ");
    else if (x == 5)
        printf("Fifty ");
    else if (x == 6)
        printf("Sixty ");
    else if (x == 7)
        printf("Seventy ");
    else if (x == 8)
        printf("Eighty ");
    else if (x == 9)
        printf("Ninety ");
}

我已经编码一个月了,所以我为简单的错误道歉。

最佳答案

在你计算出b之后并调整了inclusive所以现在是 9,你得到你的 else案例,它设置了c到 9(因此是 Ninety 输出)和 d为 0,但紧接着设置 d到 9(因此输出中的 Nine)。我觉得你搞混了cdelse案例。

关于c - 为什么我的程序会给我多余的输出? C程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30821723/

相关文章:

c - 将整数类型转换为指针时出现整数指针赋值故障

c - 在 PostgreSQL 中,如何根据 C 函数中的类型 Oid 来识别类型是复合类型?

c# - 从变量更改 if 语句

mysql - 测试表是否存在

ruby-on-rails - 测试数据生成器 CSV - 类似于 Mockaroo

c - POSIX 进程和文件描述符

c - 如何在gdb中的工作目录外设置断点

Python 'if' 语句 - if in [string] == 'string' - 不起作用?

python - 为什么生成器不是上下文管理器?

c++ - QR 码生成器的 Reed-Solomon 算法