如何使我的输出只显示什么需要我的校验生成的程序对C [英] How to make my output show only whats needed with my cheque generator program for c

查看:92
本文介绍了如何使我的输出只显示什么需要我的校验生成的程序对C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经能够输出任何数字在被输入到它的程序,但它会将其转换为文字,而不只是数字,例如,如果你输入1234.56,将其转换为一千二百三十四元与... 56美分。仙应该总是用数字。到目前为止,一切都很正常,但是如果我把在少一千个我会得到多余的词,如在有千或百为单位。例如,如果我输入15.77我的输出将是千一百十五美元和...... 77美分。
我不想千元百在那里,没有那些那就完美了!

I have made a program that can output whatever numeral is inputted in to it but it will convert it to words instead of just numerals for example if you input "1234.56" it will convert it to "One Thousand Two Hundred Thirty Four Dollars and ... 56 Cents". The cents should always be in numerals. So far everything works great, however if I put in an amount that is less that one thousand I will get the excess words such as "Thousand" or "Hundred" in there. For example if I input "15.77" my output will be "Thousand Hundred Fifteen Dollars and ... 77 Cents". I don't want the Thousand or Hundred to be there, without those it would be perfect!

在code是如下:

#include <stdio.h>

void printNum(int);
void printNum2(int);
void printNum3(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.01 || 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;

        printNum(a);  //Printing is the variables are in the thousands, hundreds, tens or ones categories.
        printf("Thousand ");
        printNum(b);
        printf("Hundred ");
        printNum2(c);
        printf("");
        printNum3(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("Ninteen ");
     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 ");
}

void printNum3(int x)
{
    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 ");

}

我一直在编码整整一个月,所以如果它看起来像我犯简单的错误,这就是为什么。

I have been coding for exactly one month now so if it seems like I made simple mistakes that's why.

推荐答案

您需要添加在你用printfs条件语句:

You need to add conditionals around your printfs:

if (a > 0)
{
    printNum(a);  //Printing is the variables are in the thousands, hundreds, tens or ones categories.
    printf("Thousand ");
}

这篇关于如何使我的输出只显示什么需要我的校验生成的程序对C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆