How to make my output show only whats needed with my cheque generator program for c

时光总嘲笑我的痴心妄想 提交于 2019-12-11 21:02:17

问题


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!

The code is as follows:

#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.


回答1:


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 ");
}



回答2:


The problem is that you are unconditionally printing the "Thousand", "Hundred", etc...

    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");

if the number you send to printNum is zero, you don't want to print out your text string, you'd have to check for that condition:

/* call to printNum x */
if ( /* check if the parameter to printNum matches any case, seems to be if not zero){
    printf(/* whatever string is appropriate */);
}



回答3:


as an example, say the input was 512.26

512.26/1000 <<< that should be 512.26/ 1000.f so both operands of the divide are floats

will result in a float value of : 0.51226f NOT 0.0f

using the floor(0.51226f) Will result in 0.0f

Suggest always applying the floor() function to yield a 0.0f

Even with a float, a comparison such as if( value != 0.0f ) then print it

would work after applying the floor() function.



来源:https://stackoverflow.com/questions/30813698/how-to-make-my-output-show-only-whats-needed-with-my-cheque-generator-program-fo

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!