Why does my program give me the excess output? C program

Derrick Rose

I've made a program that is a cheque generator. Whatever amount you input in the scanf will output in words. For example if I were to to input "1234.56" My output will be "One Thousand Two Hundred Thirty Four Dollars and ... 56 Cents", or if I wanted to input the amount "0.01" the output will be "Zero Dollars and ... 1 Cents". The program works perfectly, however there is a minor issue, if I wanted to input the amount "9909" it will output "Nine Thousand Nine Hundred Ninety Nine Dollars and ... 0 Cents". The output should be "Nine Thousand Nine Hundred Nine Dollars and ... 0 Cents". Please help!

The code is as follows:

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

I have been coding for a month now so I apologize for the simple errors.

Scott Hunter

After you have calculated b and adjusted inclusive so it is now 9, you get to your else case, which sets c to 9 (hence the Ninety output) and d to 0, but immediately afterwards sets d to 9 (hence the Nine in the output). I think you mixed up c and d in the else case.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

C++ Why does this program does not give any output?

From Dev

Why does list in program give an unexpected output?

From Dev

Simple C Program does not give Expected Output

From Dev

Why does my C program keep giving me 1 as the error?

From Dev

Why this Ansi C program does not give result?

From Dev

Why does my program give the wrong output once you have clicked a button?

From Dev

Why does my program give the wrong output once you have clicked a button?

From Dev

program in which there is high iterations loops in C doesn't give me an output .. why?

From Dev

The program does'nt give output

From Dev

Why does my program output a huge decimal?

From Dev

Why does my program output a huge decimal?

From Dev

Why does my attempt to access a MariaDB in my Java program get give me an 'access denied' exception even tho my password is right?

From Dev

Why does printf(i) give 0 as output in this program?

From Dev

Java Program does not give the output for values of n>6, Why?

From Dev

Why is the output of my C program incorrect?

From Dev

Why does Bash give "No such file or directory" for a program that's in my PATH?

From Dev

C Program: Why does this array have this output?

From Java

Why does my output give me the wrong amount of each number?

From Dev

Why does my C program crash?

From Dev

Why such output of C program

From Dev

Why is my program giving me the wrong output when I run it?

From Java

Why does writing this program output me weird strings?

From Dev

Switch case program does not give the output

From Dev

Why my following program does not print any output ( ArrayList )?

From Dev

Why does my program output the wrong highest number?

From Dev

Why does my C program print out the same output no matter what I put for the input?

From Dev

Why does this program output 1?

From Dev

Why does this program output "1"?

From Dev

Why does my C dynamic array give me access violation?

Related Related

  1. 1

    C++ Why does this program does not give any output?

  2. 2

    Why does list in program give an unexpected output?

  3. 3

    Simple C Program does not give Expected Output

  4. 4

    Why does my C program keep giving me 1 as the error?

  5. 5

    Why this Ansi C program does not give result?

  6. 6

    Why does my program give the wrong output once you have clicked a button?

  7. 7

    Why does my program give the wrong output once you have clicked a button?

  8. 8

    program in which there is high iterations loops in C doesn't give me an output .. why?

  9. 9

    The program does'nt give output

  10. 10

    Why does my program output a huge decimal?

  11. 11

    Why does my program output a huge decimal?

  12. 12

    Why does my attempt to access a MariaDB in my Java program get give me an 'access denied' exception even tho my password is right?

  13. 13

    Why does printf(i) give 0 as output in this program?

  14. 14

    Java Program does not give the output for values of n>6, Why?

  15. 15

    Why is the output of my C program incorrect?

  16. 16

    Why does Bash give "No such file or directory" for a program that's in my PATH?

  17. 17

    C Program: Why does this array have this output?

  18. 18

    Why does my output give me the wrong amount of each number?

  19. 19

    Why does my C program crash?

  20. 20

    Why such output of C program

  21. 21

    Why is my program giving me the wrong output when I run it?

  22. 22

    Why does writing this program output me weird strings?

  23. 23

    Switch case program does not give the output

  24. 24

    Why my following program does not print any output ( ArrayList )?

  25. 25

    Why does my program output the wrong highest number?

  26. 26

    Why does my C program print out the same output no matter what I put for the input?

  27. 27

    Why does this program output 1?

  28. 28

    Why does this program output "1"?

  29. 29

    Why does my C dynamic array give me access violation?

HotTag

Archive